#include <bits/stdc++.h>
using namespace std;
int sum[11][110][110];
int s[110][110];
struct node {
  int x, y, s;
} p[100010];
int main(void) {
  int n, q, c, i, j, k;
  while (scanf("%d%d%d", &n, &q, &c) == 3) {
    for (i = 1; i <= n; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].s);
    memset(sum, 0, sizeof(sum));
    for (i = 0; i <= c; i++) {
      memset(s, 0, sizeof(s));
      for (j = 1; j <= n; j++) s[p[j].x][p[j].y] += (p[j].s + i) % (c + 1);
      for (j = 1; j <= 100; j++) {
        for (k = 1; k <= 100; k++) {
          sum[i][j][k] = sum[i][j - 1][k] + sum[i][j][k - 1] -
                         sum[i][j - 1][k - 1] + s[j][k];
        }
      }
    }
    while (q--) {
      int t, x, y, xx, yy;
      scanf("%d%d%d%d%d", &t, &x, &y, &xx, &yy);
      t = t % (c + 1);
      int ans = sum[t][xx][yy] - sum[t][xx][y - 1] - sum[t][x - 1][yy] +
                sum[t][x - 1][y - 1];
      printf("%d\n", ans);
    }
  }
  return 0;
}
