#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double EPS = 1e-6;
int dp[101][101][11];
void solve() {
  int n, q, c;
  cin >> n >> q >> c;
  int x, y, s;
  for (int i = (0); i < (int)(n); i++) {
    cin >> x >> y >> s;
    dp[x][y][s]++;
  }
  for (int i = (0); i <= (int)(c); i++) {
    for (int j = (1); j <= (int)(100); j++) {
      for (int k = (1); k <= (int)(100); k++) {
        dp[j][k][i] += dp[j - 1][k][i] + dp[j][k - 1][i] - dp[j - 1][k - 1][i];
      }
    }
  }
  int x1, y1, x2, y2, t;
  for (int i = (0); i < (int)(q); i++) {
    long long ans(0);
    cin >> t >> x1 >> y1 >> x2 >> y2;
    for (int j = (0); j <= (int)(c); j++) {
      ans += ((j + t) % (c + 1)) * (dp[x2][y2][j] - dp[x2][y1 - 1][j] -
                                    dp[x1 - 1][y2][j] + dp[x1 - 1][y1 - 1][j]);
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
