#include <bits/stdc++.h>
using namespace std;
int f[12][105][105];
int n = 102;
int m = 102;
void update(int c, int x, int y, int delta) {
  for (int i = x; i <= n; i = i | (i + 1))
    for (int j = y; j <= m; j = j | (j + 1)) f[c][i][j] += delta;
}
int getSum(int c, int x, int y) {
  int res = 0;
  for (int i = x; i > 0; i = (i & (i + 1)) - 1)
    for (int j = y; j > 0; j = (j & (j + 1)) - 1) res += f[c][i][j];
  return res;
}
int getSum(int c, int xFrom, int xTo, int yFrom, int yTo) {
  return getSum(c, xTo, yTo) - getSum(c, xTo, yFrom - 1) -
         getSum(c, xFrom - 1, yTo) + getSum(c, xFrom - 1, yFrom - 1);
}
int cnt[12];
int main() {
  int N, q, c, x, y_, s;
  int t, x1, x2, y1, y2;
  cin >> N >> q >> c;
  for (int i = 1; i <= N; i++) {
    scanf("%d%d%d", &x, &y_, &s);
    update(s, x, y_, 1);
  }
  for (int i = 1; i <= q; i++) {
    long long ans = 0;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    for (int j = 0; j <= c; j++) {
      cnt[j] = getSum(j, x1, x2, y1, y2);
    }
    for (int j = 0; j <= c; j++) {
      ans += ((j + t) % (c + 1)) * cnt[j];
    }
    printf("%lld\n", ans);
  }
}
