#include <bits/stdc++.h>
using namespace std;
long long a[11][101][101];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    long long n, i, j, p = 0, m, k, total, x, y, q, c;
    cin >> n >> q >> c;
    for (i = 0; i < n; i++) {
      cin >> x >> y >> p;
      a[p][x][y]++;
    }
    for (k = 0; k < 11; k++) {
      for (i = 1; i < 101; i++) {
        for (j = 1; j < 101; j++) {
          a[k][i][j] += a[k][i - 1][j] + a[k][i][j - 1] - a[k][i - 1][j - 1];
        }
      }
    }
    while (q--) {
      long long x1, x2, y1, y2, p1;
      cin >> p1 >> x1 >> y1 >> x2 >> y2;
      long long ans = 0;
      for (i = 0; i < 11; i++) {
        total = a[i][x2][y2] - a[i][x1 - 1][y2] - a[i][x2][y1 - 1] +
                a[i][x1 - 1][y1 - 1];
        long long st = (i + p1) % (c + 1);
        ans += (st * total);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
