#include <bits/stdc++.h>
using namespace std;
int pet[103][103];
int dp[103][103][15];
int RMS(int x1, int y1, int x2, int y2, int t) {
  return dp[x2][y2][t] - dp[x1 - 1][y2][t] - dp[x2][y1 - 1][t] +
         dp[x1 - 1][y1 - 1][t];
}
int main() {
  int n, q, c;
  scanf("%d %d %d", &n, &q, &c);
  for (int i = 0; i < n; i++) {
    int x, y, s;
    scanf("%d %d %d", &x, &y, &s);
    for (int j = 0; j <= c; j++) dp[x][y][j] += (s + j) % (c + 1);
  }
  for (int i = 1; i <= 100; i++)
    for (int j = 1; j <= 100; j++)
      for (int k = 0; k <= c; k++)
        dp[i][j][k] += dp[i][j - 1][k] + dp[i - 1][j][k] - dp[i - 1][j - 1][k];
  while (q--) {
    int x1, y1, x2, y2, t;
    scanf("%d %d %d %d %d", &t, &x1, &y1, &x2, &y2);
    printf("%d\n", RMS(x1, y1, x2, y2, t % (c + 1)));
  }
}
