#include <bits/stdc++.h>
using namespace std;
const long long INF = 9e18;
const int N = 1e2 + 5;
struct BIT {
  int a[N][N];
  int n;
  void init(int _n) {
    n = _n;
    memset(a, 0, sizeof a);
  }
  void add(int x, int y, int val) {
    for (int i = x; i <= n; i += i & -i) {
      for (int j = y; j <= n; j += j & -j) a[i][j] += val;
    }
  }
  int sum(int x, int y) {
    int ret = 0;
    for (int i = x; i > 0; i -= i & -i) {
      for (int j = y; j > 0; j -= j & -j) ret += a[i][j];
    }
    return ret;
  }
  int sum(int x1, int y1, int x2, int y2) {
    return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) +
           sum(x1 - 1, y1 - 1);
  }
} bit[11];
int main() {
  int n, q, c;
  scanf("%d%d%d", &n, &q, &c);
  for (int i = 0; i <= c; i++) bit[i].init(100);
  for (int i = 0; i < n; i++) {
    int x, y, s;
    scanf("%d%d%d", &x, &y, &s);
    bit[s].add(x, y, 1);
  }
  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) * bit[i].sum(x1, y1, x2, y2);
    }
    printf("%d\n", ans);
  }
  return 0;
}
