#include <bits/stdc++.h>
using namespace std;
long long modpow(long long x, long long y, long long m) {
  long long z = 1;
  while (y) {
    if (y & 1) z = (z * x) % m;
    x = (x * x) % m;
    y >>= 1;
  }
  return z;
}
int egcd(int a, int b, int& x, int& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  int x1, y1;
  int gcd = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
int table[11][101][101];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j, k, q, c, n;
  cin >> n >> q >> c;
  vector<int> cnt(n);
  vector<vector<int> > x(n, vector<int>(3));
  for (i = 0; i < n; ++i)
    for (j = 0; j < 3; ++j) cin >> x[i][j];
  for (j = 0; j < n; ++j)
    cnt[j] = x[j][2], table[0][x[j][0]][x[j][1]] += cnt[j];
  for (i = 1; i < 101; ++i) {
    for (j = 1; j < 101; ++j)
      table[0][i][j] +=
          table[0][i - 1][j] + table[0][i][j - 1] - table[0][i - 1][j - 1];
  }
  for (i = 1; i < c + 1; ++i) {
    for (j = 0; j < n; ++j)
      cnt[j] = (cnt[j] + 1) % (c + 1), table[i][x[j][0]][x[j][1]] += cnt[j];
    for (j = 1; j < 101; ++j) {
      for (k = 1; k < 101; ++k)
        table[i][j][k] +=
            table[i][j - 1][k] + table[i][j][k - 1] - table[i][j - 1][k - 1];
    }
  }
  while (q--) {
    int t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    t %= (c + 1);
    cout << table[t][x2][y2] - table[t][x2][y1 - 1] - table[t][x1 - 1][y2] +
                table[t][x1 - 1][y1 - 1]
         << "\n";
  }
  return 0;
}
