#include <bits/stdc++.h>
using namespace std;
int main() {
  const int MAX_X = 100;
  const int MAX_Y = 100;
  const int MAX_C = 10;
  int sum[MAX_X + 2][MAX_Y + 2][MAX_C + 2] = {};
  int n, q, k;
  cin >> n >> q >> k;
  while (n--) {
    int x, y, s;
    cin >> x >> y >> s;
    sum[x][y][s]++;
  }
  for (int x = 1; x <= MAX_X; x++) {
    for (int y = 1; y <= MAX_Y; y++) {
      for (int c = 0; c <= MAX_C; c++) {
        sum[x][y][c] +=
            sum[x - 1][y][c] + sum[x][y - 1][c] - sum[x - 1][y - 1][c];
      }
    }
  }
  while (q--) {
    int t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    int ans = 0;
    for (int i = 0; i <= k; i++) {
      int x = (t + i) % (k + 1);
      ans += x * (sum[x2][y2][i] - sum[x1 - 1][y2][i] - sum[x2][y1 - 1][i] +
                  sum[x1 - 1][y1 - 1][i]);
    }
    cout << ans << endl;
  }
}
