#include <bits/stdc++.h>
using namespace std;
int sum[105][105][12];
int main() {
  int n, q, c, ans = 0;
  ;
  int s, x, y, t, x1, y1;
  scanf("%d%d%d", &n, &q, &c);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x, &y, &s);
    sum[x][y][s]++;
  }
  for (int i = 1; i <= 100; i++)
    for (int j = 1; j <= 100; j++)
      for (int k = 0; k <= 10; k++)
        sum[i][j][k] = sum[i][j][k] + sum[i - 1][j][k] + sum[i][j - 1][k] -
                       sum[i - 1][j - 1][k];
  while (q--) {
    scanf("%d%d%d%d%d", &t, &x, &y, &x1, &y1);
    ans = 0;
    for (int k = 0; k <= c; k++) {
      int v = (k + t) % (c + 1);
      int cnt = sum[x1][y1][k] + sum[x - 1][y - 1][k] - sum[x1][y - 1][k] -
                sum[x - 1][y1][k];
      ans += v * cnt;
    }
    printf("%d\n", ans);
  }
  return 0;
}
