#include <bits/stdc++.h>
using namespace std;
inline int two(int n) { return 1 << n; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
const int p = 11;
const int c = 110;
int arr[p][c][c];
int n, mod, q;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q >> mod;
  for (int i = 0; i < n; ++i) {
    int a, b, s;
    cin >> a >> b >> s;
    arr[s][a][b]++;
  }
  for (int i = 0; i <= mod; ++i) {
    for (int j = 1; j <= c; ++j) {
      for (int k = 1; k <= c; ++k) {
        arr[i][j][k] +=
            arr[i][j - 1][k] + arr[i][j][k - 1] - arr[i][j - 1][k - 1];
      }
    }
  }
  for (int i = 0; i < q; ++i) {
    int t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    int ans = 0;
    for (int j = 0; j <= mod; ++j) {
      int bright = (j + t) % (mod + 1);
      int amount = arr[j][x2][y2] - arr[j][x1 - 1][y2] - arr[j][x2][y1 - 1] +
                   arr[j][x1 - 1][y1 - 1];
      ans += (amount * bright);
    }
    cout << ans << '\n';
  }
}
