#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxp = 1e2 + 10;
const int maxn = 1e5 + 10;
int n, q, c;
struct star {
  int x, y;
  int val;
} stars[maxn];
int mx[13][maxp][maxp];
int main() {
  scanf("%d%d%d", &n, &q, &c);
  for (int i = (0); i < (n); i++) {
    scanf("%d%d%d", &stars[i].x, &stars[i].y, &stars[i].val);
  }
  c++;
  for (int i = (0); i < (c); i++) {
    for (int j = (0); j < (n); j++) {
      int x = stars[j].x, y = stars[j].y, val = stars[j].val;
      mx[i][x][y] += (val + i) % c;
    }
    for (int k = (1); k < (100 + 1); k++) {
      for (int z = (1); z < (100 + 1); z++) {
        mx[i][k][z] +=
            (mx[i][k - 1][z] + mx[i][k][z - 1] - mx[i][k - 1][z - 1]);
      }
    }
  }
  for (int i = (0); i < (q); i++) {
    int t, x1, x2, y1, y2;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    t = t % c;
    printf("%d\n", mx[t][x2][y2] - mx[t][x1 - 1][y2] - mx[t][x2][y1 - 1] +
                       mx[t][x1 - 1][y1 - 1]);
  }
  return 0;
}
