#include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
int bit[16][maxn][maxn];
int sum(int ss, int i, int j) {
  int t, s = 0;
  while (i > 0) {
    t = j;
    while (t > 0) {
      s += bit[ss][i][t];
      t -= t & -t;
    }
    i -= i & -i;
  }
  return s;
}
void add(int ss, int i, int j) {
  int t, x = 1;
  while (i <= maxn) {
    t = j;
    while (t <= maxn) {
      bit[ss][i][t] += x;
      t += t & -t;
    }
    i += i & -i;
  }
}
int main() {
  int n, q, c;
  scanf("%d%d%d", &n, &q, &c);
  int x, y, ss;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x, &y, &ss);
    add(ss, x, y);
  }
  int t, x1, y1, x2, y2;
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &t, &x1, &y1);
    scanf("%d%d", &x2, &y2);
    int ans = 0;
    for (int j = 0; j <= c; j++) {
      ans += (sum(j, x2, y2) - sum(j, x2, y1 - 1) - sum(j, x1 - 1, y2) +
              sum(j, x1 - 1, y1 - 1)) *
             ((j + t) % (c + 1));
    }
    printf("%d\n", ans);
  }
  return 0;
}
