#include <bits/stdc++.h>
using namespace std;
int arr[201][201][21];
int pref_sum[201][201][21];
int calc(int x1, int y1, int x2, int y2, int t, int c) {
  int s = t % (c + 1);
  return pref_sum[x2][y2][s] + pref_sum[x1 - 1][y1 - 1][s] -
         pref_sum[x2][y1 - 1][s] - pref_sum[x1 - 1][y2][s];
}
int main() {
  int n, q, c;
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    int x, y, s;
    cin >> x >> y >> s;
    arr[x][y][0] += s;
    for (int j = 1; j < c + 1; j++) arr[x][y][j] += (s + j) % (c + 1);
  }
  for (int q = 0; q < c + 1; q++)
    for (int i = 1; i <= 100; i++) {
      int str = 0;
      for (int j = 0; j <= 100; j++) {
        str += arr[i][j][q];
        pref_sum[i][j][q] = pref_sum[i - 1][j][q] + str;
      }
    }
  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2, t;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    cout << calc(x1, y1, x2, y2, t, c) << endl;
  }
}
