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