#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, maxx = 100 + 5, maxc = 10 + 5;
struct node {
  int x, y, s;
} st[maxn];
int n, q, c;
int sum[maxc][maxx][maxx];
int main() {
  scanf("%d %d %d", &n, &q, &c);
  for (int i = 1; i <= n; i++) scanf("%d %d %d", &st[i].x, &st[i].y, &st[i].s);
  for (int k = 0; k <= c; k++) {
    for (int i = 1; i <= n; i++) sum[k][st[i].x][st[i].y] += st[i].s;
    for (int i = 1; i <= 100; i++)
      for (int j = 1; j <= 100; j++)
        sum[k][i][j] +=
            sum[k][i - 1][j] + sum[k][i][j - 1] - sum[k][i - 1][j - 1];
    for (int i = 1; i <= n; i++) st[i].s = (st[i].s + 1) % (c + 1);
  }
  for (int i = 1; i <= q; i++) {
    int t, x1, y1, x2, y2;
    scanf("%d %d %d %d %d", &t, &x1, &y1, &x2, &y2);
    t %= (c + 1);
    printf("%d\n", sum[t][x2][y2] - sum[t][x2][y1 - 1] - sum[t][x1 - 1][y2] +
                       sum[t][x1 - 1][y1 - 1]);
  }
  return 0;
}
