#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 3;
int n, q, c;
long long a[13][103][103], sum[13][103][103];
int get(int x, int r1, int c1, int r2, int c2) {
  return sum[x][r2][c2] - sum[x][r1 - 1][c2] - sum[x][r2][c1 - 1] +
         sum[x][r1 - 1][c1 - 1];
}
int main() {
  memset(a, 0, sizeof(a));
  memset(sum, 0, sizeof(sum));
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> q >> c;
  for (int i = 0; i < n; ++i) {
    int x, y, s;
    cin >> x >> y >> s;
    a[s][x][y]++;
  }
  for (int x = 0; x <= 10; x++) {
    for (int i = 1; i <= 102; ++i)
      for (int j = 1; j <= 102; ++j) {
        sum[x][i][j] = a[x][i][j] + sum[x][i - 1][j] + sum[x][i][j - 1] -
                       sum[x][i - 1][j - 1];
      }
  }
  while (q--) {
    int t, x1, x2, y1, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    long long ans = 0;
    for (int i = 0; i <= c; i++) {
      ans += 1LL * get(i, x1, y1, x2, y2) * ((i + t) % (c + 1));
    }
    cout << ans << '\n';
  }
  return 0;
}
