#include <bits/stdc++.h>
using namespace std;
int e[105][105][15];
int bit(int n) { return n & (-n); }
int sum(int x, int y, int s) {
  int ans = 0;
  for (int i = x; i > 0; i -= bit(i))
    for (int j = y; j > 0; j -= bit(j)) {
      ans += e[i][j][s];
    }
  return ans;
}
void up(int x, int y, int k, int s) {
  for (int i = x; i < 105; i += bit(i))
    for (int j = y; j < 105; j += bit(j)) {
      e[i][j][s] += k;
    }
}
int main() {
  int n, q, c;
  cin >> n >> q >> c;
  while (n--) {
    int x, y, s;
    scanf("%d%d%d", &x, &y, &s);
    up(x, y, 1, s);
  }
  while (q--) {
    int t, x1, y1, x2, y2;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    int ans = 0;
    for (int i = 0; i <= c; i++) {
      ans += (i + t) % (c + 1) *
             (sum(x2, y2, i) - sum(x1 - 1, y2, i) - sum(x2, y1 - 1, i) +
              sum(x1 - 1, y1 - 1, i));
    }
    printf("%d\n", ans);
  }
}
