#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, q, c;
int dp[109][109][19], a[109][109][19];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q >> c;
  for (int i = 1; i <= n; i++) {
    int x, y, si;
    cin >> x >> y >> si;
    for (int k = 0; k <= c; k++) {
      a[x][y][k] += si;
      ++si;
      si %= (c + 1);
    }
  }
  for (int w = 0; w <= c; w++) {
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        dp[i][j][w] = (a[i][j][w] + dp[i - 1][j][w] + dp[i][j - 1][w] -
                       dp[i - 1][j - 1][w]);
      }
    }
  }
  while (q--) {
    long long t;
    cin >> t;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    t = t % (c + 1);
    cout << (dp[x2][y2][t] - dp[x2][y1 - 1][t] - dp[x1 - 1][y2][t] +
             dp[x1 - 1][y1 - 1][t])
         << endl;
  }
  return 0;
}
