#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using std::cin;
using std::cout;
void print() {}
template <typename T, typename... Args>
void print(T t, Args... args) {
  cout << t << ' ', print(args...);
}
template <typename... Args>
void println(Args... args) {
  print(args...), cout << '\n';
}
void printF(const char *format) { cout << format; }
template <typename T, typename... Args>
void printF(const char *format, T t, Args... args) {
  while (*format != '%' && *format) cout.put(*format++);
  if (*format++ == '\0') return;
  cout << t;
  printF(format, args...);
}
ll getTotalBright(const std::vector<ll> &v, ll time, ll C) {
  ll ret = 0;
  for (ll i = 0; i < C + 1; ++i) {
    ret += v[i] * ((time + i) % (C + 1));
  }
  return ret;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int N, Q, C, x1, y1, x2, y2, s;
  int time;
  cin >> N >> Q >> C;
  std::array<std::array<std::vector<ll>, 101>, 101> grid;
  for (auto &row : grid)
    for (auto &col : row) col.resize(C + 1, 0);
  for (int i = 0; i < N; ++i) {
    cin >> x1 >> y1 >> s;
    grid[x1][y1][s]++;
  }
  for (int i = 1; i < 101; ++i) {
    for (int h = 0; h <= C; ++h) {
      grid[i][0][h] += grid[i - 1][0][h];
      grid[0][i][h] += grid[0][i - 1][h];
    }
  }
  for (int i = 1; i < 101; ++i) {
    for (int j = 1; j < 101; ++j) {
      for (int h = 0; h <= C; ++h) {
        grid[i][j][h] +=
            grid[i - 1][j][h] + grid[i][j - 1][h] - grid[i - 1][j - 1][h];
      }
    }
  }
  for (int q = 0; q < Q; ++q) {
    cin >> time >> x1 >> y1 >> x2 >> y2;
    ll bright = 0;
    bright += getTotalBright(grid[x2][y2], time, C);
    bright -= getTotalBright(grid[x1 - 1][y2], time, C);
    bright -= getTotalBright(grid[x2][y1 - 1], time, C);
    bright += getTotalBright(grid[x1 - 1][y1 - 1], time, C);
    println(bright);
  }
  return 0;
}
