#include <bits/stdc++.h>
using namespace std;
const long long N = 5e3 + 100;
long long n, m, w;
long long K[N], P[N], tempK[N];
vector<long long> G[N], Store[N];
long long BFS(long long need, long long source, long long budget) {
  multiset<pair<long long, long long> > taken;
  for (long long i = 1; i <= w; i++) tempK[i] = K[i];
  queue<pair<long long, long long> > q;
  q.push({source, 0});
  vector<bool> vis(n + 1, false);
  while (!q.empty()) {
    long long u = q.front().first;
    long long d = q.front().second;
    q.pop();
    if (vis[u]) continue;
    vis[u] = true;
    for (long long s : Store[u]) {
      long long cnt = min({budget / P[s], tempK[s], need});
      need -= cnt;
      tempK[s] -= cnt;
      budget -= cnt * P[s];
      if (cnt) taken.insert({P[s], cnt});
      while (taken.size() && need > 0 && tempK[s] > 0 && budget < P[s] &&
             P[s] < taken.rbegin()->first) {
        auto back = *taken.rbegin();
        taken.erase(--taken.end());
        budget += back.first * back.second;
        need += back.second;
        cnt = min({tempK[s], need, budget / P[s]});
        budget -= cnt * P[s];
        need -= cnt;
        tempK[s] -= cnt;
        if (cnt) taken.insert({P[s], cnt});
        cnt = min({need, back.second, budget / back.first});
        if (cnt) {
          need -= cnt;
          budget -= back.first * cnt;
          taken.insert({back.first, cnt});
        }
      }
    }
    if (need == 0) return d;
    for (long long v : G[u])
      if (!vis[v]) q.push({v, d + 1});
  }
  if (need > 0) return -1;
}
int32_t main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 0; i < m; i++) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  scanf("%lld", &w);
  for (long long i = 1; i <= w; i++) {
    long long c;
    scanf("%lld%lld%lld", &c, &K[i], &P[i]);
    Store[c].push_back(i);
  }
  for (long long i = 1; i <= n; i++)
    sort(Store[i].begin(), Store[i].end(),
         [&](long long u, long long v) { return P[u] < P[v]; });
  long long q;
  scanf("%lld", &q);
  while (q--) {
    long long g, r, a;
    scanf("%lld%lld%lld", &g, &r, &a);
    printf("%lld\n", BFS(r, g, a));
  }
  return 0;
}
