#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
struct aa {
  int x;
  int y;
  int s;
} a[MAX];
long long g[15][120][120];
long long cha[15][120][120];
long long sum;
int n, q, c;
int t, x11, y11, x22, y22;
int main() {
  memset(g, 0, sizeof(g));
  memset(cha, 0, sizeof(cha));
  scanf("%d%d%d", &n, &q, &c);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].s);
    g[0][a[i].x][a[i].y] += a[i].s;
  }
  for (int i = 1; i <= c; i++) {
    for (int j = 1; j <= n; j++) {
      int dq = a[j].s + i;
      dq %= c + 1;
      g[i][a[j].x][a[j].y] += dq;
    }
  }
  for (int p = 0; p <= c; p++) {
    for (int i = 1; i <= 110; i++) {
      for (int j = 1; j <= 110; j++) {
        cha[p][i][j] = cha[p][i][j - 1] + g[p][j][i];
      }
    }
  }
  while (q--) {
    scanf("%d%d%d%d%d", &t, &x11, &y11, &x22, &y22);
    int tt = t % (c + 1);
    sum = 0;
    for (int i = y11; i <= y22; i++) {
      sum += cha[tt][i][x22] - cha[tt][i][x11 - 1];
    }
    printf("%I64d\n", sum);
  }
  return 0;
}
