#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
const int inf = 1e9;
int n, q, c;
int mp[105][105][12];
int dp[105][105][12];
long long query(int t, int x1, int y1, int x2, int y2) {
  long long ans = 0;
  for (int k = 0; k < 11; k++) {
    ans = ans + ((dp[x2][y2][k] - dp[x2][y1 - 1][k] - dp[x1 - 1][y2][k] +
                  dp[x1 - 1][y1 - 1][k]) *
                 ((k + t) % (c + 1)));
  }
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &q, &c);
  memset(mp, 0, sizeof(mp));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    int tx, ty, ts;
    scanf("%d%d%d", &tx, &ty, &ts);
    mp[tx][ty][ts]++;
  }
  for (int i = 1; i <= 102; i++) {
    for (int j = 1; j <= 102; j++) {
      for (int k = 0; k <= 10; k++) {
        dp[i][j][k] = dp[i - 1][j][k] + dp[i][j - 1][k] + mp[i][j][k] -
                      dp[i - 1][j - 1][k];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int tt, tx, ty, tx1, ty1;
    scanf("%d%d%d%d%d", &tt, &tx, &ty, &tx1, &ty1);
    cout << query(tt, tx, ty, tx1, ty1) << endl;
  }
  return 0;
}
