#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int C = 101;
int dp[11][C][C];
int main() {
  ios::sync_with_stdio(0);
  int n, q, MAX;
  cin >> n >> q >> MAX;
  int x, y, s;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> s;
    dp[s][x][y]++;
  }
  for (int p = 0; p <= MAX; p++) {
    for (int i = 1; i < C; i++) {
      for (int j = 1; j < C; j++) {
        dp[p][i][j] += dp[p][i - 1][j] + dp[p][i][j - 1] - dp[p][i - 1][j - 1];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int t, x1, x2, y1, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    int ans = 0;
    for (int p = 0; p <= MAX; p++) {
      int b = (p + t) % (MAX + 1);
      int amount = dp[p][x2][y2] - dp[p][x1 - 1][y2] - dp[p][x2][y1 - 1] +
                   dp[p][x1 - 1][y1 - 1];
      ans += b * amount;
    }
    cout << ans << endl;
  }
  return 0;
}
