#include <bits/stdc++.h>
const long long inf = 1e9 + 44;
const int MAX = 3e5 + 9;
const long long MOD = 1e9 + 7;
const double eps = 1e-10;
double const PI = 3.1415926535897931;
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void _print(vector<int> v) {
  cout << "[ ";
  for (auto u : v) cout << u << " ";
  cout << " ]" << endl;
}
int main() {
  int n, q, c;
  cin >> n >> q >> c;
  int dp[c + 1][103][103];
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    int x, y, s;
    cin >> x >> y >> s;
    dp[s][x][y]++;
  }
  for (int i = 0; i <= c; i++) {
    for (int r = 1; r <= 101; r++)
      for (int c = 1; c <= 101; c++)
        dp[i][r][c] += dp[i][r - 1][c] + dp[i][r][c - 1] - dp[i][r - 1][c - 1];
  }
  for (int i = 0; i < q; i++) {
    int t, x1, y1, x2, y2, ans = 0;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    for (int p = 0; p <= c; p++) {
      int bg = (p + t) % (c + 1);
      int am = dp[p][x2][y2] - dp[p][x1 - 1][y2] - dp[p][x2][y1 - 1] +
               dp[p][x1 - 1][y1 - 1];
      ans = ans + bg * am;
    }
    cout << ans << endl;
  }
}
