#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, q, c;
long long sum[N][N][20];
vector<int> grid[N][N];
int main() {
  scanf("%d%d%d", &n, &q, &c);
  for (int i = (1); i <= (int)(n); ++i) {
    int x, y, second;
    scanf("%d%d%d", &x, &y, &second);
    grid[x][y].push_back(second);
  }
  for (int t = (0); t <= (int)(10); ++t) {
    for (int x = (1); x <= (int)(100); ++x)
      for (int y = (1); y <= (int)(100); ++y) {
        for (int a : grid[x][y]) {
          sum[x][y][t] += (a + t) % (c + 1);
        }
      }
    for (int x = (1); x <= (int)(100); ++x)
      for (int y = (1); y <= (int)(100); ++y)
        sum[x][y][t] +=
            sum[x][y - 1][t] + sum[x - 1][y][t] - sum[x - 1][y - 1][t];
  }
  while (q--) {
    int t, x1, y1, x2, y2;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    t %= (c + 1);
    printf("%lld\n", sum[x2][y2][t] - sum[x1 - 1][y2][t] - sum[x2][y1 - 1][t] +
                         sum[x1 - 1][y1 - 1][t]);
  }
  return 0;
}
