#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, m, w;
int dis[N][N];
vector<int> v[N];
vector<pair<int, pair<int, int> > > a;
bool cmp(pair<int, pair<int, int> > i, pair<int, pair<int, int> > j) {
  return i.second.second < j.second.second;
}
bool ok(int c, int kk, int p, int mid) {
  for (int i = 0; i < w; i++)
    if (dis[c][a[i].first] <= mid) {
      int num = a[i].second.first;
      int prc = a[i].second.second;
      int h = min(num, min(kk, p / prc));
      kk -= h;
      p -= h * prc;
    }
  return kk == 0;
}
int solve(int c, int k, int p) {
  if (!ok(c, k, p, n)) return -1;
  int low = 0, high = n, mid;
  while (low < high) {
    mid = low + high >> 1;
    if (ok(c, k, p, mid))
      high = mid;
    else
      low = mid + 1;
  }
  return high;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) dis[i][j] = 1 << 20;
  while (m--) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  queue<int> Q;
  for (int i = 1; i <= n; i++) {
    dis[i][i] = 0;
    Q.push(i);
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (auto j : v[x])
        if (dis[i][x] + 1 < dis[i][j]) {
          dis[i][j] = 1 + dis[i][x];
          Q.push(j);
        }
    }
  }
  int c, k, p;
  scanf("%d", &w);
  for (int i = 0; i < w; i++) {
    scanf("%d %d %d", &c, &k, &p);
    a.push_back({c, {k, p}});
  }
  sort(a.begin(), a.end(), cmp);
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d %d", &c, &k, &p);
    printf("%d\n", solve(c, k, p));
  }
  return 0;
}
