#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e5 + 5;
int n, q, c;
int a[105][105][15];
int pref[105][105][15];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    int x, y, s;
    cin >> x >> y >> s;
    a[x][y][s]++;
  }
  for (int i = 1; i <= 100; i++)
    for (int j = 1; j <= 100; j++)
      for (int k = 0; k <= c; k++) {
        pref[i][j][k] = pref[i - 1][j][k] + pref[i][j - 1][k];
        pref[i][j][k] -= pref[i - 1][j - 1][k];
        pref[i][j][k] += a[i][j][k];
      }
  while (q--) {
    int t, xa, ya, xb, yb;
    cin >> t >> xa >> ya >> xb >> yb;
    int ans = 0;
    for (int i = 0; i <= c; i++) {
      int cnt = pref[xb][yb][i] - pref[xa - 1][yb][i];
      cnt -= pref[xb][ya - 1][i];
      cnt += pref[xa - 1][ya - 1][i];
      int cur = (i + t) % (c + 1);
      ans += cur * cnt;
    }
    cout << ans << '\n';
  }
  return 0;
}
