#include <bits/stdc++.h>
using namespace std;
long long int m = 1000000007;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, q, c;
  cin >> n >> q >> c;
  vector<long long int> x(n + 1), y(n + 1), s(n + 1);
  for (long long int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> s[i];
  long long int table[c + 1][101][101];
  memset(table, 0, sizeof(table));
  for (long long int i = 1; i <= n; i++) table[s[i]][x[i]][y[i]]++;
  for (long long int b = 0; b <= c; b++) {
    for (long long int xx = 1; xx <= 100; xx++) {
      for (long long int yy = 1; yy <= 100; yy++)
        table[b][xx][yy] = table[b][xx][yy] + table[b][xx - 1][yy] +
                           table[b][xx][yy - 1] - table[b][xx - 1][yy - 1];
    }
  }
  for (long long int tt = 1; tt <= q; tt++) {
    long long int x1, y1, x2, y2, t;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    long long int ans = 0;
    for (long long int b = 0; b <= c; b++) {
      long long int amount = table[b][x2][y2] - table[b][x1 - 1][y2] -
                             table[b][x2][y1 - 1] + table[b][x1 - 1][y1 - 1];
      long long int bright = (b + t) % (c + 1);
      ans = ans + amount * bright;
    }
    cout << ans << "\n";
  }
  return 0;
}
