#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int dist[N][N];
int c[N], k[N], p[N];
int n, m, w, q;
vector<int> adj[N];
void BFS() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) dist[i][j] = N;
    queue<int> q;
    dist[i][i] = 0;
    q.push(i);
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      for (int nex : adj[now])
        if (dist[i][nex] == N) {
          dist[i][nex] = dist[i][now] + 1;
          q.push(nex);
        }
    }
  }
}
int solve(int kota, int need, int budget) {
  priority_queue<pair<int, int>> pq;
  vector<pair<int, pair<int, int>>> v;
  for (int i = 1; i <= w; i++)
    if (dist[kota][c[i]] != N) v.push_back({dist[kota][c[i]], {p[i], k[i]}});
  sort(v.begin(), v.end());
  long long tot = 0;
  for (pair<int, pair<int, int>> y : v) {
    pair<int, pair<int, int>> x = y;
    int pake = 0;
    if (need > 0) {
      long long mins = min(need, x.second.second);
      need -= mins;
      tot += mins * x.second.first;
      x.second.second -= mins;
      pake += mins;
    }
    while (!pq.empty() && pq.top().first > x.second.first &&
           x.second.second > 0) {
      pair<int, int> haha = pq.top();
      pq.pop();
      long long mins = min(haha.second, x.second.second);
      haha.second -= mins;
      x.second.second -= mins;
      pake += mins;
      tot += mins * (x.second.first - haha.first);
      if (haha.second > 0) pq.push(haha);
    }
    if (pake) pq.push({x.second.first, pake});
    if (need == 0 && tot <= budget) return x.first;
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  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];
  BFS();
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int g, r, a;
    cin >> g >> r >> a;
    cout << solve(g, r, a) << "\n";
  }
  return 0;
}
