#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, c;
  cin >> n >> q >> c;
  int ar[101][101][11];
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      for (int k = 0; k < 11; k++) {
        ar[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int x, y, b;
    cin >> x >> y >> b;
    ar[x][y][b]++;
  }
  for (int k = 0; k <= 10; k++) {
    for (int i = 1; i < 101; i++) {
      for (int j = 1; j < 101; j++) {
        ar[i][j][k] = (ar[i][j - 1][k] + ar[i - 1][j][k] - ar[i - 1][j - 1][k] +
                       ar[i][j][k]);
      }
    }
  }
  while (q--) {
    int t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    int res = 0;
    for (int i = 0; i < 11; i++) {
      int ins = (t % (c + 1) + i % (c + 1)) % (c + 1);
      res += ((ar[x2][y2][i] - ar[x2][y1 - 1][i] - ar[x1 - 1][y2][i] +
               ar[x1 - 1][y1 - 1][i]) *
              ins);
    }
    cout << res << endl;
  }
}
