#include <bits/stdc++.h>
using namespace std;
long long __[110][110], num[110][110], _[15][110][110];
int n, c;
void add(int x, int yy, long long val) {
  for (; x <= 110; x += x & -x) {
    for (int y = yy; y <= 110; y += y & -y) {
      ++_[val][x][y];
    }
  }
}
long long _____(int x, int yy, int t) {
  long long ret = 0;
  for (; x; x -= x & -x) {
    for (int y = yy; y; y -= y & -y) {
      for (int i = 0; i <= c + 1; ++i) {
        ret += (i + t) % (c + 1) * _[i][x][y];
      }
    }
  }
  return ret;
}
int main() {
  int q, x, y, s, t, x1, x2, y1, y2;
  scanf("%d%d%d", &n, &q, &c);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &x, &y, &s);
    add(x, y, s % (c + 1));
  }
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    long long ans = _____(x2, y2, t);
    ans -= _____(x2, y1 - 1, t);
    ans -= _____(x1 - 1, y2, t);
    ans += _____(x1 - 1, y1 - 1, t);
    printf("%d\n", ans);
  }
  return 0;
}
