#include <bits/stdc++.h>
using namespace std;
long int Node[101][101][11];
long int GetNum(long int T, long int x1, long int y1, long int x2, long int y2,
                long int MaxB) {
  long int total = 0;
  for (int i = 0; i <= MaxB; i++) {
    long int count = (Node[x2][y2][i] - Node[x1 - 1][y2][i] -
                      Node[x2][y1 - 1][i] + Node[x1 - 1][y1 - 1][i]);
    int Number = (i + T) % (MaxB + 1);
    total += Number * count;
  }
  return total;
}
int main() {
  long int n, q, c, x, y, b;
  cin >> n >> q >> c;
  memset(Node, 0, sizeof(Node));
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> b;
    Node[x][y][b]++;
  }
  for (int X = 1; X <= 100; X++)
    for (int Y = 1; Y <= 100; Y++)
      for (int B = 0; B <= c; B++) {
        Node[X][Y][B] +=
            Node[X - 1][Y][B] + Node[X][Y - 1][B] - Node[X - 1][Y - 1][B];
      }
  long int T, x1, y1, x2, y2;
  while (q--) {
    scanf("%ld %ld %ld %ld %ld", &T, &x1, &y1, &x2, &y2);
    cout << GetNum(T, x1, y1, x2, y2, c) << endl;
  }
}
