malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long double eps = 1e-9, pi = acos(-1.0);   struct vec {     long double i, x, y;   } vectors[100005];   long double cala(const vec& v) {     long double ang = acos(v.x / sqrt(v.x * v.x + v.y * v.y));     ang = v.y > -eps ? ang : 2 * pi - ang;     return ang;   }   bool cmp(const vec& v1, const vec& v2) { return cala(v1) < cala(v2); }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++)       cin >> vectors[i].x >> vectors[i].y, vectors[i].i = i;     sort(vectors, vectors + n, cmp);     int minp = n - 1;     long double minv =         abs((long double)2 * pi - (cala(vectors[n - 1]) - cala(vectors[0])));     for (int i = 0; i < n - 1; i++) {       if (abs(cala(vectors[i + 1]) - cala(vectors[i])) < minv)         minp = i, minv = abs(cala(vectors[i + 1]) - cala(vectors[i]));     }     if (minp == n - 1)       cout << vectors[minp].i + 1 <<     << vectors[0].i + 1 << endl;     else       cout << vectors[minp].i + 1 <<     << vectors[minp + 1].i + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       if (n <= 30)         cout <<  NO n ;       else {         cout <<  YES n ;         if (n == 36 || n == 40 || n == 44)           cout <<  6 10 15   << n - 31 <<   n ;         else           cout <<  6 10 14   << n - 30 <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   vector<int> p;   long long dp1[555555];   long long dp2[555555];   long long fp(long long x) {     long long ret = 1;     long long mul = 2;     while (x) {       if (x & 1) {         ret *= mul;         ret %= mod;       }       mul *= mul;       mul %= mod;       x >>= 1;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     p.push_back(2);     p.push_back(3);     p.push_back(5);     p.push_back(7);     p.push_back(11);     p.push_back(13);     p.push_back(17);     p.push_back(19);     p.push_back(23);     p.push_back(29);     p.push_back(31);     p.push_back(37);     p.push_back(41);     p.push_back(43);     p.push_back(47);     p.push_back(53);     p.push_back(59);     p.push_back(61);     p.push_back(67);     int k = p.size();     int cnt[80] = {};     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int add;       cin >> add;       cnt[add]++;     }     dp1[0] = 1;     for (int i = 1; i < 70 + 1; i++) {       if (!cnt[i]) continue;       int shift = 0;       int rem = i;       for (int j = 0; j < k; j++) {         while (rem % p[j] == 0) {           shift ^= (1 << j);           rem /= p[j];         }       }       memset(dp2, 0, sizeof(dp2));       ;       long long add = fp(cnt[i] - 1);       for (int j = 0; j < 1 << k; j++) {         dp2[j] += dp1[j] * add % mod;         dp2[j] %= mod;         dp2[j ^ shift] += dp1[j] * add % mod;         dp2[j ^ shift] %= mod;       }       for (int j = 0; j < 1 << k; j++) {         dp1[j] = dp2[j];       }     }     cout << (dp1[0] + mod - 1) % mod << endl;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   int sum[4000005];   int cover[4000005];   int n, m;   void up(int rt) {     if (cover[rt << 1] == cover[rt << 1 | 1] && cover[rt << 1] != -1 &&         cover[rt << 1])       cover[rt] = cover[rt << 1];     else       cover[rt] = -1;   }   void down(int rt) {     if (cover[rt] == 1) {       cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];       if (sum[rt << 1] == 0) sum[rt << 1] = sum[rt];       if (sum[rt << 1 | 1] == 0) sum[rt << 1 | 1] = sum[rt];     }   }   void build(int l, int r, int rt) {     cover[rt] = 0;     if (l == r) {       cover[rt] = 0;       sum[rt] = 0;       return;     }     int m = (l + r) >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     up(rt);   }   void update(int L, int R, int d, int l, int r, int rt) {     if (L <= l && R >= r) {       if (cover[rt] == 1) return;       if (cover[rt] == 0) {         cover[rt] = 1;         sum[rt] = d;         down(rt);         return;       }     }     int m = (l + r) >> 1;     if (L <= m) update(L, R, d, l, m, rt << 1);     if (R > m) update(L, R, d, m + 1, r, rt << 1 | 1);     up(rt);   }   int query(int p, int l, int r, int rt) {     if (l == r) return sum[rt];     int m = (l + r) >> 1;     int res = 0;     if (p <= m) res += query(p, l, m, rt << 1);     if (p > m) res += query(p, m + 1, r, rt << 1 | 1);     return res;   }   int main() {     while (~scanf( %d%d , &n, &m)) {       build(1, n, 1);       for (int i = 0; i < m; i++) {         int a, b, w;         scanf( %d%d%d , &a, &b, &w);         if (w - 1 >= a) update(a, w - 1, w, 1, n, 1);         if (w + 1 <= b) update(w + 1, b, w, 1, n, 1);       }       for (int i = 1; i <= n; i++)         printf(i == n ?  %d n  :  %d  , query(i, 1, n, 1));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string arr[12] = { C ,   C# ,  D ,   D# ,  E ,  F ,                      F# ,  G ,   G# ,  A ,   B ,  H },          a, b, c;   int ok(int a1, int a2, int a3) {     int dis1, dis2;     if (a2 > a1)       dis1 = a2 - a1;     else       dis1 = 12 - a1 + a2;     if (a3 > a2)       dis2 = a3 - a2;     else       dis2 = 12 - a2 + a3;     if (dis1 == 4 && dis2 == 3) return 1;     if (dis1 == 3 && dis2 == 4) return 0;     return 2;   }   int main() {     cin >> a >> b >> c;     int ind1, ind2, ind3;     for (int i = 0; i < 12; i++) {       if (arr[i] == a) ind1 = i;       if (arr[i] == b) ind2 = i;       if (arr[i] == c) ind3 = i;     }     if (ok(ind1, ind2, ind3) == 1 || ok(ind1, ind3, ind2) == 1 ||         ok(ind2, ind1, ind3) == 1 || ok(ind2, ind3, ind1) == 1 ||         ok(ind3, ind1, ind2) == 1 || ok(ind3, ind2, ind1) == 1)       cout <<  major  << endl;     else {       if (ok(ind1, ind2, ind3) == 0 || ok(ind1, ind3, ind2) == 0 ||           ok(ind2, ind1, ind3) == 0 || ok(ind2, ind3, ind1) == 0 ||           ok(ind3, ind1, ind2) == 0 || ok(ind3, ind2, ind1) == 0)         cout <<  minor  << endl;       else         cout <<  strange  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   class domino {    public:     int data[4];     bool IsEqual(domino b) {       for (long long move = 0; move <= 3; move++) {         bool f = true;         for (long long i = 0; i <= 3; i++)           if (data[i] != b.data[(i + move) % 4]) f = false;         if (f) return true;       }       return false;     }   };   vector<domino> a;   int main() {     int n;     cin >> n;     for (long long i = 1; i <= n; i++) {       domino temp;       string s;       cin >> s;       temp.data[0] = s[0] -  0 ;       temp.data[1] = s[1] -  0 ;       cin >> s;       temp.data[3] = s[0] -  0 ;       temp.data[2] = s[1] -  0 ;       bool f = true;       if (a.size() == 0) a.push_back(temp);       for (long long k = 0; k <= a.size() - 1; k++)         if (temp.IsEqual(a[k])) f = false;       if (f) a.push_back(temp);       if (i != n) cin >> s;     }     cout << a.size();   }
#include <bits/stdc++.h>   using namespace std;   const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};   const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};   set<pair<int, pair<int, int> > > heap;   vector<vector<int> > now;   int main() {     int n, m;     cin >> n >> m;     if (n >= 10 && m >= 10) {       cout << (m * n + 1) / 2 << endl;       return 0;     }     now.assign(n, vector<int>(m, 0));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         int cnt = 1;         for (int k = 0; k < 8; k++) {           int nx = i + dx[k];           int ny = j + dy[k];           if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;           cnt++;         }         heap.insert(make_pair(cnt, make_pair(i, j)));         now[i][j] = cnt;       }     int ans = 0;     while (!heap.empty()) {       pair<int, pair<int, int> > v = *heap.begin();       heap.erase(heap.begin());       ans++;       int x = v.second.first;       int y = v.second.second;       for (int i = 0; i < 8; i++) {         int nx = x + dx[i];         int ny = y + dy[i];         if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;         int cnt = now[nx][ny];         if (heap.count(make_pair(cnt, make_pair(nx, ny)))) {           heap.erase(make_pair(cnt, make_pair(nx, ny)));           for (int j = 0; j < 8; j++) {             int x = nx + dx[j];             int y = ny + dy[j];             if (x < 0 || x >= n || y < 0 || y >= m) continue;             int cnt = now[x][y];             if (heap.count(make_pair(cnt, make_pair(x, y)))) {               heap.erase(make_pair(cnt, make_pair(x, y)));               now[x][y]--;               cnt--;               heap.insert(make_pair(cnt, make_pair(x, y)));             }           }         }       }     }     cout << max(ans, (m * n + 1) / 2) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;     node(int a, int b) { x = a, y = b; }   };   int d1[1010][1010];   int main() {     int n, t;     queue<node> Q;     for (int i = 0; i <= 1000; i++) {       for (int j = 0; j <= 1000; j++) {         d1[i][j] = 5000;       }     }     int a, b, c, d;     cin >> n >> a >> b >> c >> d;     Q.push(node(a, b));     d1[a][b] = 0;     while (!Q.empty()) {       node v = Q.front();       Q.pop();       int q = v.x;       int w = v.y;       if (q == c && w == d) {         cout << d1[q][w];         return 0;       }       if (q + 1 <= n && (w == 0 || w == n) && d1[q + 1][w] > d1[q][w] + 1) {         d1[q + 1][w] = d1[q][w] + 1;         Q.push(node(q + 1, w));       }       if (q - 1 >= 0 && (w == 0 || w == n) && d1[q - 1][w] > d1[q][w] + 1) {         d1[q - 1][w] = d1[q][w] + 1;         Q.push(node(q - 1, w));       }       if (w + 1 <= n && (q == 0 || q == n) && d1[q][w + 1] > d1[q][w] + 1) {         d1[q][w + 1] = d1[q][w] + 1;         Q.push(node(q, w + 1));       }       if (w - 1 >= 0 && (q == 0 || q == n) && d1[q][w - 1] > d1[q][w] + 1) {         d1[q][w - 1] = d1[q][w] + 1;         Q.push(node(q, w - 1));       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     scanf( %d %d , &n, &k);     bool first = 1;     for (int i = n; i >= n - k + 1; --i) {       if (first) {         printf( %d , i);         first = !first;       } else         printf(  %d , i);     }     for (int i = 1; i <= n - k; ++i) {       if (first) {         printf( %d , i);         first = !first;       } else         printf(  %d , i);     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     size_t rnn;     cin >> rnn;     size_t rnsize = 2 * rnn;     size_t rncnt = rnsize;     vector<long> rnvec(rnsize);     while (rncnt--) cin >> rnvec[rncnt];     sort(rnvec.begin(), rnvec.end());     long rnsum_fin = 999999;     long rnsum = 0;     for (size_t rncnt1 = 0; rncnt1 < rnsize; ++rncnt1)       for (size_t rncnt2 = 0; rncnt2 < rncnt1; ++rncnt2) {         rnsum = 0;         for (size_t rncnt3 = 0, rnpos = 0; rncnt3 < rnsize; ++rncnt3)           if (rncnt3 != rncnt1 && rncnt3 != rncnt2) {             ++rnpos;             if (rnpos % 2)               rnsum -= rnvec[rncnt3];             else               rnsum += rnvec[rncnt3];           }         rnsum_fin = min(rnsum, rnsum_fin);       }     cout << rnsum_fin;   }
#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, i;       scanf( %d , &n);       if (n % 4 != 0)         printf( NO n );       else {         printf( YES n );         int sum1 = 0, sum2 = 0;         for (i = 0; i < n / 2; i++) {           printf( %d  , 2 * (i + 1));           sum1 += 2 * (i + 1);         }         for (i = 0; i < n / 2 - 1; i++) {           printf( %d  , 2 * (i + 1) - 1);           sum2 += 2 * (i + 1) - 1;         }         printf( %d n , sum1 - sum2);       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N_ = 105;   int N, M, X, Y;   int len(int x) {     for (int l = 1, v = 7;; l++) {       if (v >= x) return l;       v *= 7;     }   }   set<long long> S;   bool chk[100];   void dfs(int i, long long p, long long q) {     if (p >= N || q >= M) return;     if (i == X + Y) {       S.insert(p * (long long)1e9 + q);       return;     }     for (int d = 0; d < 7; d++) {       if (!chk[d]) {         chk[d] = true;         ((i < X ? p : q) *= 7) += d;         dfs(i + 1, p, q);         ((i < X ? p : q) -= d) /= 7;         chk[d] = false;       }     }   }   int main() {     scanf( %d %d , &N, &M);     X = len(N), Y = len(M);     if (X + Y > 7) return 0 & puts( 0 );     dfs(0, 0, 0);     printf( %lu n , S.size());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 85;   long long fibo[N + 5];   long long dp[N + 5][3][3];   long long arr[N + 5];   void pre() {     fibo[0] = 1, fibo[1] = 2;     for (int i = 2; i <= N; i++) {       fibo[i] = fibo[i - 1] + fibo[i - 2];     }   }   long long solve(int idx, int cur1, int cur2) {     if (idx == -1) {       if (cur1 + cur2 != 0) return 0;       return 1;     }     long long &res = dp[idx][cur1][cur2];     if (res != -1) return res;     res = 0;     if (cur1 + arr[idx] > 2) return 0;     if (cur1 + arr[idx] <= 1) res += solve(idx - 1, cur2, 0);     if (cur1 + arr[idx] == 1) res += solve(idx - 1, cur2 + 1, 1);     if (cur1 + arr[idx] == 2) res += solve(idx - 1, cur2 + 1, 1);     return res;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     pre();     int ntc;     cin >> ntc;     while (ntc--) {       long long x;       cin >> x;       for (int i = N; i >= 0; i--) {         arr[i] = 0;         if (x < fibo[i]) continue;         x -= fibo[i];         arr[i] = 1;       }       memset(dp, -1, sizeof dp);       cout << solve(N, 0, 0) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   const long long MOD = 100030001, H = 47;   map<int, long long> d[N];   vector<int> adj[N];   set<pair<long long, int> > s;   vector<pair<int, pair<long long, int> > > nei[N];   long long r[N], l[N], num[N], ch[N], h[3 * N], n, sz, ans, o = 10, c = 11;   void add(int ind, int val) {     auto it = s.lower_bound({ind, val});     if (it == s.end() || it->first != ind)       s.insert({ind, val});     else {       if (it->second + val != 0) s.insert({ind, it->second + val});       s.erase(it);     }     return;   }   void f(int v, int u) {     add(d[v][v], -1);     add(d[u][u], 1);     add(d[v][u], 1);     add(d[u][v], -1);     return;   }   void DFS1(int v, int par = 0) {     for (int u : adj[v]) {       if (u != par) {         DFS1(u, v);         ch[v] += ch[u];         nei[v].push_back({d[u][v], {ch[u], u}});       }     }     ch[v]++;     long long ans = o;     sort(nei[v].begin(), nei[v].end());     for (auto u : nei[v]) {       ans = ans * h[2 * u.second.first] % MOD;       ans = (ans + u.first) % MOD;     }     d[v][par] = (ans * H + c) % MOD;     return;   }   void DFS2(int v, int par = 0) {     nei[v].clear();     for (int u : adj[v]) {       nei[v].push_back({d[u][v], {ch[u], u}});       if (u == par) nei[v].back().second.first = n - ch[v];     }     sort(nei[v].begin(), nei[v].end());     l[0] = o, r[nei[v].size() + 1] = c, num[nei[v].size() + 1] = 1;     for (int i = 1; i <= nei[v].size(); i++) {       auto u = nei[v][i - 1];       l[i] = (l[i - 1] * h[u.second.first * 2] + d[u.second.second][v]) % MOD;     }     for (int i = nei[v].size(); i > 0; i--) {       auto u = nei[v][i - 1];       num[i] = num[i + 1] + u.second.first * 2;       r[i] = (r[i + 1] + d[u.second.second][v] * h[num[i + 1]]) % MOD;     }     for (int i = 0; i < nei[v].size(); i++) {       d[v][nei[v][i].second.second] = (l[i] * h[num[i + 2]] + r[i + 2]) % MOD;     }     for (int u : adj[v])       if (u != par) DFS2(u, v);     add(d[v][par], 1);     return;   }   void DFS3(int v, int par = 0) {     if (s.size() > sz) {       ans = v;       sz = s.size();     }     for (int u : adj[v]) {       if (u != par) {         f(v, u);         DFS3(u, v);       }     }     f(v, par);     return;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int u, v;     cin >> n;     h[0] = 1;     for (int i = 0; i <= n * 2 + 10; i++) h[i + 1] = h[i] * H % MOD;     for (int i = 1; i < n; i++) {       cin >> u >> v;       adj[--u].push_back(--v);       adj[v].push_back(u);     }     if (n == 1) {       cout << 1 << endl;       return 0;     }     DFS1(0);     DFS2(0);     DFS3(0);     cout << ans + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int k;   bool debug = false;   char mp[170][170];   int l[170], r[170];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       scanf( %s , mp[i]);       l[i] = m;       r[i] = -1;       for (int j = 0; j < m; j++)         if (mp[i][j] ==  W ) {           l[i] = j;           break;         }       for (int j = m - 1; j >= 0; j--)         if (mp[i][j] ==  W ) {           r[i] = j;           break;         }     }     int ans = 0, pos = 0, dir = 1;     for (int i = 0, j = 1; i < n; i = j) {       j = i + 1;       while (j < n && r[j] == -1) j++;       if (r[i] != -1) {         if (dir == 1) {           ans += r[i] - pos;           pos = r[i];         } else {           ans += pos - l[i];           pos = l[i];         }       }       if (j == n)         break;       else {         dir = (j - i) % 2 == 1 ? 1 - dir : dir;         int npos;         if (dir == 1) {           npos = min(l[j], pos);         } else {           npos = max(r[j], pos);         }         ans += abs(npos - pos);         pos = npos;         ans += (j - i);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<long long> cnt(26);     string s;     for (int ss = 0; ss < (int)n; ss++) {       cin >> s;       for (int i = 0; i < (int)s.size(); i++) s[i] -=  a ;       int nfst = 0, nlst = 0;       while (nfst < s.size() && s[nfst] == s[0]) nfst++;       while (nlst < s.size() && s[s.size() - nlst - 1] == s.back()) nlst++;       vector<long long> old(cnt);       fill(cnt.begin(), cnt.end(), 0);       for (int i = 0; i < (int)26; i++) cnt[i] = old[i] > 0;       if (nfst == s.size())         cnt[s[0]] = (old[s[0]] + 1) * nfst + old[s[0]];       else if (s[0] == s.back()) {         if (old[s[0]]) cnt[s[0]] = nfst + 1 + nlst;       } else {         if (old[s[0]]) cnt[s[0]] = nfst + 1;         if (old[s.back()]) cnt[s.back()] = 1 + nlst;       }       char cur = s[0];       long long streak = 1;       cnt[cur] = max(cnt[cur], streak);       for (int i = 1; i < s.size(); i++) {         if (s[i] == cur)           streak++;         else {           cur = s[i];           streak = 1;         }         cnt[cur] = max(cnt[cur], streak);       }       for (int i = 0; i < (int)26; i++) cnt[i] = min(cnt[i], (long long)1e9);     }     long long ans = 0;     for (int i = 0; i < (int)26; i++) ans = max(ans, cnt[i]);     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > v;   map<long long, long long> m;   int qpow(long long a, long long b, long long m) {     long long c = 1;     while (b) {       if (b & 1) c = c * a % m;       b >>= 1;       a = a * a % m;     }     return c;   }   int main() {     ios::sync_with_stdio(false);     long long num, n, s, t;     cin >> n >> s;     if (8 * s / n < 63) {       num = pow(2, 8 * s / n);     } else {       num = 0x7fffffffffffffff;     }     for (int i = (0); i < (n); ++i) {       cin >> t;       if (m.count(t) == 0) {         v.push_back(make_pair(t, 0));         m[t] = 1;       } else {         m[t]++;       }     }     int len = v.size();     for (int i = (0); i < (len); ++i) {       v[i].second = m[v[i].first];     }     sort(v.begin(), v.end());     long long l = -1, r = len;     long long ans = 0;     while (l < r && len > num) {       l++;       ans += v[l].second;       len--;     }     long long minn = ans;     while (l >= 0) {       if (l >= r) {         cout << n;         return 0;       }       ans -= v[l].second;       len++;       l--;       while (len > num) {         r--;         ans += v[r].second;         len--;       }       minn = min(ans, minn);     }     cout << minn;     return 0;   }
#include <bits/stdc++.h>   int inp() {     char c = getchar();     int neg = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) neg = -1;       c = getchar();     }     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum * neg;   }   int n, p;   int main() {     n = inp();     p = inp();     for (int i = 1; i <= 1000; i++) {       if (n - (i * p) <= 0) break;       if (__builtin_popcount(n - (i * p)) <= i && i <= (n - (i * p))) {         printf( %d n , i);         return 0;       }     }     printf( -1 n );   }
#include <bits/stdc++.h>   using namespace std;   long long int sum, max_lev;   vector<pair<long long int, long long int>> level[5010];   vector<long long int> adj[5010];   vector<pair<long long int, long long int>> store[5010];   vector<pair<pair<long long int, long long int>, long long int>> v;   long long int vis[5010];   void bfs(long long int s, long long int lev) {     queue<pair<long long int, long long int>> q;     q.push(make_pair(s, lev));     vis[s] = 1;     while (!q.empty()) {       s = q.front().first;       lev = q.front().second;       for (long long int i = 0; i < store[s].size(); i++) {         level[lev].push_back(make_pair(store[s][i].first, store[s][i].second));         sum += store[s][i].first;       }       q.pop();       max_lev = max(lev, max_lev);       for (long long int i = 0; i < adj[s].size(); i++) {         if (vis[adj[s][i]] == 0) {           vis[adj[s][i]] = 1;           q.push(make_pair(adj[s][i], lev + 1));         }       }     }   }   bool check(long long int mid, long long int max_cost, long long int tot) {     long long int cost = 0, curr = 0, p, rem, i, j;     for (i = 0; i < v.size(); i++) {       if (v[i].second > mid) continue;       p = v[i].first.first * v[i].first.second;       if (cost + p > max_cost) {         rem = max_cost - cost;         p = rem / v[i].first.first;         curr += p;         break;       }       curr += v[i].first.second;       cost += p;     }     if (curr >= tot) return true;     return false;   }   long long int bs(long long int low, long long int high, long long int max_cost,                    long long int tot) {     long long int ans = -1;     while (low <= high) {       long long int mid = (low + high) / 2;       if (check(mid, max_cost, tot)) {         high = mid - 1;         ans = mid;       } else {         low = mid + 1;       }     }     return ans;   }   int main() {     long long int n, m, a, b, i, w, c, k, p, q, g, r, j;     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (i = 1; i <= m; i++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     cin >> w;     for (i = 1; i <= w; i++) {       cin >> c >> k >> p;       store[c].push_back(make_pair(k, p));     }     cin >> q;     while (q--) {       sum = max_lev = 0;       cin >> g >> r >> a;       for (i = 0; i <= n; i++) level[i].clear();       v.clear();       memset(vis, 0, sizeof(vis));       bfs(g, 0);       if (sum < r) {         cout <<  -1 n ;         continue;       }       for (i = 0; i <= max_lev; i++) {         for (j = 0; j < level[i].size(); j++) {           v.push_back(               make_pair(make_pair(level[i][j].second, level[i][j].first), i));         }       }       sort(v.begin(), v.end());       cout << bs(0, max_lev, a, r) <<   n ;     }     return 0;   }