#include <bits/stdc++.h>
using namespace std;
int a[107][107][17], b[107][107][17];
int main() {
  ios_base::sync_with_stdio(false);
  int n, q, c;
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    int x, y, s;
    cin >> x >> y >> s;
    b[x][y][s]++;
  }
  for (int i = 1; i <= 100; i++)
    for (int j = 1; j <= 100; j++) {
      for (int t = 0; t <= c; t++) {
        a[i][j][t] += a[i - 1][j][t];
        for (int z = 0; z <= j; z++) a[i][j][t] += b[i][z][t];
      }
    }
  int t1, x1, y1, x2, y2;
  for (int i = 1; i <= q; i++) {
    cin >> t1 >> x1 >> y1 >> x2 >> y2;
    int ans = 0;
    for (int j = 0; j <= c; j++) {
      int act = (j + t1) % (c + 1);
      ans += (a[x2][y2][j] + a[x1 - 1][y1 - 1][j] - a[x1 - 1][y2][j] -
              a[x2][y1 - 1][j]) *
             act;
    }
    cout << ans << endl;
  }
  return 0;
}
