#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
vector<int> adj[N];
int dist[N][N];
void dijkstar(int s, int n) {
  int i, id, d, u, v, j;
  for (i = 1; i <= n; ++i) dist[s][i] = n + 5;
  dist[s][s] = 0;
  queue<int> pq;
  pq.push(s);
  while (!pq.empty()) {
    u = pq.front();
    pq.pop();
    d = dist[s][u];
    for (i = 0; i < adj[u].size(); ++i) {
      v = adj[u][i];
      if (dist[s][v] > d + 1) {
        dist[s][v] = d + 1;
        pq.push(v);
      }
    }
  }
}
pair<int, int> shop[N];
int c[N], k[N], p[N];
bool istrue(int TIME, int a, int r, int g, int n, int pos) {
  int u, v, i, id;
  long long ret = 0, temp, need;
  for (i = 0; i < pos; ++i) {
    id = shop[i].second;
    if (dist[c[id]][g] <= TIME) {
      need = min(r, k[id]);
      r -= need;
      ret += need * p[id];
    }
  }
  if (ret <= a && r == 0) return 1;
  return 0;
}
void solve() {
  int n, m, w, i, x, y;
  cin >> n >> m;
  for (i = 0; i < m; ++i)
    scanf("%d%d", &x, &y), adj[x].push_back(y), adj[y].push_back(x);
  for (i = 1; i <= n; ++i) dijkstar(i, n);
  cin >> w;
  for (i = 0; i < w; ++i) {
    scanf("%d%d%d", &c[i], &k[i], &p[i]);
    shop[i] = make_pair(p[i], i);
  }
  sort(shop, shop + w);
  int l = 0, r = n, mid, q, g, a;
  cin >> q;
  int R;
  while (q--) {
    scanf("%d%d%d", &g, &R, &a);
    if (!istrue(n, a, R, g, n, w)) {
      printf("-1\n");
      continue;
    }
    l = 0, r = n;
    while (l < r) {
      mid = (l + r) / 2;
      if (istrue(mid, a, R, g, n, w))
        r = mid;
      else {
        l = mid + 1;
      }
    }
    printf("%d\n", l);
  }
}
int main() { solve(); }
