#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double PI = 4 * atan((long double)1);
long long power(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n % 2) res *= a;
    a *= a;
    n /= 2;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n, q, c;
  cin >> n >> q >> c;
  vector<pair<long long, long long> > vx[101];
  vector<long long> vx1[101];
  for (int i = (0); i < (n); ++i) {
    long long int x, y, c1;
    cin >> x >> y >> c1;
    vx[x].push_back({y, c1});
  }
  for (int i = (0); i < (101); ++i) {
    sort(vx[i].begin(), vx[i].end());
    if (vx[i].size() != 0)
      for (int j = (0); j < (vx[i].size()); ++j)
        vx1[i].push_back(vx[i][j].first);
  }
  vector<long long> dp[c + 1][101];
  for (int k = (0); k < (c + 1); ++k) {
    for (int i = (0); i < (101); ++i) {
      dp[k][i].push_back(0);
      if (vx[i].size() != 0)
        for (int j = (0); j < (vx[i].size()); ++j)
          dp[k][i].push_back(dp[k][i][j] + (vx[i][j].second + k) % (c + 1));
    }
  }
  for (int i = (0); i < (q); ++i) {
    long long int c2, x1, y1, x2, y2, ans = 0;
    cin >> c2 >> x1 >> y1 >> x2 >> y2;
    c2 = c2 % (c + 1);
    for (int i = (x1); i < (x2 + 1); ++i) {
      long long int ind1 =
          lower_bound(vx1[i].begin(), vx1[i].end(), y1) - vx1[i].begin();
      long long int ind2 =
          upper_bound(vx1[i].begin(), vx1[i].end(), y2) - vx1[i].begin();
      ans += (dp[c2][i][ind2] - dp[c2][i][ind1]);
    }
    cout << ans;
    cout << "\n";
  }
  return 0;
}
