#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, q, c, su[105][105], pre[15][105][105];
vector<int> ha[105][105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q >> c;
  for (int i = 1; i <= n; i++) {
    int x, y, bri;
    cin >> x >> y >> bri;
    ha[x][y].push_back(bri);
  }
  for (int t = 0; t < c + 1; t++) {
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        int sum = 0;
        for (auto x : ha[i][j]) {
          sum += (x + t) % (c + 1);
        }
        su[i][j] = sum;
      }
    }
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        pre[t][i][j] = pre[t][i - 1][j] + pre[t][i][j - 1] -
                       pre[t][i - 1][j - 1] + su[i][j];
      }
    }
  }
  while (q--) {
    int g, x1, y1, x2, y2;
    cin >> g >> x1 >> y1 >> x2 >> y2;
    g = g % (c + 1);
    int ans = pre[g][x2][y2] - pre[g][x2][y1 - 1] - pre[g][x1 - 1][y2] +
              pre[g][x1 - 1][y1 - 1];
    cout << ans << '\n';
  }
  return 0;
}
