#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
const int M = 1e9 + 7;
long long dp[11][N][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, q, c, x, y, s;
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> s;
    for (int j = 0; j <= c; j++) dp[j][x][y] += (s + j) % (c + 1);
  }
  for (int k = 0; k <= c; k++) {
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        dp[k][i][j] += dp[k][i - 1][j] + dp[k][i][j - 1] - dp[k][i - 1][j - 1];
      }
    }
  }
  while (q--) {
    long long t, x1, x2, y1, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    t = t % (c + 1);
    cout << dp[t][x2][y2] + dp[t][x1 - 1][y1 - 1] - dp[t][x1 - 1][y2] -
                dp[t][x2][y1 - 1]
         << "\n";
  }
}
