#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, c;
  int x, y, s;
  int t, x1, y1, x2, y2;
  long long sum = 0;
  int list[101][101][11];
  memset(list, 0, sizeof(list));
  cin >> n >> q >> c;
  while (n--) {
    cin >> x >> y >> s;
    list[x][y][s]++;
  }
  for (int i = 1; i < 101; i++)
    for (int j = 1; j < 101; j++) {
      for (int k = 0; k < 11; k++)
        list[i][j][k] +=
            list[i - 1][j][k] + list[i][j - 1][k] - list[i - 1][j - 1][k];
    }
  for (int i = 0; i < q; i++) {
    sum = 0;
    cin >> t >> x1 >> y1 >> x2 >> y2;
    for (int j = 0; j < 11; j++) {
      sum += (list[x2][y2][j] - list[x1 - 1][y2][j] - list[x2][y1 - 1][j] +
              list[x1 - 1][y1 - 1][j]) *
             ((j + t) % (c + 1));
    }
    cout << sum << endl;
  }
  return 0;
}
