#include <bits/stdc++.h>
using namespace std;
int s[102][102][11];
int main() {
  int n, q, c, i, j, k, x, y, _x, _y, t;
  scanf("%d%d%d", &n, &q, &c);
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &t);
    ++s[x][y][t];
  }
  for (k = 0; k <= c; k++) {
    for (i = 1; i <= 100; i++)
      for (j = 1; j <= 100; j++) s[i][j][k] += s[i][j - 1][k];
    for (i = 1; i <= 100; i++)
      for (j = 1; j <= 100; j++) s[i][j][k] += s[i - 1][j][k];
  }
  for (i = 1; i <= q; i++) {
    scanf("%d%d%d%d%d", &t, &x, &y, &_x, &_y);
    int ans = 0;
    for (k = 0; k <= c; k++)
      ans += (k + t) % (c + 1) *
             (s[_x][_y][k] - s[_x][y - 1][k] - s[x - 1][_y][k] +
              s[x - 1][y - 1][k]);
    printf("%d\n", ans);
  }
  return 0;
}
