The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).

Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.

You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right — (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.

A star lies in a rectangle if it lies on its border or lies strictly inside it.

Input

The first line contains three integers n, q, c (1 ≤ n, q ≤ 105, 1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.

The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 ≤ xi, yi ≤ 100, 0 ≤ si ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness.

The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 ≤ ti ≤ 109, 1 ≤ x1i < x2i ≤ 100, 1 ≤ y1i < y2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle.

Output

For each view print the total brightness of the viewed stars.

Examples

Input

2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5


Output

3
0
3


Input

3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51


Output

3
3
5
0

Note

Let's consider the first example.

At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.

At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.

At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
import java.util.*;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.io.*;
public class Main {
 
	static ArrayList<Integer> adj[];
	static char color[];
	static boolean b[],visited[],possible;
	static Map<Integer,HashSet<Integer>> s;
	static int totalnodes,colored;
	static int count[];
	static long sum[];
	public static void main(String[] args) throws IOException {
		
		
		in=new InputReader(System.in);
		w=new PrintWriter(System.out);
      int n=in.nextInt(),q=in.nextInt(),c=in.nextInt();
      int[][][] a=new int[11][101][101];
      int i=0;
      while(n-->0)
      {
    	  int x=in.nextInt(),y=in.nextInt(),b=in.nextInt();
    	++a[b][x][y];
    	++i;
      }
      for(i=0;i<=c;i++)
      {
    	  for(int j=1;j<101;j++)
    	  {
    		  for(int k=1;k<101;k++)
    		  {
    			  
    			  a[i][j][k]+=a[i][j-1][k]+a[i][j][k-1]-a[i][j-1][k-1];
    		  }
    	  }
      }
      while(q-->0)
      {
    	  int t=in.nextInt(),xi=in.nextInt(),yi=in.nextInt(),xj=in.nextInt(),yj=in.nextInt();
    	  long ans=0;
    	   for(i=0;i<=c;i++)
    	   {
    		   int cb=(t+i)%(c+1);
    		   //w.println((a[i][xj][yj]-a[i][xi-1][yj]-a[i][xj][yi-1]+a[i][xi-1][yi-1])*cb);
    		   ans+=(a[i][xj][yj]-a[i][xi-1][yj]-a[i][xj][yi-1]+a[i][xi-1][yi-1])*cb;
    	   }
    	  w.println(ans);
      }
        	 
        
       
       w.close();			
		}
	
	public static long isCube(long n)
	{
		long x=0,y=1000001;
		while(x<=y)
		{
			long m1=x+(y-x)/3,m2=y-(y-x)/3;
			if(m1*m1*m1==n)
				return m1;
			else if(m2*m2*m2==n)
				return m2;
			else if(m1*m1*m1>n)
				  y=m1-1;
			else if(m2*m2*m2<n)
				  x=m2+1;
			else
			{
				x=m1+1;
				y=m2-1;
			}
		}
		return -1;
		
	}

	   public static int kadane (int a[])
	    {
	        int n = a.length;
	        int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
	        for (int i = 0; i < n; i++)
	        {
	            max_ending_here = max_ending_here + a[i];
	           
	            if (max_so_far < max_ending_here)
	                max_so_far = max_ending_here;
	            if (max_ending_here < 0)
	                max_ending_here = 0;
	        }
	        return max_so_far;
	    }
	public static int find_pivot(int[] a,int k)
	{
		int pivot=pivot_element(a,0,a.length-1);
		if(a[pivot]==k)
			 return pivot;
		else if(pivot==-1)
			 return binary_search(a,0,a.length-1,k);
		else if(a[0]<=k)
			 return binary_search(a,0,pivot-1,k);
		else 
			return binary_search(a,pivot+1,a.length-1,k);
		
	}
	public static int pivot_element(int[] a,int l,int r)
	{
	   if(r<l)
		   return -1;
	   if(r==l)
		   return l;
			int mid=(l+r)/2;
			 if (mid < r && a[mid] > a[mid + 1])
			       return mid;
			   if (mid > l && a[mid] < a[mid - 1])
			       return (mid-1);
			   if (a[l] >a[mid])
			       return pivot_element(a, l, mid-1);
			   return pivot_element(a, mid + 1, r);
		
	}
	public static int binary_search(int[] a,int l,int r,int k)
	{
		if(r<l)
			return -1;
		int mid=(l+r)/2;
		if(a[mid]==k)
			 return mid;
		else if(a[mid]>k)
			 return binary_search(a,l,mid-1,k);
		else
			 return binary_search(a,mid+1,r,k); 
	}
	public static String[] subString(String s, int n) 
	{
	    String[] st =new String[(n*n+n)/2];
	    for(int i=0;i<st.length;i++)
	    	st[i]="";
	    int l=0;
	    for (int len = 1; len <= n; len++) 
	    {    
	        // Pick ending point
	        for (int i = 0; i <= n - len; i++) 
	        {
	            //  Print characters from current
	            // starting point to current ending
	            // point.  
	            int j = i + len - 1;            
	            for (int k = i; k <= j; k++) 
	                st[l]+=s.charAt(k);
	      //w.println(st[l]);
	            l++;
	            //cout << endl;
	        }
	    }
	    return st;
	}
	  public static long factorial(long n)
	    {
	        long res = 1, i;
	        for (i=2; i<=n; i++)
	            res *= i;
	        return res;
	    }
	
   
	public static int factors(int n,int start){
		int count = 0;
		int i;
		for(i=1;i*i<n;i++)
			if(n%i == 0){
				if(i>start)	count++;
				if(n/i > start)	count++;
			}
		if(i*i == n && i>start)	count++;
		return count;
	}


 	public static int isPrime(int n){
	    int i;

	    if (n==2)
	        return 1;

	    if (n%2==0)
	        return 0;

	    for (i=3;i<=Math.sqrt(n);i+=2)
	        if (n%i==0)
	            return 0;

	    return 1;
	}

	
	static InputReader in;
	static PrintWriter w;
	public static void seive(long n){
		b = new boolean[(int) (n+1)];
	Arrays.fill(b, true);
		for(int i = 2;i*i<=n;i++){
			if(b[i]){
				sum[i]=count[i];
			//	System.out.println(sum[i]+" wf");
				for(int p = 2*i;p<=n;p+=i){
					b[p] = false;
					sum[i]+=count[p];  
					//System.out.println(sum[i]);
				}
			}
		
		}
	}
	static class Pair implements Comparable<Pair> {
		int x;
		int y;
		int c;
		
		public Pair(){
			
		}
		public Pair(int x,int y,int c) {
			this.x=x;
			this.y=y;
			this.c=c;
			
		}
		public int compareTo(Pair other) {
			
				return 0;
		}
		
		/*public String toString() {
			return "[u=" + u + ", v=" + v + "]";
		}*/
	}
	static class Node2{
		Node2 left = null;
		Node2 right = null;
		Node2 parent = null;
		int data;
	}
	
	//binaryStree
	static class BinarySearchTree{
		Node2 root = null;
		int height = 0;
		int max = 0;
		int cnt = 1;
		ArrayList<Integer> parent = new ArrayList<Integer>();
		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
		public void insert(int x){
			Node2 n = new Node2();
			n.data = x;
			if(root==null){
				root = n;
			}
			else{
				Node2 temp = root,temp2 = null;
				while(temp!=null){
					temp2 = temp;
					if(x>temp.data) temp = temp.right;
					else temp = temp.left;
				}
				if(x>temp2.data) temp2.right = n;
				else temp2.left = n;
				n.parent = temp2;
				parent.add(temp2.data);
			}
		}
		public Node2 getSomething(int x, int y, Node2 n){
			if(n.data==x || n.data==y) return n;
			else if(n.data>x && n.data<y) return n;
			else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
			else return getSomething(x,y,n.left);
		}
		public Node2 search(int x,Node2 n){
			if(x==n.data){
				max = Math.max(max, n.data);
				return n;
			}
			if(x>n.data){
				max = Math.max(max, n.data);
				return search(x,n.right);
			}
			else{
				max = Math.max(max, n.data);
				return search(x,n.left);
			}
		}
		public int getHeight(Node2 n){
			if(n==null) return 0;
			height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
			return height;
		}
	}
public static void debug(Object... o) {
	System.out.println(Arrays.deepToString(o));
}
 
public static String rev(String s)
{
	StringBuilder sb=new StringBuilder(s);
	sb.reverse();
	return sb.toString();
}
 
static long lcm(long a, long b)
{
    return a * (b / gcd(a, b));
}
 
static long gcd(long a, long b)
{
    while (b > 0)
    {
        long temp = b;
        b = a % b; // % is remainder
        a = temp;
    }
    return a;
}
 
public static long max(long x, long y, long z){
		if(x>=y && x>=z) return x;
		if(y>=x && y>=z) return y;
		return z;
	}
	static int[] sieve(int n,int[] arr)
	{
	for(int i=2;i*i<=n;i++)
	{
		if(arr[i]==0)
		{
			for(int j=i*2;j<=n;j+=i)
				arr[j]=1;
		}
	}
	return arr;
	}
 
 
 
 
static class InputReader {
 
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
 
public InputReader(InputStream stream) {
  this.stream = stream;
}
 
public int snext() {
  if (snumChars == -1)
    throw new InputMismatchException();
  if (curChar >= snumChars) {
    curChar = 0;
    try {
      snumChars = stream.read(buf);
    } catch (IOException e) {
      throw new InputMismatchException();
    }
    if (snumChars <= 0)
      return -1;
  }
  return buf[curChar++];
}
 
public int nextInt() {
  int c = snext();
  while (isSpaceChar(c)) {
    c = snext();
  }
  int sgn = 1;
  if (c == '-') {
    sgn = -1;
    c = snext();
  }
  int res = 0;
  do {
    if (c < '0' || c > '9')
      throw new InputMismatchException();
    res *= 10;
    res += c - '0';
    c = snext();
  } while (!isSpaceChar(c));
  return res * sgn;
}
 
public long nextLong() {
  int c = snext();
  while (isSpaceChar(c)) {
    c = snext();
  }
  int sgn = 1;
  if (c == '-') {
    sgn = -1;
    c = snext();
  }
  long res = 0;
  do {
    if (c < '0' || c > '9')
      throw new InputMismatchException();
    res *= 10;
    res += c - '0';
    c = snext();
  } while (!isSpaceChar(c));
  return res * sgn;
}
 
public int[] nextIntArray(int n) {
	 return nextIntArray(n, 0);
	    }
	    
public int[] nextIntArray(int n, int off) {
	 int[] arr = new int[n + off];
	   for (int i = 0; i < n; i++) {
	    arr[i + off] = nextInt();
	    }
	    return arr;
	    }
	    
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
	    }
        
	public long[] nextLongArray(int n, int off) {
	 long[] arr = new long[n + off];
	 for (int i = 0; i < n; i++) {
		  arr[i + off] = nextLong();
		 }
 return arr;
	}
 
 
public String readString() {
  int c = snext();
  while (isSpaceChar(c)) {
    c = snext();
  }
  StringBuilder res = new StringBuilder();
  do {
    res.appendCodePoint(c);
    c = snext();
  } while (!isSpaceChar(c));
  return res.toString();
}
 
public String nextLine() {
  int c = snext();
  while (isSpaceChar(c))
    c = snext();
  StringBuilder res = new StringBuilder();
  do {
    res.appendCodePoint(c);
    c = snext();
  } while (!isEndOfLine(c));
  return res.toString();
}
 
public boolean isSpaceChar(int c) {
  if (filter != null)
    return filter.isSpaceChar(c);
  return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
 
private boolean isEndOfLine(int c) {
  return c == '\n' || c == '\r' || c == -1;
}
 
public interface SpaceCharFilter {
  public boolean isSpaceChar(int ch);
}
}
	
}