#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
const int MXN = 3e5 + 612;
long long int dp[101][101][12];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, q, c;
  cin >> n >> q >> c;
  c++;
  for (long long int i = 0; i < n; i++) {
    long long int x, y, z;
    cin >> x >> y >> z;
    dp[x][y][z]++;
  }
  for (long long int k = 0; k < c; k++) {
    for (long long int i = 1; i < 101; i++) {
      for (long long int j = 1; j < 101; 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 int t, x1, x2, y1, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    x1--, y1--;
    long long int ans = 0;
    for (long long int i = 0; i < c; i++) {
      long long int val = (i + t) % c;
      ans +=
          (dp[x2][y2][i] - dp[x1][y2][i] - dp[x2][y1][i] + dp[x1][y1][i]) * val;
    }
    cout << ans << "\n";
  }
  return 0;
}
