#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int no[maxn][maxn][15], sum[maxn][maxn][15];
int main() {
  int n, q, c, ans, i, j, k, x, y, s, ss, t, x1, x2, y1, y2, tt, num;
  while (scanf("%d%d%d", &n, &q, &c) != EOF) {
    memset(no, 0, sizeof(no));
    memset(sum, 0, sizeof(sum));
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d", &x, &y, &s);
      no[x][y][s]++;
    }
    for (j = 1; j <= 100; j++) {
      for (i = 1; i <= 100; i++) {
        for (k = 0; k <= c; k++) {
          sum[i][j][k] = sum[i - 1][j][k] + no[i][j][k];
        }
      }
    }
    while (q--) {
      ans = 0;
      scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
      for (j = y1; j <= y2; j++) {
        for (k = 0; k <= c; k++) {
          num = sum[x2][j][k] - sum[x1][j][k] + no[x1][j][k];
          if (num > 0) {
            if (t <= c - k)
              ss = k + t;
            else {
              x = t - (c - k);
              tt = x % (c + 1);
              if (tt)
                ss = tt - 1;
              else
                ss = c;
            }
            ans += num * ss;
          }
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
