#include <bits/stdc++.h>
#pragma warning(disable : 4786)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
long long qmod(long long a, long long n, long long mod) {
  long long ans = 1;
  while (n) {
    if (n & 1ll) {
      ans = ans * a % mod;
    }
    n >>= 1;
    a = a * a % mod;
  }
  return ans;
}
template <class T>
inline T lowbit(T x) {
  return x & -x;
}
template <class T>
inline T sqr(T x) {
  return (x) * (x);
}
template <class T>
inline T my_pow(T a, T n) {
  T ans = 1;
  while (n) {
    if (n & 1ll) {
      ans = ans * a;
    }
    n >>= 1;
    a = a * a;
  }
  return ans;
}
template <class T>
inline T My_Find(T x, T a[], T l, T r) {
  int mid, ans = -1;
  while (l <= r) {
    mid = l + r >> 1;
    if (a[mid] > x)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ans;
}
template <class T>
inline bool scan(T& ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
struct FastIO {
  static const int S = 100 << 1;
  int wpos;
  char wbuf[S];
  FastIO() : wpos(0) {}
  inline int xchar() {
    static char buf[S];
    static int len = 0, pos = 0;
    if (pos == len) {
      pos = 0;
      len = (int)fread(buf, 1, S, stdin);
    }
    if (pos == len) {
      return -1;
    }
    return buf[pos++];
  }
  inline int xint() {
    int s = 1, c = xchar(), x = 0;
    while (c <= 32) {
      c = xchar();
    }
    if (c == '-') {
      s = -1;
      c = xchar();
    }
    for (; '0' <= c && c <= '9'; c = xchar()) {
      x = x * 10 + c - '0';
    }
    return x * s;
  }
  ~FastIO() {
    if (wpos) {
      fwrite(wbuf, 1, wpos, stdout);
      wpos = 0;
    }
  }
} io;
const long long mod = 998244353;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 111;
const int maxm = 2222222;
int n, q, c;
int a[11][maxn][maxn], pre[11][maxn][maxn];
int main() {
  memset(a, 0, sizeof a);
  memset(pre, 0, sizeof pre);
  scanf("%d%d%d", &n, &q, &c);
  for (int i = 1; i <= n; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    a[z][x][y]++;
  }
  for (int k = 0; k <= 10; ++k)
    for (int i = 1; i <= 100; ++i) {
      for (int j = 1; j <= 100; ++j) {
        pre[k][i][j] = pre[k][i - 1][j] + pre[k][i][j - 1] -
                       pre[k][i - 1][j - 1] + a[k][i][j];
      }
    }
  while (q--) {
    int t, x1, yzzz, x2, y2;
    scanf("%d%d%d%d%d", &t, &x1, &yzzz, &x2, &y2);
    int ans = 0;
    for (int i = 0; i <= 10; ++i) {
      ans += (pre[i][x2][y2] - pre[i][x1 - 1][y2] - pre[i][x2][yzzz - 1] +
              pre[i][x1 - 1][yzzz - 1]) *
             ((i + t) % (c + 1));
    }
    printf("%d\n", ans);
  }
  return 0;
}
