#include <bits/stdc++.h>
using namespace std;
const int MAXX = 110;
int n, q, c, mtr[MAXX][MAXX][13], precalc[MAXX][MAXX][13];
int main() {
  scanf("%d %d %d", &n, &q, &c);
  for (int i = 1, x, y, s; i <= n; ++i) {
    scanf("%d %d %d", &x, &y, &s);
    mtr[x][y][s]++;
  }
  for (int i = 0, j, k; i <= c; ++i)
    for (j = 1; j <= 100; ++j)
      for (k = 1; k <= 100; ++k)
        precalc[j][k][i] = precalc[j][k - 1][i] + precalc[j - 1][k][i] -
                           precalc[j - 1][k - 1][i] + mtr[j][k][i];
  while (q--) {
    int t, x1, y1, x2, y2, ans = 0;
    scanf("%d %d %d %d %d", &t, &x1, &y1, &x2, &y2);
    t %= (c + 1);
    for (int i = 0; i <= c; ++i)
      ans += (precalc[x2][y2][i] - precalc[x2][y1 - 1][i] -
              precalc[x1 - 1][y2][i] + precalc[x1 - 1][y1 - 1][i]) *
             ((i + t) % (c + 1));
    printf("%d\n", ans);
  }
}
