#include <bits/stdc++.h>
using namespace std;
struct star {
  int bri[11] = {0};
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  long long int n, q, c, x, y, b, sum = 0, i, j, k, x1, x2, y1, y2, t;
  cin >> n >> q >> c;
  star a[102][102];
  for (i = 1; i <= n; i++) {
    cin >> x >> y >> k;
    a[x][y].bri[k]++;
  }
  for (j = 1; j <= 100; j++) {
    for (i = 2; i <= 100; i++) {
      for (k = 0; k <= c; k++) {
        a[i][j].bri[k] += a[i - 1][j].bri[k];
      }
    }
  }
  int local[11], ans = 0;
  while (q-- > 0) {
    cin >> t >> x1 >> y1 >> x2 >> y2;
    ans = 0;
    for (j = y1; j <= y2; j++) {
      if (x1 == 0) {
        for (k = 0; k <= c; k++) {
          local[k] = a[x2][j].bri[k];
        }
      } else {
        for (k = 0; k <= c; k++) {
          local[k] = a[x2][j].bri[k] - a[x1 - 1][j].bri[k];
        }
      }
      for (k = 0; k <= c; k++) {
        ans += (local[k] * ((k + t) % (c + 1)));
      }
    }
    cout << ans << endl;
  }
}
