#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
const int INF = 0x7ffffff;
const int MOD = 1e9 + 7;
struct star {
  int x, y, s;
} all[MAXN + 17];
int dp[100 + 17][100 + 17][10 + 17];
int main() {
  int n, q, c;
  cin >> n >> q >> c;
  for (int i = 0; i < n; ++i) scanf("%d%d%d", &all[i].x, &all[i].y, &all[i].s);
  for (int i = 0; i <= c; ++i) {
    for (int j = 0; j < n; ++j) dp[all[j].x][all[j].y][i] += all[j].s;
    for (int j = 1; j <= 100; ++j)
      for (int k = 1; k <= 100; ++k)
        dp[j][k][i] += dp[j - 1][k][i] + dp[j][k - 1][i] - dp[j - 1][k - 1][i];
    for (int j = 0; j < n; ++j) (++all[j].s) %= (c + 1);
  }
  for (int i = 0; i < q; ++i) {
    int a, b, x, d, e;
    scanf("%d%d%d%d%d", &a, &b, &x, &d, &e);
    a %= (c + 1);
    long long ans =
        dp[d][e][a] - dp[d][x - 1][a] - dp[b - 1][e][a] + dp[b - 1][x - 1][a];
    cout << ans << endl;
  }
  return 0;
}
