#include <bits/stdc++.h>
using namespace std;
const long long maxn = 50 + 10, mod = 1e9 + 7, inf = 2e9;
long long psum[maxn][maxn * 4][maxn * 4], ans;
long long n, q, c;
int main() {
  cin >> n >> q >> c;
  for (int i = 0; i < n; i++) {
    long long x, y, s;
    cin >> x >> y >> s;
    for (int j = 0; j <= c; j++) {
      psum[j][x][y] += (s + j) % (c + 1);
    }
  }
  for (int i = 1; i <= 2 * maxn + 1; i++) {
    for (int k = 0; k <= c; k++) {
      for (int j = 1; j <= 2 * maxn + 1; j++) {
        psum[k][i][j] += psum[k][i][j - 1];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    ans = 0;
    long long t, x1, y1, x2, y2;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    t %= (c + 1);
    for (int j = x1; j <= x2; j++) ans += psum[t][j][y2] - psum[t][j][y1 - 1];
    cout << ans << endl;
  }
}
