#include <bits/stdc++.h>
using namespace std;
long long cnt[110][110][11];
int main() {
  long long n, q, c;
  scanf("%lld %lld %lld", &n, &q, &c);
  ++c;
  long long x, y, s;
  for (long long i = 0; i < n; i++) {
    scanf("%lld %lld %lld", &x, &y, &s);
    cnt[x][y][s] += 1;
  }
  long long t, x1, y1, x2, y2;
  for (long long i = 1; i <= 100; i++) {
    for (long long k = 0; k < c; k++) {
      for (long long j = 1; j <= 100; j++) {
        cnt[i][j][k] += cnt[i][j - 1][k];
      }
    }
  }
  for (long long i = 0; i < q; i++) {
    scanf("%lld %lld %lld %lld %lld", &t, &x1, &y1, &x2, &y2);
    if (x1 > x2) {
      swap(x1, x2);
    }
    if (y1 > y2) {
      swap(y1, y2);
    }
    long long tot = 0;
    for (long long i = x1; i <= x2; i++) {
      for (long long j = 0; j < c; j++) {
        tot += (cnt[i][y2][j] - cnt[i][y1 - 1][j]) * ((j + t) % c);
      }
    }
    printf("%lld\n", tot);
  }
  return 0;
}
