#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 5;
queue<int> Queue;
int n, m, w, q, id[maxn], c[maxn], a[maxn];
int k[maxn], p[maxn], g[maxn], r[maxn];
int d[maxn][maxn];
bool visited[maxn];
vector<int> Adj[maxn];
void Input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    Adj[u].push_back(v);
    Adj[v].push_back(u);
  }
  cin >> w;
  for (int i = 1; i <= w; i++) {
    cin >> c[i] >> k[i] >> p[i];
    id[i] = i;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) cin >> g[i] >> r[i] >> a[i];
}
void BFS(int st) {
  fill(visited + 1, visited + 1 + n, false);
  fill(d[st] + 1, d[st] + 1 + n, n + 1);
  Queue.push(st);
  d[st][st] = 0;
  visited[st] = true;
  while (!Queue.empty()) {
    int u = Queue.front();
    Queue.pop();
    for (int v : Adj[u])
      if (!visited[v]) {
        d[st][v] = d[st][u] + 1;
        visited[v] = true;
        Queue.push(v);
      }
  }
}
bool Check(int s, int x) {
  fill(visited + 1, visited + 1 + w, false);
  for (int i = 1; i <= w; i++)
    if (d[g[s]][c[i]] <= x) visited[i] = true;
  int valz = a[s], cnt = 0;
  for (int i = 1; i <= w; i++)
    if (visited[id[i]]) {
      int x = min(valz / p[id[i]], k[id[i]]);
      cnt += x;
      valz -= x * p[id[i]];
    }
  return cnt >= r[s];
}
void FindRes(int s) {
  int low = 0, high = n, res = n + 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (Check(s, mid)) {
      res = min(res, mid);
      high = mid - 1;
    } else
      low = mid + 1;
  }
  if (res == n + 1)
    cout << -1 << '\n';
  else
    cout << res << '\n';
}
void Solve() {
  for (int i = 1; i <= n; i++) BFS(i);
  sort(id + 1, id + 1 + w, [](int x, int y) {
    if (p[x] != p[y])
      return p[x] < p[y];
    else
      return k[x] < k[y];
  });
  for (int i = 1; i <= q; i++) FindRes(i);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  Input();
  Solve();
}
