#include <bits/stdc++.h>
using namespace std;
const int P = 11;
const int L = 101;
const int M = 101;
int n, q, c;
int cnt[P][L][M];
int main() {
  scanf("%d%d%d", &n, &q, &c);
  for (int j = 0; j < 11; j++) {
    for (int i = 0; i < 101; i++) {
      for (int k = 0; k < 101; k++) {
        cnt[j][i][k] = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int x, y, s;
    scanf("%d%d%d", &x, &y, &s);
    cnt[s][x][y] = cnt[s][x][y] + 1;
  }
  for (int j = 0; j < 11; j++) {
    for (int i = 1; i < 101; i++) {
      for (int k = 1; k < 101; k++) {
        if (i == 1 and k > 1) {
          cnt[j][i][k] = cnt[j][i][k - 1] + cnt[j][i][k];
        }
        if (k == 1 and i > 1) {
          cnt[j][i][k] = cnt[j][i - 1][k] + cnt[j][i][k];
        }
        if (i > 1 and k > 1) {
          cnt[j][i][k] = cnt[j][i - 1][k] + cnt[j][i][k - 1] -
                         cnt[j][i - 1][k - 1] + cnt[j][i][k];
        }
      }
    }
  }
  for (int z = 0; z < q; z++) {
    int t, x1, y1, x2, y2;
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    int amount[11];
    for (int j = 0; j < 11; j++) {
      if (x1 > 1 and y1 > 1) {
        amount[j] = cnt[j][x2][y2] - cnt[j][x1 - 1][y2] - cnt[j][x2][y1 - 1] +
                    cnt[j][x1 - 1][y1 - 1];
      }
      if (x1 == 1 and y1 == 1) {
        amount[j] = cnt[j][x2][y2];
      }
      if (x1 > 1 and y1 == 1) {
        amount[j] = cnt[j][x2][y2] - cnt[j][x1 - 1][y2];
      }
      if (x1 == 1 and y1 > 1) {
        amount[j] = cnt[j][x2][y2] - cnt[j][x2][y1 - 1];
      }
    }
    int sum = 0;
    for (int j = 0; j < 11; j++) {
      int temp = (j + t) % (c + 1);
      sum = sum + temp * amount[j];
    }
    printf("%d\n", sum);
  }
  return 0;
}
