#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000 + 100;
const int INF = 1000000000;
vector<int> g[maxN];
int ind[maxN], cnt[maxN], cost[maxN], n, m, dist[maxN], q[maxN], counter = 0;
void goBFS(int v) {
  for (int i = 1; i <= n; ++i) dist[i] = INF;
  dist[v] = 0;
  int head = 0, tail = 1;
  q[tail] = v;
  while (head < tail) {
    ++head;
    int v = q[head];
    for (int i = 0; i < g[v].size(); ++i) {
      int next = g[v][i];
      if (dist[next] > dist[v] + 1) {
        dist[next] = dist[v] + 1;
        ++tail;
        q[tail] = next;
      }
    }
  }
}
struct node {
  int d;
  int size;
  int cost;
  node() { d = size = cost = 0; }
  node(int _d, int _size, int _cost) { d = _d, size = _size, cost = _cost; }
};
node all[maxN];
bool mycmp(node a, node b) { return (a.cost < b.cost); }
int curInd, curCnt, curCost;
bool f(int val) {
  long long sum = 0, cost = 0;
  for (int i = 1; i <= counter; ++i) {
    if (all[i].d > val) continue;
    long long q = min((long long)all[i].size, curCnt - sum);
    sum += q;
    cost += q * all[i].cost;
  }
  if (sum == curCnt && cost <= curCost) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int store;
  cin >> store;
  for (int i = 1; i <= store; ++i) {
    cin >> ind[i] >> cnt[i] >> cost[i];
  }
  int test;
  cin >> test;
  for (int tst = 1; tst <= test; ++tst) {
    cin >> curInd >> curCnt >> curCost;
    goBFS(curInd);
    counter = 0;
    for (int i = 1; i <= store; ++i) {
      if (dist[ind[i]] != INF) {
        ++counter;
        all[counter] = node(dist[ind[i]], cnt[i], cost[i]);
      }
    }
    sort(all + 1, all + counter + 1, mycmp);
    int le = 0, ri = n;
    while (ri - le > 1) {
      int mi = (ri + le) / 2;
      if (f(mi))
        ri = mi;
      else
        le = mi;
    }
    if (f(le))
      cout << le << endl;
    else if (f(ri))
      cout << ri << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
