#include <bits/stdc++.h>
using namespace std;
class CStarSky {
  int N, Q, C;
  vector<vector<vector<int>>> S;

 public:
  void input(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in >> N >> Q >> C;
    S = vector<vector<vector<int>>>(
        C + 1, vector<vector<int>>(110, vector<int>(110, 0)));
    for (int(i) = (0); (i) < (N); ++(i)) {
      int x, y, b;
      in >> x >> y >> b;
      for (int(j) = (0); (j) < (C + 1); ++(j))
        S[j][x + 1][y + 1] += (b + j) % (C + 1);
    }
  }
  void query(std::istream& in, std::ostream& out) {
    int t, x1, y1, x2, y2;
    in >> t >> x1 >> y1 >> x2 >> y2;
    int res = S[t % (C + 1)][x2 + 1][y2 + 1] - S[t % (C + 1)][x2 + 1][y1] -
              S[t % (C + 1)][x1][y2 + 1] + S[t % (C + 1)][x1][y1];
    out << res << '\n';
  }
  void solve(std::istream& in, std::ostream& out) {
    input(in, out);
    for (int(i) = (0); (i) < (C + 1); ++(i)) {
      for (int(j) = (1); (j) < (101 + 1); ++(j)) {
        for (int(k) = (1); (k) < (100 + 1); ++(k)) S[i][j][k + 1] += S[i][j][k];
        for (int(k) = (1); (k) < (101 + 1); ++(k)) S[i][j][k] += S[i][j - 1][k];
      }
    }
    for (int(i) = (0); (i) < (Q); ++(i)) {
      query(in, out);
    }
  }
};
int main() {
  CStarSky solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
