#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v) is >> t.first >> t.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) is >> t;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\n';
  return os;
}
double pi = acos(-1.0);
const long long md = 1e9 + 7;
const long long INF = 1e15;
long long dx1[] = {0, 0, -1, 1};
long long dy1[] = {1, -1, 0, 0};
template <class T>
T abst(T a) {
  return a < 0 ? -a : a;
}
template <class T>
T max2(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T min2(T a, T b) {
  return a < b ? a : b;
}
long long pre[101][101][11];
long long arr[101][101][11];
void solve() {
  long long n, q, c;
  cin >> n >> q >> c;
  for (long long i = 0; i < n; i++) {
    long long x, y, s;
    cin >> x >> y >> s;
    x--;
    y--;
    arr[x][y][s]++;
  }
  for (long long i = 0; i <= 100; i++) {
    for (long long j = 0; j <= 100; j++) {
      for (long long k = 0; k <= c; k++) pre[i][j][k] = arr[i][j][k];
    }
  }
  for (long long j = 0; j <= 100; j++) {
    for (long long i = 1; i <= 100; i++) {
      for (long long k = 0; k <= c; k++) pre[i][j][k] += pre[i - 1][j][k];
    }
  }
  for (long long i = 0; i <= 100; i++) {
    for (long long j = 1; j <= 100; j++) {
      for (long long k = 0; k <= c; k++) pre[i][j][k] += pre[i][j - 1][k];
    }
  }
  while (q--) {
    long long t, ulx, uly, lrx, lry;
    cin >> t >> ulx >> uly >> lrx >> lry;
    ulx--;
    uly--;
    lrx--;
    lry--;
    long long ans1[11] = {0}, ans2[11] = {0}, ans3[11] = {0}, ans4[11] = {0};
    memcpy(ans4, pre[lrx][lry], sizeof pre[lrx][lry]);
    if (ulx - 1 >= 0 and uly - 1 >= 0)
      memcpy(ans1, pre[ulx - 1][uly - 1], sizeof pre[ulx - 1][uly - 1]);
    if (ulx - 1 >= 0) memcpy(ans2, pre[ulx - 1][lry], sizeof pre[ulx - 1][lry]);
    if (uly - 1 >= 0) memcpy(ans3, pre[lrx][uly - 1], sizeof pre[lrx][uly - 1]);
    long long ans = 0;
    for (long long i = 0; i <= c; i++) {
      ans += (ans4[i] - ans2[i] - ans3[i] + ans1[i]) * ((i + t) % (c + 1));
    }
    cout << ans << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
