#include <bits/stdc++.h>
using namespace std;
const long long maxn = 105;
long long n, q, c;
long long a[12][maxn][maxn];
long long pref[12][maxn][maxn];
void precalc() {
  for (long long i = 1; i < maxn; i++) {
    for (long long j = 1; j < maxn; j++) {
      for (long long x = 0; x <= c; ++x) {
        pref[x][i][j] = pref[x][i - 1][j] + pref[x][i][j - 1] -
                        pref[x][i - 1][j - 1] + a[x][i][j];
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q >> c;
  for (long long i = 0; i < n; ++i) {
    long long x, y, z;
    cin >> x >> y >> z;
    a[z][x][y]++;
  }
  precalc();
  for (long long i = 0; i < q; ++i) {
    long long x = 0;
    long long t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    long long sm = 0;
    for (long long j = 0; j <= c; ++j) {
      long long res = pref[j][x2][y2] - pref[j][x1 - 1][y2] -
                      pref[j][x2][y1 - 1] + pref[j][x1 - 1][y1 - 1];
      sm += ((j + t) % (c + 1)) * res;
    }
    cout << sm << "\n";
  }
}
