#include <bits/stdc++.h>
using namespace std;
long long int n, m, w, q, ans, g, r, a, mx;
vector<long long int> adj[5005];
vector<pair<long long int, long long int> > city[5005];
long long int level[5005], vis[5005];
vector<long long int> cities[5005];
vector<pair<pair<long long int, long long int>, long long int> > vv;
void bfs(long long int src) {
  queue<long long int> q;
  level[src] = 1;
  long long int i, j, k;
  q.push(src);
  while (!q.empty()) {
    i = q.front();
    q.pop();
    if (vis[i]) continue;
    vis[i] = 1;
    for (j = 0; j < adj[i].size(); j++) {
      k = adj[i][j];
      if (!vis[k]) {
        level[k] = min(level[k], level[i] + 1);
        q.push(k);
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (level[i] <= 5005) {
      mx = max(mx, level[i]);
      cities[level[i]].push_back(i);
    }
  }
}
void createvv(long long int tim) {
  long long int i, j, k, x, y;
  for (i = 1; i <= tim; i++) {
    for (j = 0; j < cities[i].size(); j++) {
      x = cities[i][j];
      for (k = 0; k < city[x].size(); k++) {
        vv.push_back({city[x][k], i});
      }
    }
  }
  sort(vv.begin(), vv.end());
}
bool check(long long int tim) {
  long long int i, j, k, x, y;
  long long int curritem = 0, currcost = 0;
  for (i = 0; i < vv.size(); i++) {
    j = vv[i].first.first;
    k = vv[i].first.second;
    ;
    x = vv[i].second;
    ;
    if (x > tim) continue;
    if (curritem + k < r) {
      curritem += k;
      if (currcost + k * j > a)
        return 0;
      else
        currcost += k * j;
    } else if (curritem + k >= r) {
      long long int req = r - curritem;
      if (currcost + req * j > a)
        return 0;
      else {
        return 1;
      }
    }
  }
  return 0;
}
void solve() {
  long long int i, j, k, lo, hi, mid;
  vv.clear();
  for (i = 1; i <= n; i++) {
    cities[i].clear();
    level[i] = 100000;
    vis[i] = 0;
  }
  mx = 1;
  bfs(g);
  lo = 1;
  hi = mx;
  createvv(mx);
  if (check(mx)) {
    while (lo < hi) {
      mid = lo + (hi - lo) / 2;
      if (check(mid)) {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    cout << hi - 1 << "\n";
  } else
    cout << "-1\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long int i, j, k, x, y, u, v, c, p;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  cin >> w;
  for (i = 1; i <= w; i++) {
    cin >> c >> k >> p;
    city[c].push_back({p, k});
  }
  cin >> q;
  while (q--) {
    cin >> g >> r >> a;
    solve();
  }
  return 0;
}
