#include <bits/stdc++.h>
using namespace std;
const long long N = 10000;
long long dp[105][105][11], a[105][105];
signed main() {
  ios::sync_with_stdio(false);
  long long n, q, c;
  cin >> n >> q >> c;
  for (long long i = 1; i <= n; i++) {
    long long x, y, v;
    cin >> x >> y >> v;
    dp[x][y][v]++;
  }
  for (long long k = 0; k <= c; k++) {
    for (long long i = 1; i <= 100; i++) {
      for (long long j = 1; j <= 100; j++)
        dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
    }
  }
  while (q--) {
    long long t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    long long ans = 0;
    for (long long k = 0; k <= c; k++) {
      long long cnt = dp[x2][y2][k] - dp[x2][y1 - 1][k] - dp[x1 - 1][y2][k] +
                      dp[x1 - 1][y1 - 1][k];
      ans += (k + t) % (c + 1) * cnt;
    }
    cout << ans << endl;
  }
}
