#include <bits/stdc++.h>
inline void r(int &a) {
  static char c;
  while ((c = getchar()) < '0') continue;
  a = c - '0';
  while ((c = getchar()) >= '0') a = a * 10 + c - '0';
}
inline int lowbit(int x) { return x & (-x); }
const int N = 1e5 + 10, M = 1e2 + 10, O = 20;
int n, q, c, b[M][M][O];
inline void add(int x, int y, int k) {
  for (int i = x; i < M; i += lowbit(i))
    for (int j = y; j < M; j += lowbit(j)) b[i][j][k]++;
}
inline int get(int x, int y, int k) {
  static int ans;
  ans = 0;
  for (int i = x; i; i -= lowbit(i))
    for (int j = y; j; j -= lowbit(j)) ans += b[i][j][k];
  return ans;
}
inline int get(int x1, int y1, int x2, int y2, int k) {
  return get(x2, y2, k) - get(x1 - 1, y2, k) - get(x2, y1 - 1, k) +
         get(x1 - 1, y1 - 1, k);
}
int main() {
  r(n);
  r(q);
  r(c);
  for (int i = 1; i <= n; i++) {
    static int x, y, s;
    r(x);
    r(y);
    r(s);
    add(x, y, s);
  }
  while (q--) {
    static int t, x1, y1, x2, y2, ans, w;
    r(t);
    r(x1);
    r(y1);
    r(x2);
    r(y2);
    ans = 0;
    for (int i = 0; i <= c; i++) {
      w = (i + t) % (c + 1);
      ans += w * get(x1, y1, x2, y2, i);
    }
    printf("%d\n", ans);
  }
  return 0;
}
