#include <bits/stdc++.h>
using namespace std;
int dp[105][105][12];
int main() {
  int n, C, q;
  scanf("%d%d%d", &n, &q, &C);
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    dp[a][b][c]++;
  }
  for (int i = 1; i <= 100; i++)
    for (int j = 1; j <= 100; j++)
      for (int k = 0; k <= 10; k++)
        dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
  while (q--) {
    int t, x1, x2, y1, y2;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    long long ans = 0;
    for (int i = 0; i <= C; i++) {
      int l = (i + t) % (C + 1);
      ans += l * (dp[x2][y2][i] - dp[x2][y1 - 1][i] - dp[x1 - 1][y2][i] +
                  dp[x1 - 1][y1 - 1][i]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
