#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, c;
  int a, b, v;
  int sum, ans;
  int t, x1, x2, y1, y2;
  int star[105][105][11];
  memset(star, 0, sizeof(star));
  scanf("%d %d %d", &n, &q, &c);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &a, &b, &v);
    star[a][b][v]++;
  }
  for (int i = 1; i <= 101; i++) {
    for (int j = 1; j <= 101; j++) {
      for (int k = 0; k <= c; k++) {
        star[i][j][k] +=
            star[i - 1][j][k] + star[i][j - 1][k] - star[i - 1][j - 1][k];
      }
    }
  }
  while (q--) {
    scanf("%d %d %d %d %d", &t, &x1, &y1, &x2, &y2);
    sum = 0;
    ans = 0;
    for (int i = 0; i <= c; i++) {
      sum = star[x2][y2][i] - star[x1 - 1][y2][i] - star[x2][y1 - 1][i] +
            star[x1 - 1][y1 - 1][i];
      ans += (i + t) % (c + 1) * sum;
    }
    printf("%d\n", ans);
  }
}
