#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, INF = 1e7 + 3, MAXN = 1e6 + 1;
int dp[120][120][20];
int main() {
  long long n, q, c;
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    int x, y, s;
    cin >> x >> y >> s;
    dp[x][y][s]++;
  }
  for (int i = 1; i <= 105; i++)
    for (int j = 1; j <= 105; j++)
      for (int p = 0; p <= 10; p++) {
        dp[i][j][p] += dp[i - 1][j][p];
        dp[i][j][p] += dp[i][j - 1][p];
        dp[i][j][p] -= dp[i - 1][j - 1][p];
      }
  for (int i = 0; i < q; i++) {
    long long tm, x1, y1, x2, y2;
    cin >> tm >> x1 >> y1 >> x2 >> y2;
    long long ans = 0;
    for (int j = 0; j <= 10; j++) {
      long long s = tm % (c + 1);
      long long val = (j + s) % (c + 1);
      long long ans2 = 0;
      ans2 += dp[x2][y2][j];
      ans2 -= dp[x1 - 1][y2][j];
      ans2 -= dp[x2][y1 - 1][j];
      ans2 += dp[x1 - 1][y1 - 1][j];
      ans += val * ans2;
    }
    cout << ans << endl;
  }
}
