#include <bits/stdc++.h>
using namespace std;
int cs[102][102][11];
int main() {
  int n, q, c;
  scanf("%d%d%d", &n, &q, &c);
  int x, y, s;
  c++;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x, &y, &s);
    cs[x][y][s]++;
  }
  for (int s = 0; s < 11; s++) {
    for (int i = 1; i <= 100; i++)
      for (int j = 1; j <= 100; j++) {
        cs[i][j][s] += cs[i - 1][j][s] - cs[i - 1][j - 1][s] + cs[i][j - 1][s];
      }
  }
  int t, x1, y1, x2, y2;
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
    long long ans = 0, tmp = 0;
    for (int s = 0; s < 11; s++) {
      tmp = cs[x2][y2][s] + cs[x1 - 1][y1 - 1][s] - cs[x1 - 1][y2][s] -
            cs[x2][y1 - 1][s];
      ans += tmp * ((t + s) % c);
      tmp = 0;
    }
    cout << ans << endl;
  }
}
