#include <bits/stdc++.h>
using pii = std::pair<int, int>;
using LL = long long;
const int MAXN = 5010;
const int INF = 1e9;
int n, m;
std::vector<int> G[MAXN];
std::vector<std::pair<pii, int>> st;
int dis[MAXN][MAXN];
bool check(int maxd, int u, int num, int maxp) {
  std::vector<pii> v;
  for (auto it : st)
    if (dis[u][it.second] <= maxd) v.push_back(it.first);
  LL cost = 0;
  for (auto it : v) {
    if (num > it.second) {
      cost += 1ll * it.second * it.first;
      num -= it.second;
    } else {
      cost += 1ll * num * it.first;
      num = 0;
      break;
    }
  }
  return num == 0 && cost <= 1ll * maxp;
}
int calc(int u, int num, int maxp) {
  int l = 0, r = n, ans = -1;
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (check(m, u, num, maxp))
      r = m - 1, ans = m;
    else
      l = m + 1;
  }
  return ans;
}
void dijkstra(int s) {
  if (dis[s][s] == 0) return;
  std::priority_queue<pii, std::vector<pii>, std::greater<pii>> q;
  q.push({0, s});
  dis[s][s] = 0;
  while (!q.empty()) {
    pii x = q.top();
    q.pop();
    int& u = x.second;
    if (x.first > dis[s][u]) continue;
    for (auto v : G[u]) {
      pii y(x.first + 1, v);
      if (dis[s][v] > y.first) {
        dis[s][v] = y.first;
        q.push(y);
      }
    }
  }
}
void init() {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) dis[i][j] = INF;
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  for (int i = 0, x, y; i < m; ++i) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y), G[y].push_back(x);
  }
  int q;
  scanf("%d", &q);
  for (int x, y, z; q--;) {
    scanf("%d%d%d", &x, &y, &z);
    st.push_back({{z, y}, x});
  }
  std::sort(begin(st), end(st));
  scanf("%d", &q);
  for (int x, y, z; q--;) {
    scanf("%d%d%d", &x, &y, &z);
    dijkstra(x);
    printf("%d\n", calc(x, y, z));
  }
}
