#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long C = 101;
const long long P = 11;
long long cnt[P][C][C];
int32_t main() {
  long long n, q, c, t, x, y, s, x1, x2, y1, y2;
  cin >> n >> q >> c;
  memset(cnt, 0, sizeof(cnt));
  for (long long i = 0; i < n; i++) {
    long long x, y, s;
    cin >> x >> y >> s;
    cnt[s][x][y] += 1;
  }
  for (long long p = 0; p <= c; p++) {
    for (long long i = 1; i < C; i++) {
      for (long long j = 1; j < C; j++) {
        cnt[p][i][j] +=
            cnt[p][i - 1][j] + cnt[p][i][j - 1] - cnt[p][i - 1][j - 1];
      }
    }
  }
  for (long long i = 1; i <= q; ++i) {
    cin >> t >> x1 >> y1 >> x2 >> y2;
    long long ans = 0;
    for (long long p = 0; p <= c; p++) {
      long long brightness = (p + t) % (c + 1);
      long long amount = cnt[p][x2][y2] - cnt[p][x1 - 1][y2] -
                         cnt[p][x2][y1 - 1] + cnt[p][x1 - 1][y1 - 1];
      ans += brightness * amount;
    }
    cout << ans << '\n';
  }
}
