#include<bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MASK(i) (1LL<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define div ___div
#define next ___next
#define prev ___prev
#define left ___left
#define right ___right
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<class X,class Y>
void minimize(X &x,const Y &y) {
if (x>y) x=y;
}
template<class X,class Y>
void maximize(X &x,const Y &y) {
if (x<y) x=y;
}
template<class T>
T Abs(const T &x) {
return (x<0?-x:x);
}
/* Author: Van Hanh Pham - skyvn97 */
/** END OF TEMPLATE - ACTUAL SOLUTION COMES HERE **/
#define MAX_SIZE 125125
#define MAX_QUERY 250250
const int MAX_NEED = (int)1e9 + 7;
const long long MAX_BUDGET = (long long)1e18 + 7LL;
class SegmentTree {
private:
int cnt[MASK(18) + 7];
long long sum[MASK(18) + 7];
int n;
public:
SegmentTree(int _n = 0) {
n = _n;
memset(cnt, 0, sizeof cnt);
memset(sum, 0, sizeof sum);
}
void reset(void) {
memset(cnt, 0, sizeof cnt);
memset(sum, 0, sizeof sum);
}
void add(int pos, int capa, int cost) {
int i = 1, l = 1, r = n;
while (true) {
cnt[i] += capa;
sum[i] += 1LL * capa * cost;
minimize(cnt[i], MAX_NEED);
minimize(sum[i], MAX_BUDGET);
if (l == r) return;
int m = (l + r) >> 1;
if (pos > m) {
i = 2 * i + 1; l = m + 1;
} else {
i = 2 * i; r = m;
}
}
}
bool haveEnough(int need, long long budget) const {
if (cnt[1] < need) return false;
int i = 1, l = 1, r = n;
while (true) {
if (l == r) return sum[i] / cnt[i] * need <= budget;
int m = (l + r) >> 1;
if (cnt[2 * i] == need) return sum[2 * i] <= budget;
if (cnt[2 * i] > need) {
i = 2 * i; r = m;
} else {
budget -= sum[2 * i];
if (budget < 0) return false;
need -= cnt[2 * i];
i = 2 * i + 1; l = m + 1;
}
}
}
};
struct Store {
int place, have, price;
Store() {
place = have = price = 0;
}
void input(void) {
scanf("%d%d%d", &place, &have, &price);
}
bool operator < (const Store &s) const {
return price < s.price;
}
};
struct Query {
int need, low, high;
long long budget;
Query(int _need = 0, long long _budget = 0) {
need = _need; budget = _budget; low = high = 0;
}
};
#define MAX_QUERY_NODE 15
vector<int> adj[MAX_SIZE];
int dist[MAX_QUERY_NODE + 3][MAX_SIZE];
pair<int, int> storesByDist[MAX_SIZE];
Store stores[MAX_SIZE];
int numNode, numEdge, numStore, numQuery;
SegmentTree myit;
Query queries[MAX_QUERY];
vector<int> queryID[MAX_SIZE];
vector<pair<int, int>> asks;
void init(void) {
scanf("%d%d", &numNode, &numEdge);
REP(love, numEdge) {
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
scanf("%d", &numStore);
FOR(i, 1, numStore) stores[i].input();
sort(stores + 1, stores + numStore + 1);
scanf("%d", &numQuery);
FOR(i, 1, numQuery) {
int place, need; long long budget;
scanf("%d%d%lld", &place, &need, &budget);
queries[i] = Query(need, budget);
queryID[place].push_back(i);
}
}
void bfs(int s, int dist[]) {
queue<int> q;
dist[s] = 0; q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
FORE(it, adj[u]) if (dist[*it] > numNode) {
dist[*it] = dist[u] + 1;
q.push(*it);
}
}
}
void prepare(void) {
memset(dist, 0x3f, sizeof dist);
FOR(i, 1, MAX_QUERY_NODE) bfs(i, dist[i]);
myit = SegmentTree(numStore);
}
void answerQuery(int place) {
if (queryID[place].empty()) return;
FOR(i, 1, numStore) storesByDist[i] = make_pair(dist[place][stores[i].place], i);
sort(storesByDist + 1, storesByDist + numStore + 1);
FORE(it, queryID[place]) {
queries[*it].low = 0;
queries[*it].high = numNode;
}
while (true) {
asks.clear();
FORE(it, queryID[place]) {
int low = queries[*it].low, high = queries[*it].high;
if (low == high) continue;
asks.push_back(make_pair(high - low == 1 ? low : (low + high) >> 1, *it));
// if (*it == 4) printf("CHECK POINT %d\n", high - low == 1 ? low : (low + high) >> 1);
}
if (asks.empty()) break;
myit.reset();
sort(ALL(asks));
int j = 1;
FORE(it, asks) {
while (j <= numStore) {
int k = storesByDist[j].se;
if (dist[place][stores[k].place] > it->fi) break;
myit.add(k, stores[k].have, stores[k].price);
// printf("ADD pos %d, capa %d, price %d\n", k, stores[k].have, stores[k].price);
j++;
}
// if (it->se == 4) printf("Checking need %d, budget %lld\n", queries[it->se].need, queries[it->se].budget);
if (myit.haveEnough(queries[it->se].need, queries[it->se].budget)) queries[it->se].high = it->fi;
else queries[it->se].low = it->fi + 1;
}
}
}
void process(void) {
FOR(i, 1, MAX_QUERY_NODE) answerQuery(i);
FOR(i, 1, numQuery) printf("%d ", queries[i].low < numNode ? queries[i].low : -1);
printf("\n");
}
int main(void) {
#ifdef ONLINE_JUDGE
freopen("shipping.inp", "r", stdin);
freopen("shipping.out", "w", stdout);
#endif // ONLINE_JUDGE
init();
prepare();
process();
return 0;
}
/*** LOOK AT MY CODE. MY CODE IS AMAZING :D ***/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBGT1IoaSxhLGIpIGZvciAoaW50IGk9KGEpLF9iPShiKTtpPD1fYjtpPWkrMSkKI2RlZmluZSBGT1JEKGksYixhKSBmb3IgKGludCBpPShiKSxfYT0oYSk7aT49X2E7aT1pLTEpCiNkZWZpbmUgUkVQKGksbikgZm9yIChpbnQgaT0wLF9uPShuKTtpPF9uO2k9aSsxKQojZGVmaW5lIEZPUkUoaSx2KSBmb3IgKF9fdHlwZW9mKCh2KS5iZWdpbigpKSBpPSh2KS5iZWdpbigpO2khPSh2KS5lbmQoKTtpKyspCiNkZWZpbmUgQUxMKHYpICh2KS5iZWdpbigpLCh2KS5lbmQoKQojZGVmaW5lIGZpICAgZmlyc3QKI2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Bsb25nIHN1bVtNQVNLKDE4KSArIDddOwogICAgaW50IG47CgogICAgcHVibGljOgogICAgU2VnbWVudFRyZWUoaW50IF9uID0gMCkgewogICAgICAgIG4gPSBfbjsKICAgICAgICBtZW1zZXQoY250LCAwLCBzaXplb2YgY250KTsKICAgICAgICBtZW1zZXQoc3VtLCAwLCBzaXplb2Ygc3VtKTsKICAgIH0KCiAgICB2b2lkIHJlc2V0KHZvaWQpIHsKICAgICAgICBtZW1zZXQoY250LCAwLCBzaXplb2YgY250KTsKICAgICAgICBtZW1zZXQoc3VtLCAwLCBzaXplb2Ygc3VtKTsKICAgIH0KCiAgICB2b2lkIGFkZChpbnQgcG9zLCBpbnQgY2FwYSwgaW50IGNvc3QpIHsKICAgICAgICBpbnQgaSA9IDEsIGwgPSAxLCByID0gbjsKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBjbnRbaV0gKz0gY2FwYTsKICAgICAgICAgICAgc3VtW2ldICs9IDFMTCAqIGNhcGEgKiBjb3N0OwogICAgICAgICAgICBtaW5pbWl6ZShjbnRbaV0sIE1BWF9ORUVEKTsKICAgICAgICAgICAgbWluaW1pemUoc3VtW2ldLCBNQVhfQlVER0VUKTsKCiAgICAgICAgICAgIGlmIChsID09IHIpIHJldHVybjsKCiAgICAgICAgICAgIGludCBtID0gKGwgKyByKSA+PiAxOwogICAgICAgICAgICBpZiAocG9zID4gbSkgewogICAgICAgICAgICAgICAgaSA9IDIgKiBpICsgMTsgbCA9IG0gKyAxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgaSA9IDIgKiBpOyByID0gbTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBib29sIGhhdmVFbm91Z2goaW50IG5lZWQsIGxvbmcgbG9uZyBidWRnZXQpIGNvbnN0IHsKICAgICAgICBpZiAoY250WzFdIDwgbmVlZCkgcmV0dXJuIGZhbHNlOwogICAgICAgIGludCBpID0gMSwgbCA9IDEsIHIgPSBuOwoKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpZiAobCA9PSByKSByZXR1cm4gc3VtW2ldIC8gY250W2ldICogbmVlZCA8PSBidWRnZXQ7CgogICAgICAgICAgICBpbnQgbSA9IChsICsgcikgPj4gMTsKICAgICAgICAgICAgaWYgKGNudFsyICogaV0gPT0gbmVlZCkgcmV0dXJuIHN1bVsyICogaV0gPD0gYnVkZ2V0OwogICAgICAgICAgICBpZiAoY250WzIgKiBpXSA+IG5lZWQpIHsKICAgICAgICAgICAgICAgIGkgPSAyICogaTsgciA9IG07CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBidWRnZXQgLT0gc3VtWzIgKiBpXTsKICAgICAgICAgICAgICAgIGlmIChidWRnZXQgPCAwKSByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgICAgICBuZWVkIC09IGNudFsyICogaV07CiAgICAgICAgICAgICAgICBpID0gMiAqIGkgKyAxOyBsID0gbSArIDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn07CgpzdHJ1Y3QgU3RvcmUgewogICAgaW50IHBsYWNlLCBoYXZlLCBwcmljZTsKCiAgICBTdG9yZSgpIHsKICAgICAgICBwbGFjZSA9IGhhdmUgPSBwcmljZSA9IDA7CiAgICB9CgogICAgdm9pZCBpbnB1dCh2b2lkKSB7CiAgICAgICAgc2NhbmYoIiVkJWQlZCIsICZwbGFjZSwgJmhhdmUsICZwcmljZSk7CiAgICB9CgogICAgYm9vbCBvcGVyYXRvciA8IChjb25zdCBTdG9yZSAmcykgY29uc3QgewogICAgICAgIHJldHVybiBwcmljZSA8IHMucHJpY2U7CiAgICB9Cn07CgpzdHJ1Y3QgUXVlcnkgewogICAgaW50IG5lZWQsIGxvdywgaGlnaDsKICAgIGxvbmcgbG9uZyBidWRnZXQ7CgogICAgUXVlcnkoaW50IF9uZWVkID0gMCwgbG9uZyBsb25nIF9idWRnZXQgPSAwKSB7CiAgICAgICAgbmVlZCA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AgICB3aGlsZSAodHJ1ZSkgewogICAgICAgIGFza3MuY2xlYXIoKTsKICAgICAgICBGT1JFKGl0LCBxdWVyeUlEW3BsYWNlXSkgewogICAgICAgICAgICBpbnQgbG93ID0gcXVlcmllc1sqaXRdLmxvdywgaGlnaCA9IHF1ZXJpZXNbKml0XS5oaWdoOwogICAgICAgICAgICBpZiAobG93ID09IGhpZ2gpIGNvbnRpbnVlOwogICAgICAgICAgICBhc2tzLnB1c2hfYmFjayhtYWtlX3BhaXIoaGlnaCAtIGxvdyA9PSAxID8gbG93IDogKGxvdyArIGhpZ2gpID4+IDEsICppdCkpOwovLyAgICAgICAgICAgIGlmICgqaXQgPT0gNCkgcHJpbnRmKCJDSEVDSyBQT0lOVCAlZFxuIiwgaGlnaCAtIGxvdyA9PSAxID8gbG93IDogKGxvdyArIGhpZ2gpID4+IDEpOwogICAgICAgIH0KICAgICAgICBpZiAoYXNrcy5lbXB0eSgpKSBicmVhazsKCiAgICAgICAgbXlpdC5yZXNldCgpOwogICAgICAgIHNvcnQoQUxMKGFza3MpKTsKICAgICAgICBpbnQgaiA9IDE7CgogICAgICAgIEZPUkUoaXQsIGFza3MpIHsKICAgICAgICAgICAgd2hpbGUgKGogPD0gbnVtU3RvcmUpIHsKICAgICAgICAgICAgICAgIGludCBrID0gc3RvcmVzQnlEaXN0W2pdLnNlOwogICAgICAgICAgICAgICAgaWYgKGRpc3RbcGxhY2VdW3N0b3Jlc1trXS5wbGFjZV0gPiBpdC0+ZmkpIGJyZWFrOwogICAgICAgICAgICAgICAgbXlpdC5hZGQoaywgc3RvcmVzW2tdLmhhdmUsIHN0b3Jlc1trXS5wcmljZSk7Ci8vICAgICAgICAgICAgICAgIHByaW50ZigiQUREIHBvcyAlZCwgY2FwYSAlZCwgcHJpY2UgJWRcbiIsIGssIHN0b3Jlc1trXS5oYXZlLCBzdG9yZXNba10ucHJpY2UpOwogICAgICAgICAgICAgICAgaisrOwogICAgICAgICAgICB9CgovLyAgICAgICAgICAgI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