#include <bits/stdc++.h>
using namespace std;
const long long N = 800;
const long long M = 100000;
const long long md = (1e16);
const long long mx = (1e18);
const long long mn = (1e16);
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first < b.first || a.first == b.first && a.second > b.second;
}
long long n, m, k, l, w, s, j, i, f[12][200][200], sum[12][200][200], x1, yy1,
    x2, y2, t, summ, p1, p2, p3, q, c, res;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q >> c;
  for (i = 1; i <= n; i++) {
    cin >> p1 >> p2 >> p3;
    f[p3][p1][p2]++;
  }
  for (l = 0; l <= 10; l++) {
    for (i = 1; i <= 100; i++) {
      for (j = 1; j <= 100; j++)
        sum[l][i][j] = sum[l][i - 1][j] + sum[l][i][j - 1] -
                       sum[l][i - 1][j - 1] + f[l][i][j];
    }
  }
  for (l = 1; l <= q; l++) {
    cin >> t >> x1 >> yy1 >> x2 >> y2;
    res = 0;
    for (i = 0; i <= c; i++) {
      summ = sum[i][x2][y2] - sum[i][x2][yy1 - 1] - sum[i][x1 - 1][y2] +
             sum[i][x1 - 1][yy1 - 1];
      res += summ * ((i + t) % (c + 1));
    }
    cout << res << endl;
  }
}
