#include <bits/stdc++.h>
using namespace std;
const int inf_int = 2e9;
const long long inf_ll = 2e18;
int Gcd(int p, int q) { return q == 0 ? p : Gcd(q, p % q); }
int Pow(int p, int q) {
  int ans = 1;
  while (q) {
    if (q & 1) ans = ans * p;
    p = p * p;
    q >>= 1;
  }
  return ans;
}
inline int read() {
  int ra, fh;
  char rx;
  rx = getchar(), ra = 0, fh = 1;
  while ((rx < '0' || rx > '9') && rx != '-') rx = getchar();
  if (rx == '-') fh = -1, rx = getchar();
  while (rx >= '0' && rx <= '9') ra *= 10, ra += rx - 48, rx = getchar();
  return ra * fh;
}
int dp[15][105][105];
int main() {
  int n = read(), q = read(), c = read();
  for (int i = 0; i < n; i++) {
    int x = read(), y = read(), z = read();
    for (int j = 0; j <= 10; j++) dp[j][x][y] += (z + j) % (c + 1);
  }
  for (int i = 0; i <= 10; i++) {
    for (int j = 1; j <= 100; j++) {
      for (int k = 1; k <= 100; k++) {
        dp[i][j][k] += dp[i][j - 1][k] + dp[i][j][k - 1] - dp[i][j - 1][k - 1];
      }
    }
  }
  while (q--) {
    int t = read(), x1 = read(), y1 = read(), x2 = read(), y2 = read();
    t = t % (c + 1);
    int res = dp[t][x2][y2] - dp[t][x1 - 1][y2] - dp[t][x2][y1 - 1] +
              dp[t][x1 - 1][y1 - 1];
    printf("%d\n", res);
  }
  return 0;
}
