#include <bits/stdc++.h>
namespace fastIO {
bool IOerror = 0;
inline char nc() {
  static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 100000, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
template <class T>
inline bool read(T& x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror) return false;
  if (ch == '-') sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
  if (sign) x = -x;
  return true;
}
inline bool read(double& x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror) return false;
  if (ch == '-') sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
  if (ch == '.') {
    double tmp = 1;
    ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
      tmp /= 10.0, x += tmp * (ch - '0');
  }
  if (sign) x = -x;
  return true;
}
inline bool read(char* s) {
  char ch = nc();
  for (; blank(ch); ch = nc())
    ;
  if (IOerror) return false;
  for (; !blank(ch) && !IOerror; ch = nc()) *s++ = ch;
  *s = 0;
  return true;
}
inline bool read(char& c) {
  for (c = nc(); blank(c); c = nc())
    ;
  if (IOerror) {
    c = -1;
    return false;
  }
  return true;
}
template <class T, class... U>
bool read(T& h, U&... t) {
  return read(h) && read(t...);
}
};  // namespace fastIO
using namespace fastIO;
using namespace std;
const long long N = 1e2 + 5;
const double eps = 1e-7;
const double pi = acos(-1.0);
const long long mod = 998244353;
long long sum[N][N][15];
long long n, q, c;
void f() {
  for (long long x = 1; x <= 100; x++)
    for (long long y = 1; y <= 100; y++)
      for (long long c = 0; c <= 10; c++)
        sum[x][y][c] +=
            sum[x - 1][y][c] + sum[x][y - 1][c] - sum[x - 1][y - 1][c];
}
signed main() {
  read(n, q, c);
  for (long long i = 1; i <= n; i++) {
    long long ini, x, y;
    read(x, y, ini);
    sum[x][y][ini]++;
  }
  f();
  while (q--) {
    long long t, x1, y1, x2, y2, res = 0;
    read(t, x1, y1, x2, y2);
    for (long long tc = 0; tc <= c; tc++) {
      long long nc = (tc + t) % (c + 1);
      res += nc * (sum[x2][y2][tc] - sum[x2][y1 - 1][tc] - sum[x1 - 1][y2][tc] +
                   sum[x1 - 1][y1 - 1][tc]);
    }
    printf("%lld\n", res);
  }
  return 0;
}
