#include <bits/stdc++.h>
 using namespace std;
 int n, Mod, p[100010], a[200010], nxt[200010], w[200010], tot, root,
     size[100010], son[100010], cnt;
 bool vis[100010];
 long long ans, mul[100010];
 struct record {
   int dep, step;
   record(int D = 0, int S = 0) {
     dep = D;
     step = S;
   }
 } pre[100010], suf[100010];
 int comp(record a, record b) { return a.dep < b.dep; }
 void add(int x, int y, int val) {
   tot++;
   a[tot] = y;
   nxt[tot] = p[x];
   w[tot] = val;
   p[x] = tot;
 }
 void getroot(int u, int fa, int N) {
   size[u] = 1;
   son[u] = 0;
   for (int i = p[u]; i != 0; i = nxt[i])
     if (a[i] != fa && vis[a[i]] == false) {
       getroot(a[i], u, N);
       size[u] += size[a[i]];
       son[u] = max(son[u], size[a[i]]);
     }
   son[u] = max(son[u], N - size[u]);
   if (son[u] < son[root]) root = u;
 }
 void getdeep(int u, int fa, long long pred, int step, long long sufd) {
   pre[++cnt] = record(pred, step);
   suf[cnt] = record(sufd, step);
   for (int i = p[u]; i != 0; i = nxt[i])
     if (a[i] != fa && vis[a[i]] == false)
       getdeep(a[i], u, (pred * 10 + w[i]) % Mod, step + 1,
               (sufd + w[i] * mul[step] % Mod) % Mod);
 }
 int findL(int l, int r, int x, record *v) {
   int mid;
   while (l != r) {
     mid = (l + r) >> 1;
     if (v[mid].dep >= x)
       r = mid;
     else
       l = mid + 1;
   }
   if (v[l].dep != x) return 1000000000;
   return l;
 }
 int findR(int l, int r, int x, record *v) {
   int mid, ans = 0;
   while (l <= r) {
     mid = (l + r) >> 1;
     if (v[mid].dep <= x) {
       ans = max(ans, mid);
       l = mid + 1;
     } else
       r = mid - 1;
   }
   if (v[ans].dep != x) return 0;
   return ans;
 }
 long long getnum(int l, int r, int now, record *v) {
   int L, R;
   if (l > r) return 0;
   L = findL(l, r, now, v);
   R = findR(l, r, now, v);
   if (L <= R) return R - L + 1;
   return 0;
 }
 void exgcd(int a, int b, long long &x, long long &y) {
   if (b == 0) {
     x = 1;
     y = 0;
     return;
   }
   long long t;
   exgcd(b, a % b, x, y);
   t = x;
   x = y;
   y = t - (a / b) * y;
 }
 int getinv(long long A, long long B) {
   long long x, y;
   exgcd(A, Mod, x, y);
   x = (x % Mod + Mod) % Mod;
   x = (x * B) % Mod;
   return x;
 }
 long long calc(int u, int pre0, int s0, int suf0) {
   long long sum = 0;
   cnt = 0;
   getdeep(u, 0, pre0, s0, suf0);
   sort(pre + 1, pre + cnt + 1, comp);
   sort(suf + 1, suf + cnt + 1, comp);
   for (int i = 1; i <= cnt; i++) {
     int now = getinv(mul[pre[i].step], Mod - pre[i].dep);
     sum += getnum(1, cnt, now, suf);
     if (now == 0) --sum;
   }
   return sum;
 }
 void work(int u) {
   ans += calc(u, 0, 0, 0);
   vis[u] = true;
   for (int i = p[u]; i != 0; i = nxt[i])
     if (vis[a[i]] == false) {
       ans -= calc(a[i], w[i] % Mod, 1, w[i] % Mod);
       root = 0;
       getroot(a[i], 0, size[a[i]]);
       work(root);
     }
 }
 int main() {
   scanf('%d%d', &n, &Mod);
   for (int i = 1; i < n; i++) {
     int x, y, v;
     scanf('%d%d%d', &x, &y, &v);
     ++x;
     ++y;
     add(x, y, v);
     add(y, x, v);
   }
   son[0] = 1000000000;
   root = 0;
   mul[0] = 1;
   for (int i = 1; i <= n; i++) mul[i] = mul[i - 1] * 10 % Mod;
   getroot(1, 0, n);
   work(root);
   printf('%I64d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline T gcd(T a, T b) {
   return b ? gcd(b, a % b) : a;
 }
 template <class T>
 inline T LCM(T a, T b) {
   if (a < 0) return LCM(-a, b);
   if (b < 0) return LCM(a, -b);
   return a * (b / gcd(a, b));
 }
 vector<int> arr;
 const int N = 50005;
 int memo[5001];
 int save[N];
 int visit[N];
 int find(int pos, int &xr) {
   for (int i = 0; i < N; i++) visit[i] = 0;
   vector<int> toAdd;
   int end = pos;
   visit[arr[pos]] = 1;
   for (int i = 0; i < pos; i++) {
     visit[arr[i]] = 2;
   }
   if (visit[arr[pos]] == 2) {
     return -1;
   }
   bool cant = false;
   for (int i = pos; i < arr.size(); i++) {
     if (visit[arr[i]] == 2) {
       cant = true;
     }
     if (visit[arr[i]] == 1) {
       if (cant) return -1;
       for (int j = 0; j < toAdd.size(); j++) {
         visit[toAdd[j]] = 1;
       }
       toAdd.clear();
       end = i;
     } else {
       toAdd.push_back(arr[i]);
     }
   }
   xr = 0;
   for (int i = 0; i < N; i++) {
     if (visit[i] == 1) {
       xr = xr ^ i;
     }
   }
   return end;
 }
 int solve(int from) {
   int txr = 0;
   int ret = 0;
   if (from >= arr.size()) return 0;
   if (memo[from] != -1) {
     return memo[from];
   }
   int end = find(from, txr);
   if (end != -1) {
     ret = max(txr + solve(end + 1), solve(from + 1));
   } else {
     ret = solve(from + 1);
   }
   memo[from] = ret;
   return ret;
 }
 int main() {
   int n, i, temp, j;
   for (i = (int)0; i < (int)5001; ++i) {
     memo[i] = -1;
   }
   scanf('%d', &n);
   for (i = (int)0; i < (int)n; ++i) {
     scanf('%d', &temp);
     arr.push_back(temp);
   }
   int xx;
   cout << solve(0);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int a, b, c, d;
 void solve() {
   double m = max((3.0) * a / (10.0), (double)(a - a * c / 250));
   double v = max((3.0) * b / (10.0), (double)(b - b * d / 250));
   if (m > v) cout << 'Misha' << endl;
   if (m == v) cout << 'Tie' << endl;
   if (m < v) cout << 'Vasya' << endl;
 }
 int main() {
   while (cin >> a >> b >> c >> d) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 string op;
 bool check(int l, int r) {
   char c;
   cout << '? ' << l << ' ' << r << endl;
   cin >> c;
   return c == 'x';
 }
 int main() {
   while (cin >> op) {
     if (op == 'end') {
       return 0;
     }
     int l = 1, r = 2;
     while (1) {
       if (check(l, r)) break;
       l <<= 1, r <<= 1;
     }
     while (l + 1 < r) {
       int mid = (l + r) >> 1;
       if (check(l - 1, mid))
         r = mid;
       else
         l = mid + 1;
     }
     if (l == 1 && r == 2) {
       if (check(2, 3))
         cout << '! 1' << endl;
       else
         cout << '! 2' << endl;
     } else {
       if (l == r)
         cout << '! ' << l << endl;
       else {
         if (check(l, l + 1))
           cout << '! ' << l + 1 << endl;
         else
           cout << '! ' << l << endl;
       }
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, k, a[1000005];
 long long l;
 vector<int> val, cou, dp[1000005];
 void add(int &a, int b) {
   a += b;
   if (a >= 1000000007) a -= 1000000007;
 }
 void setup() {
   cin >> n >> l >> k;
   for (int i = 0; i <= n - 1; i++) {
     cin >> a[i];
     val.push_back(a[i]);
   }
   sort(val.begin(), val.end());
   val.resize(unique(val.begin(), val.end()) - val.begin());
   cou.resize(val.size());
   for (int i = 0; i <= n - 1; i++) {
     dp[i].resize(k + 1);
     dp[i][1] = 1;
     a[i] = lower_bound(val.begin(), val.end(), a[i]) - val.begin();
   }
 }
 void xuly() {
   for (int j = 2; j <= k; j++) {
     for (int i = 0; i <= int(cou.size()) - 1; i++) cou[i] = 0;
     for (int i = 0; i <= n - 1; i++) add(cou[a[i]], dp[i][j - 1]);
     for (int i = 1; i <= int(cou.size()) - 1; i++) add(cou[i], cou[i - 1]);
     for (int i = 0; i <= n - 1; i++) dp[i][j] = cou[a[i]];
   }
   int ans = 0;
   for (int i = 0; i <= n - 1; i++)
     for (int j = 1; j <= k; j++) {
       long long m = l / n;
       if (i < l % n) m++;
       m = m - j + 1;
       if (m > 0) ans = (ans + (m % 1000000007) * dp[i][j]) % 1000000007;
     }
   cout << ans;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(NULL);
   cout.tie(NULL);
   setup();
   xuly();
   return 0;
 }


#include <bits/stdc++.h>
 const int N = 500005;
 int to[N << 1], edge, Next[N << 1], last[N], n, x, y, d[N], mx1[N], mx2[N];
 int f[N], fmx[N], ans1, mx[N], f0[N], f1[N], sum[N];
 std::vector<int> e[N];
 void upd(int &x, int y) {
   if (y > x) x = y;
 }
 void add(int x, int y) {
   to[++edge] = y;
   Next[edge] = last[x];
   last[x] = edge;
 }
 void dfs1(int x, int y) {
   f[x] = y;
   for (int i = last[x]; i; i = Next[i]) {
     int u = to[i];
     if (u == y) continue;
     dfs1(u, x);
     if (mx1[u] + 1 > mx1[x])
       mx2[x] = mx1[x], mx1[x] = mx1[u] + 1;
     else if (mx1[u] + 1 > mx2[x])
       mx2[x] = mx1[u] + 1;
   }
 }
 void dfs2(int x, int y) {
   if (y) e[fmx[x]].push_back(x);
   for (int i = last[x]; i; i = Next[i]) {
     int u = to[i];
     if (u == y) continue;
     if (mx1[x] == mx1[u] + 1)
       fmx[u] = std::max(mx2[x], fmx[x]) + 1;
     else
       fmx[u] = std::max(mx1[x], fmx[x]) + 1;
     e[mx1[u] + 1].push_back(x);
   }
   for (int i = last[x]; i; i = Next[i])
     if (to[i] != y) dfs2(to[i], x);
 }
 int main() {
   scanf('%d', &n);
   for (int i = 1; i < n; i++) {
     scanf('%d%d', &x, &y);
     add(x, y), add(y, x);
     d[x]++, d[y]++;
   }
   for (int i = 1; i <= n; i++) upd(ans1, d[i] + 1);
   dfs1(1, 0);
   dfs2(1, 0);
   f0[n] = f1[n] = 1;
   for (int i = n; i >= 1; i--) {
     int lst = 0;
     for (auto u : e[i]) {
       sum[u]++;
       upd(f0[i], sum[u]);
       upd(f1[i - 1], sum[u]);
       if (lst != u) upd(f1[i], sum[u]);
       upd(mx[f[u]], sum[u]);
       upd(f0[i], sum[u] + mx[u] - 2);
       if (f[u]) upd(f0[i], sum[u] + sum[f[u]] - 2);
       lst = u;
     }
   }
   for (int i = n; i >= 1; i--) upd(f0[i], f0[i + 1]), upd(f1[i], f1[i + 1]);
   printf('%d ', ans1);
   for (int i = 2; i <= n; i++)
     if (i & 1)
       printf('%d ', f1[i / 2]);
     else
       printf('%d ', f0[i / 2]);
 }


#include <bits/stdc++.h>
 using namespace std;
 int a[1000];
 bool b[1000][1000];
 int main() {
   int N, M;
   scanf('%d%d', &N, &M);
   for (int i = 0; i < N; i++) scanf('%d', &a[i]);
   for (int i = 0; i < M; i++) {
     int x, y;
     scanf('%d%d', &x, &y);
     --x;
     --y;
     b[x][y] = true;
     b[y][x] = true;
   }
   int mincost = 1000000000;
   for (int x = 0; x < N; x++)
     for (int y = 0; y < N; y++)
       for (int z = 0; z < N; z++)
         if (b[x][y] && b[y][z] && b[x][z])
           if (a[x] + a[y] + a[z] < mincost) mincost = a[x] + a[y] + a[z];
   if (mincost == 1000000000)
     printf('-1');
   else
     printf('%d
', mincost);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   vector<vector<int> > arr(n + 1, vector<int>(m + 1));
   for (int k = 1; k <= n; k++) {
     for (int i = 1; i <= m; i++) {
       cin >> arr[k][i];
       arr[k][i] += arr[k][i - 1];
     }
   }
   for (int i = 1; i <= m; i++) {
     for (int k = 1; k <= n; k++) {
       arr[k][i] += arr[k - 1][i];
     }
   }
   int a, b;
   cin >> a >> b;
   int res = 1e9;
   for (int k = a; k <= n; k++) {
     for (int i = b; i <= m; i++) {
       res = min(res,
                 arr[k][i] - arr[k][i - b] - arr[k - a][i] + arr[k - a][i - b]);
     }
   }
   for (int k = b; k <= n; k++) {
     for (int i = a; i <= m; i++) {
       res = min(res,
                 arr[k][i] - arr[k][i - a] - arr[k - b][i] + arr[k - b][i - a]);
     }
   }
   cout << res << '
';
   return 0;
 }


#include <bits/stdc++.h>
 const int N = 1e5 + 50;
 using namespace std;
 int n, m, h, tot, mx, maxn, ans;
 int a[N], b[N], cnt[N], num[N];
 int read() {
   int s = 0, t = 1;
   char c;
   while (c < '0' || c > '9') {
     if (c == '-') t = -1;
     c = getchar();
   }
   while (c >= '0' && c <= '9') {
     s = s * 10 + c - '0';
     c = getchar();
   }
   return s * t;
 }
 long long readl() {
   long long s = 0, t = 1;
   char c;
   while (c < '0' || c > '9') {
     if (c == '-') t = -1;
     c = getchar();
   }
   while (c >= '0' && c <= '9') {
     s = s * 10 + c - '0';
     c = getchar();
   }
   return s * t;
 }
 int main() {
   n = read();
   m = read();
   for (int i = 1; i <= m; i++) a[i] = read(), b[a[i]]++, mx = max(mx, a[i]);
   for (int i = 1; i <= mx; i++)
     if (b[i] > 0) cnt[++tot] = b[i];
   for (int i = 1; i <= m; i++) {
     h = 0;
     for (int j = 1; j <= tot; j++) h += cnt[j] / i;
     if (h >= n)
       ans = i;
     else
       break;
   }
   printf('%d', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   int n, q;
   cin >> n >> q;
   set<int> s;
   multiset<int> d;
   auto Add = [&](int x) {
     auto ret = s.insert(x);
     auto it = ret.first;
     if (next(it) != s.end() && it != s.begin()) {
       d.erase(d.find(*next(it) - *prev(it)));
     }
     if (it != s.begin()) {
       d.insert(*it - *prev(it));
     }
     if (next(it) != s.end()) {
       d.insert(*next(it) - *it);
     }
   };
   auto Remove = [&](int x) {
     auto it = s.find(x);
     if (it != s.begin()) {
       d.erase(d.find(*it - *prev(it)));
     }
     if (next(it) != s.end()) {
       d.erase(d.find(*next(it) - *it));
     }
     if (next(it) != s.end() && it != s.begin()) {
       d.insert(*next(it) - *prev(it));
     }
     s.erase(it);
   };
   for (int i = 0; i < n; i++) {
     int foo;
     cin >> foo;
     Add(foo);
   }
   for (int i = 0; i <= q; i++) {
     if (d.empty()) {
       cout << 0 << '
';
     } else {
       cout << *prev(s.end()) - *s.begin() - *prev(d.end()) << '
';
     }
     if (i == q) {
       break;
     }
     int c, x;
     cin >> c >> x;
     if (c) {
       Add(x);
     } else {
       Remove(x);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline long long read() {
   char ch = getchar();
   long long s = 0, w = 1;
   while (ch < '0' || ch > '9') {
     if (ch == '-') w = -1;
     ch = getchar();
   }
   while (ch >= '0' && ch <= '9') {
     s = s * 10 + ch - '0';
     ch = getchar();
   }
   return s * w;
 }
 int d[1000010], t[1000010], prime[1000010], tot, vis[1000010];
 int n, fa[1000010], a[1000010], m;
 long long c[1000010];
 inline void prepare(int Maxn) {
   d[1] = 1;
   for (register int i = 2; i <= Maxn; i++) {
     if (!vis[i]) {
       prime[++tot] = i;
       t[i] = d[i] = 2;
     }
     for (register int j = 1; 1LL * prime[j] * i <= Maxn; j++) {
       vis[i * prime[j]] = 1;
       if (i % prime[j]) {
         d[i * prime[j]] = d[i] * 2;
         t[i * prime[j]] = 2;
       } else {
         d[i * prime[j]] = d[i] / t[i] * (t[i] + 1);
         t[i * prime[j]] = t[i] + 1;
         break;
       }
     }
   }
 }
 inline void add(int x, int v) {
   for (; x <= n; x += x & (-x)) c[x] += v;
 }
 inline long long query(int x) {
   long long sum = 0;
   for (; x; x -= x & (-x)) sum += c[x];
   return sum;
 }
 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
 int main() {
   prepare(1000010 - 10);
   n = read(), m = read();
   for (register int i = 1; i <= n; i++) a[i] = read(), fa[i] = i;
   fa[n + 1] = n + 1;
   for (register int i = 1; i <= n; i++) add(i, a[i]);
   while (m--) {
     int opt = read(), l = read(), r = read();
     if (opt == 1) {
       for (register int i = l; i <= r; i = find(i + 1)) {
         add(i, d[a[i]] - a[i]);
         a[i] = d[a[i]];
         if (a[i] == 2 || a[i] == 1) fa[find(i)] = find(i + 1);
       }
     } else {
       printf('%lld
', query(r) - query(l - 1));
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char str[100050];
 char a[5000], b[5000];
 int len1, len2, len3;
 bool pp(int s1, int s2) {
   while (s2 <= len2) {
     if (str[s1] != a[s2]) return false;
     s1++, s2++;
   }
   return true;
 }
 bool pp2(int s1, int s2) {
   while (s2 <= len3) {
     if (str[s1] != b[s2]) return false;
     s1++, s2++;
   }
   return true;
 }
 bool rp(int s1, int s2) {
   while (s2 <= len2) {
     if (str[s1] != a[s2]) return false;
     s1--, s2++;
   }
   return true;
 }
 bool rp2(int s1, int s2) {
   while (s2 <= len3) {
     if (str[s1] != b[s2]) return false;
     s1--, s2++;
   }
   return true;
 }
 int main() { printf('Even
'); }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long num, tmp, sum, mx, l, r, mid;
   cin >> num;
   mx = sum = 0;
   for (int i = 0; i < num; i++) {
     cin >> tmp;
     sum += tmp;
     mx = max(mx, tmp);
   }
   l = mx;
   r = sum;
   while (l < r) {
     mid = (l + r) / 2;
     if (mid * num - sum >= mid)
       r = mid;
     else
       l = mid + 1;
   }
   cout << l;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 505;
 const int maxm = 1e4 + 5;
 struct node {
   int pre[maxn];
   void init() {
     for (int i = 0; i < maxn; ++i) {
       pre[i] = i;
     }
   }
   int Find(int x) {
     if (x != pre[x]) pre[x] = Find(pre[x]);
     return pre[x];
   }
   void v(int x, int y) {
     int fx = Find(x), fy = Find(y);
     if (fx != fy) pre[fy] = fx;
   }
 };
 struct node q[maxm], h[maxm];
 int u[maxm], v[maxm];
 int n, m;
 int solve(node a, node b) {
   int ans = 0;
   for (int i = 1; i <= n; ++i) {
     a.v(i, b.Find(i));
   }
   for (int i = 1; i <= n; ++i) {
     if (a.Find(i) == i) ans++;
   }
   return ans;
 }
 int main() {
   scanf('%d%d', &n, &m);
   for (int i = 1; i <= m; ++i) scanf('%d%d', &u[i], &v[i]);
   q[0].init();
   h[m + 1].init();
   for (int i = 1; i <= m; ++i) {
     q[i] = q[i - 1];
     q[i].v(u[i], v[i]);
   }
   for (int i = m; i >= 1; --i) {
     h[i] = h[i + 1];
     h[i].v(u[i], v[i]);
   }
   int tt;
   scanf('%d', &tt);
   while (tt--) {
     int x, y;
     scanf('%d%d', &x, &y);
     printf('%d
', solve(q[x - 1], h[y + 1]));
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   vector<long long int> v, t;
   v.push_back(0), t.push_back(0);
   long long int sum = 0, tt = 1, x = 0;
   double result = 0;
   long long int ix, a, b;
   for (int i = 0; i < n; ++i) {
     cin >> ix;
     if (ix == 1) {
       cin >> a >> b;
       t[a - 1] += b;
       sum += a * b;
     } else if (ix == 2) {
       cin >> a;
       v.push_back(a), t.push_back(0);
       sum += a;
       ++tt;
     } else {
       t[t.size() - 2] += t.back();
       sum -= (v.back() + t.back());
       --tt;
       v.pop_back(), t.pop_back();
     }
     result = (double)sum / (double)tt;
     cout << fixed << setprecision(6) << result << '
';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long N = 1e9 + 5;
 long long n, k;
 bool valid(long long x) {
   long long maxi = (x)*k - ((x - 1) * ((x + 2) / 2.0));
   if (maxi >= n)
     return true;
   else
     return false;
 }
 long long binary() {
   long long lo = 0, mid, hi = k;
   while (hi - lo > 0) {
     mid = (hi + lo) / 2;
     if (valid(mid))
       hi = mid;
     else
       lo = mid + 1;
   }
   if (hi == lo)
     return lo;
   else
     return -1;
 }
 int main() {
   scanf('%I64d %I64d', &n, &k);
   long long ans = binary();
   long long maxi = k;
   maxi += (ans - 1) * k - ((ans - 1) * ((ans + 2) / 2));
   if (ans & 1 && ans != 1) maxi--;
   if (maxi >= n)
     printf('%I64d', ans);
   else
     printf('-1');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long N = 1e6 + 10;
 int a[N], b[N];
 int main() {
   int n;
   cin >> n;
   for (int i = 1; i <= n; i++) {
     cin >> a[i];
   }
   vector<int> ans;
   for (int i = 1; i <= n; i++) {
     b[i] = a[i] - a[i - 1];
   }
   for (int i = 1; i <= n; i++) {
     int flag = 0;
     for (int j = 1; i + j <= n; j++) {
       if (b[j] != b[i + j]) {
         flag++;
         break;
       }
     }
     if (!flag) {
       ans.push_back(i);
     }
   }
   cout << ans.size() << endl;
   for (auto i : ans) cout << i << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e5 + 6;
 bool flag = true;
 vector<int> v[N];
 int a[N], p[N], h[N], g[N];
 void dfs(int u, int temp = -1) {
   a[u] = p[u];
   int sum = 0;
   for (auto x : v[u]) {
     if (x == temp) continue;
     dfs(x, u);
     sum += g[x];
     a[u] += a[x];
   }
   if ((a[u] + h[u]) % 2 != 0) flag = false;
   g[u] = (a[u] + h[u]) / 2;
   if (g[u] >= 0 and g[u] <= a[u]) {
   } else
     flag = false;
   if (sum > g[u]) flag = false;
 }
 int main() {
   long long int t;
   cin >> t;
   while (t--) {
     int n, m;
     cin >> n >> m;
     for (int i = 0; i < n; i++) cin >> p[i];
     ;
     for (int i = 0; i < n; i++) cin >> h[i];
     ;
     for (int i = 0; i < n - 1; i++) {
       int a, b;
       cin >> a >> b;
       a--;
       b--;
       v[a].push_back(b);
       v[b].push_back(a);
     }
     dfs(0);
     cout << (flag ? 'YES' : 'NO') << endl;
     flag = true;
     for (int i = 0; i < n; i++) v[i].clear();
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int i = 0, j = 0, a;
   string s, t;
   cin >> s >> t;
   a = t.length();
   while (a--) {
     if (t[j] == s[i]) {
       i++;
     }
     j++;
   }
   cout << i + 1;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long lca(long long a, long long b) {
   while (a != b) {
     if (a < b)
       b /= 2;
     else
       a /= 2;
   }
   return a;
 }
 long long inf = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
 int main() {
   ios_base::sync_with_stdio(0);
   int t;
   cin >> t;
   map<long long, long long> M;
   while (t-- > 0) {
     int x;
     cin >> x;
     long long u, v, w;
     cin >> u >> v;
     if (x == 1) {
       cin >> w;
       long long y = lca(u, v);
       while (u != y) {
         M[u] += w;
         u /= 2;
       }
       while (v != y) {
         M[v] += w;
         v /= 2;
       }
     } else {
       long long y = lca(u, v);
       long long ans = 0;
       while (u != y) {
         ans += M[u];
         u /= 2;
       }
       while (v != y) {
         ans += M[v];
         v /= 2;
       }
       cout << ans << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n;
 int a[100];
 int main() {
   int x = 1;
   scanf('%d', &n);
   for (int i = 1; i <= n; ++i) {
     scanf('%d', &a[i]);
     if (a[i] < a[x]) x = i;
   }
   if (n == 1 || (n == 2 && a[1] == a[2])) {
     puts('-1');
   } else {
     printf('1
%d
', x);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char comp[200005];
 vector<pair<int, int> > edge[200005];
 vector<int> father[200005];
 vector<string> ans;
 int dis[200005], n, k;
 queue<pair<int, int> > q;
 void dfs(int u) {
   if (ans.size() >= k) return;
   if (u == n + 1) {
     ans.push_back(comp + 1);
     return;
   }
   for (int i = 0; i < father[u].size(); i++) {
     comp[father[u][i]] = '1';
     dfs(u + 1);
     comp[father[u][i]] = '0';
   }
 }
 int main() {
   char a[10];
   int m;
   cin >> n >> m >> k;
   for (int i = 1; i <= m; i++) {
     int u, v;
     scanf('%d %d', &u, &v);
     edge[u].push_back(make_pair(v, i));
     edge[v].push_back(make_pair(u, i));
   }
   memset(dis, -1, sizeof(dis));
   for (int i = 1; i <= m; i++) comp[i] = '0';
   q.push(make_pair(1, 0));
   dis[1] = 0;
   while (!q.empty()) {
     pair<int, int> pa = q.front();
     q.pop();
     int u = pa.first, d = pa.second;
     for (int i = 0; i < edge[u].size(); i++) {
       int v = edge[u][i].first;
       if (dis[v] == -1) {
         dis[v] = d + 1;
         q.push(make_pair(v, d + 1));
       }
     }
   }
   for (int i = 2; i <= n; i++) {
     for (int j = 0; j < edge[i].size(); j++) {
       if (dis[i] == dis[edge[i][j].first] + 1)
         father[i].push_back(edge[i][j].second);
     }
   }
   dfs(2);
   if (ans.size() >= k) {
     cout << k << endl;
     for (int i = 0; i < k; i++) cout << ans[i] << endl;
   } else {
     cout << ans.size() << endl;
     for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   for (int i = 0; i < n; i++) {
     int s, f, t;
     cin >> s >> f >> t;
     int x = t % (2 * (m - 1));
     int y = t - x;
     int ans = 0;
     if (s == f) {
       ans = t;
     } else if (s < f) {
       if (x <= s - 1) {
         ans = y + f - 1;
       } else {
         ans = y + 2 * (m - 1) + f - 1;
       }
     } else {
       if (x <= (m - 1) + (m - s)) {
         ans = y + (m - 1) + m - f;
       } else {
         ans = y + 3 * (m - 1) + m - f;
       }
     }
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 + 5;
 int n, m, t;
 vector<int> e[maxn];
 vector<int> ans;
 stack<int> s;
 bool vis[maxn];
 int dep[maxn];
 int N;
 bool v[maxn];
 int dfs(int x, int fr) {
   vis[x] = true;
   s.push(x);
   for (auto y : e[x]) {
     if (y == fr) continue;
     if (!vis[y]) {
       dep[y] = dep[x] + 1;
       dfs(y, x);
       dep[y] = 0;
     } else if (dep[y]) {
       if (dep[x] - dep[y] + 1 >= N) {
         printf('2
');
         printf('%d
', dep[x] - dep[y] + 1);
         while (s.top() != y) {
           printf('%d ', s.top());
           s.pop();
         }
         printf('%d
', s.top());
         exit(0);
       }
     }
   }
   if (!v[x]) {
     for (auto y : e[x]) {
       v[y] = true;
     }
     ans.push_back(x);
   }
   s.pop();
   return 0;
 }
 int read() {
   int x;
   scanf('%d', &x);
   return x;
 }
 int main() {
   int i, j;
   int x, y;
   n = read();
   m = read();
   N = sqrt(n);
   while (N * N < n) N++;
   for (i = 1; i <= m; i++) {
     x = read();
     y = read();
     e[x].push_back(y);
     e[y].push_back(x);
   }
   for (i = 1; i <= n; i++) {
     if (!vis[i]) {
       dep[i] = 1;
       dfs(i, -1);
       dep[i] = 0;
     }
   }
   printf('1
');
   for (i = 1; i <= n; i++) {
     if (v[i]) continue;
     N--;
     printf('%d ', i);
     if (!N) break;
   }
   printf('
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, a[110], mx;
 const int N = 30;
 map<int, int> mem;
 int Penu(int now) {
   if (!now || mem.find(now) != mem.end()) return mem[now];
   register int ans = 0;
   register set<int> mex;
   for (register int i = 1; i < 31; i++)
     if (now >> i) mex.insert(Penu(now & ((1 << i) - 1) | (now >> i)));
   while (mex.count(ans)) ans++;
   return mem[now] = ans;
 }
 int ans;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   cout.tie(nullptr);
   cin >> n;
   mem[0] = 0;
   map<int, int> ma;
   for (register int i = 1, j; i <= n; i++) {
     cin >> j;
     for (register int r = 2, c = 0; r * r <= j; r++, c = 0) {
       while (!(j % r)) {
         c++;
         j /= r;
       }
       if (c) ma[r] |= (1 << c);
     }
     if (j > 1) ma[j] |= 2;
   }
   for (auto u : ma) ans ^= Penu(u.second);
   cout << (!ans ? 'Arpa
' : 'Mojtaba
');
   return 0;
 }


#include<bits/stdc++.h>

 #define ll long long int
 #define ld long double
 #define pb push_back
 #define fi first
 #define se second
 #define all(x) x.begin(),x.end()
 #define mem(x,y) memset(x,y,sizeof(x))
 #define pii pair<int,int>
 #define pll pair<ll,ll>
 #define INF 1e9+10
 #define INFL 1e18
 #define mod 1000000007
 //#define mod 998244353
 #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 using namespace std;

 ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;}
 ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

 //cout<< fixed << setprecision(10)
 //__builtin_popcountll();
 //|_2.4_|=2
 int vis[500005];
 vector<int>g[500005];

 int lev[500005];
 int ans[500005];

 void bfs(int s)
 {
 	queue<int>q;
 	q.push(s);
 	vis[s]=1;
 	lev[s]=0;

 	while(!q.empty())
 	{
 		int x=q.front();
 		q.pop();
 		for(int i=0;i<g[x].size();i++)
 		{

 			if(vis[g[x][i]]==0)
 			{
 				lev[g[x][i]]=lev[x]+1;
 				vis[g[x][i]]=1;
 				q.push(g[x][i]);
 			}

 		}

 	}
 }
 void dfs(int s)
 {
 	vis[s]=1;
 	for(auto x:g[s])
 	{
 		if(lev[s]>=lev[x])
 			continue;

 		dfs(x);

 		ans[s]=min(ans[s],ans[x]);
 	}
 }

 int main(){

 	fast;
 	int T=1;
 	cin>>T;
 	while(T--)
 	{
 		int n,i,j,m;
 		cin>>n>>m;
 		vector<pii>v;
 		for(i=0;i<m;i++)
 		{
 			int r,c;
 			cin>>r>>c;
 			v.pb({c,r});
 		}
 		sort(all(v));
 		int l=0,r;
 		if(m%2==1)
 		{
 			cout<<'NO
';
 			continue;
 		}
 		for(i=0;i+1<m;i+=2)
 		{
 			if(v[i].fi==v[i+1].fi)
 			{
 				continue;

 			}
 			else
 			{
 				if(i+2<m&&v[i+1].fi==v[i+2].fi)
 				{
 					l=1;
 					break;
 				}
 				if((v[i].se==v[i+1].se&&(v[i+1].fi-v[i].fi-1)%2==0)||(v[i].se!=v[i+1].se&&(v[i+1].fi-v[i].fi-1)%2==1))
 				{
 					continue;

 				}
 				else
 				{
 					l=1;
 					break;
 				}

 			}
 		}
 		if(l)
 		{
 			cout<<'NO
';
 		}
 		else
 		{
 			cout<<'YES
';
 		}

 	}
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   int n;
   string s, t;
   cin >> n >> s >> t;
   string ss = s, tt = t;
   sort(ss.begin(), ss.end());
   sort(tt.begin(), tt.end());
   if (ss != tt)
     cout << -1 << '
';
   else {
     vector<int> ans;
     for (int j = (int)(n - 1); j >= int(0); j--) {
       char target = t[j];
       ;
       int pos = n - j - 1;
       ;
       while (target != s[pos]) pos++;
       ;
       ;
       ans.push_back(n);
       reverse(s.begin(), s.end());
       ;
       ans.push_back(pos);
       string temp(s.begin() + n - pos, s.end());
       reverse(temp.begin(), temp.end());
       s.erase(s.begin() + n - pos, s.end());
       s.insert(s.begin(), temp.begin(), temp.end());
       ;
       ans.push_back(1);
       char c = s[n - 1];
       s.erase(s.begin() + n - 1);
       s.insert(s.begin(), c);
       ;
     }
     cout << int((ans).size()) << '
';
     for (int i = 0; i < int(int((ans).size())); i++) {
       if (i) cout << ' ';
       cout << ans[i];
     }
     cout << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int MOD = 1000000000 + 7;
 long long int arr[200005];
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   long long int t;
   cin >> t;
   while (t--) {
     long long int n, sum = 0;
     cin >> n;
     for (long long int i = 0; i < n; i++) cin >> arr[i];
     long long int i = 0;
     vector<long long int> vec;
     while (1) {
       long long int maxp = INT_MIN;
       long long int maxn = INT_MIN;
       for (; i < n; i++) {
         if (arr[i] < 0)
           break;
         else {
           maxp = max(maxp, arr[i]);
         }
       }
       if (maxp != INT_MIN) vec.push_back(maxp);
       if (i == n) break;
       for (; i < n; i++) {
         if (arr[i] >= 0)
           break;
         else {
           maxn = max(maxn, arr[i]);
         }
       }
       if (maxn != INT_MIN) vec.push_back(maxn);
       if (i == n) break;
     }
     for (long long int i = 0; i < vec.size(); i++) sum += vec[i];
     cout << sum << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long maxn = 2e5 + 5;
 int n, k, a[maxn], ans[maxn], pos[maxn];
 class Node {
  public:
   int l, r;
 };
 Node lis[maxn];
 int main() {
   scanf('%d%d', &n, &k);
   for (int(i) = 0; (i) < (n); (i)++) {
     cin >> a[i + 1];
     pos[a[i + 1]] = i + 1;
     lis[i + 1].l = i;
     lis[i + 1].r = i + 2;
   }
   int team = 1;
   for (int i = n; i >= 1; i--) {
     int p = pos[i];
     if (ans[p] != 0) continue;
     ans[p] = team;
     for (int(j) = 0; (j) < (k); (j)++) {
       int el = lis[p].r;
       if (el != n + 1) {
         ans[el] = team;
         lis[lis[el].l].r = lis[el].r;
         lis[lis[el].r].l = lis[el].l;
       } else
         break;
     }
     for (int(j) = 0; (j) < (k); (j)++) {
       int el = lis[p].l;
       if (el != 0) {
         ans[el] = team;
         lis[lis[el].l].r = lis[el].r;
         lis[lis[el].r].l = lis[el].l;
       } else
         break;
     }
     lis[lis[p].l].r = lis[p].r;
     lis[lis[p].r].l = lis[p].l;
     if (team == 1)
       team = 2;
     else
       team = 1;
   }
   for (int(i) = 0; (i) < (n); (i)++) cout << ans[i + 1];
   cout << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline bool isLow(char ch) { return (ch >= 'a' && ch <= 'z'); }
 inline bool isUpp(char ch) { return (ch >= 'A' && ch <= 'Z'); }
 inline bool isDig(char ch) { return (ch >= '0' && ch <= '9'); }
 inline bool are_equal(double a, double b) {
   return fabs(a - b) < numeric_limits<double>::epsilon();
 }
 inline bool less_than(double a, double b) {
   return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) *
                     numeric_limits<double>::epsilon());
 }
 inline bool greater_than(double a, double b) {
   return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) *
                     numeric_limits<double>::epsilon());
 }
 template <typename T, typename... args>
 inline string arrStr(T arr, int n) {
   stringstream s;
   s << '[ ';
   for (int i = 0; i < n - 1; i++) s << arr[i] << ', ';
   s << arr[n - 1] << ' ]';
   return s.str();
 }
 inline void __evars_begin(int line) { cout << '#' << line << ': '; }
 template <typename T>
 inline void __evars_out_var(vector<T> val) {
   cout << arrStr(val, val.size());
 }
 template <typename T>
 inline void __evars_out_var(T *val) {
   cout << arrStr(val, 10);
 }
 template <typename T>
 inline void __evars_out_var(T val) {
   cout << val;
 }
 inline void __evars(vector<string>::iterator it) { cout << endl; }
 template <typename T, typename... Args>
 inline void __evars(vector<string>::iterator it, T a, Args... args) {
   cout << it->substr((*it)[0] == ' ', it->length()) << ' = ';
   __evars_out_var(a);
   cout << '; ';
   __evars(++it, args...);
 }
 void in(int &n, int a[]) {
   cin >> n;
   for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i];
 }
 void in(int &n, vector<int> &a) {
   cin >> n;
   a.resize(n);
   for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i];
 }
 void out(int n, int a[]) {
   for (__typeof(n - 1) i = (0); i <= (n - 1); i++)
     cout << a[i] << '
'[i + 1 == n];
 }
 void out(vector<int> &a) {
   for (__typeof(a.size() - 1) i = (0); i <= (a.size() - 1); i++)
     cout << a[i] << '
'[i + 1 == a.size()];
 }
 template <typename T>
 T power(T val, T p, T mod) {
   val %= mod;
   T result = 1;
   while (p > 0) {
     if (p & 0x1) result = (result * val) % mod;
     val = (val * val) % mod;
     p >>= 1;
   }
   return result;
 }
 vector<string> split(const string &s, char c) {
   vector<string> v;
   stringstream ss(s);
   string x;
   while (getline(ss, x, c)) v.emplace_back(x);
   return move(v);
 }
 template <typename T>
 T gcd(T a, T b) {
   return b == 0 ? a : gcd(b, a % b);
 }
 template <typename T>
 T lcm(T a, T b) {
   return (a * b) / gcd(a, b);
 }
 template <typename T>
 string toString(T x) {
   stringstream ss;
   ss << x;
   return ss.str();
 }
 template <typename T>
 T stringToT(string s, T a) {
   T p;
   istringstream ss(s);
   ss >> p;
   return p;
 }
 template <typename T>
 T SOD(T n) {
   __typeof(n) sum = 0, i = 1;
   for (; i * i <= n; i++)
     sum += (n % i) ? 0 : ((((i) * (i)) == n) ? i : i + n / i);
   return sum;
 }
 template <typename T, typename S>
 ostream &operator<<(ostream &os, const pair<T, S> &v) {
   os << '(' << v.first << ', ' << v.second << ')';
   return os;
 }
 template <typename T, typename S>
 ostream &operator<<(ostream &os, const map<T, S> &v) {
   for (auto it : v) os << it.first << ' : ' << it.second << '
';
   return os;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const set<T> &v) {
   os << '[';
   for (auto it : v) {
     os << it;
     if (it != *v.rbegin()) os << ', ';
   }
   os << ']';
   return os;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const vector<T> &v) {
   os << '[';
   for (int i = 0; i < v.size(); i++) {
     os << v[i];
     if (i != (int)v.size() - 1) os << ', ';
   }
   os << ']';
   return os;
 }
 const int fx[] = {+1, -1, +0, +0};
 const int fy[] = {+0, +0, +1, -1};
 const int fx8[] = {+0, +0, +1, -1, -1, +1, -1, +1};
 const int fy8[] = {-1, +1, +0, +0, +1, +1, -1, -1};
 const int kx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
 const int ky[] = {-1, 1, -2, 2, -2, 2, -1, 1};
 int Set(int N, int pos) { return N = N | (1 << pos); }
 int reset(int N, int pos) { return N = N & ~(1 << pos); }
 bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
 const int mod = 1e9 + 7;
 const int mx = 2 * 100001;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   long long x, y;
   int cases, tc;
   cin >> tc;
   for (cases = 1; cases <= tc; cases++) {
     cin >> x >> y;
     long long d = x - y;
     if (d == 1)
       cout << 'NO
';
     else
       cout << 'YES
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 using namespace std;
 template <class T>
 using V = vector<T>;
 template <typename T>
 using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
 long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
 long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
 long long int pom(long long int a, long long int n, int m) {
   long long int x = 1;
   for (a %= m; n; n /= 2) n& 1 ? x = x * a % m : 0, a = a * a % m;
   return x;
 }
 int dx[4] = {-1, 0, 1, 0};
 int dy[4] = {0, -1, 0, 1};
 int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
 int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
 long long int cmp1(pair<pair<long long int, long long int>, long long int> a,
                    pair<pair<long long int, long long int>, long long int> b) {
   return a.first.second > b.first.second;
 }
 long long int cmp2(pair<long long int, long long int> a,
                    pair<long long int, long long int> b) {
   if (a.second != b.second)
     return a.second < b.second;
   else
     return a.first > b.first;
 }
 int main(int argc, char* argv[]) {
   cin.tie(0);
   ios::sync_with_stdio(false);
   long long int T;
   cin >> T;
   while (T--) {
     long long int n;
     cin >> n;
     V<long long int> a(n + 1);
     for (long long int i = 0; i < n; i++) {
       cin >> a[i];
     }
     long long int ans = 0;
     bool abc = 0;
     long long int cnt = 0;
     for (long long int i = 1; i < n; i++) {
       if (a[i - 1] == 1 && a[i] == 0) {
         abc = 1;
         cnt = 1;
       } else if (abc == 1 && a[i] == 0) {
         cnt++;
       }
       if (abc == 1 && (a[i] == 0 && a[i + 1] == 1 && i < n - 1)) {
         ans += cnt;
         cnt = 0;
         abc = 0;
       }
     }
     cout << ans << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = 0x3f3f3f3f;
 const double eps = 1e-6;
 const double PI = acos(-1);
 const double R = 0.57721566490153286060651209;
 const int maxn = 1005;
 struct Edge {
   int from, to, cap, flow, cost;
   Edge(int u, int v, int c, int f, int cc)
       : from(u), to(v), cap(c), flow(f), cost(cc) {}
 };
 struct MCMF {
   int n, m;
   vector<Edge> edges;
   vector<int> G[maxn];
   int inq[maxn];
   int d[maxn];
   int p[maxn];
   int a[maxn];
   void init(int n) {
     this->n = n;
     for (int i = 0; i <= n; ++i) G[i].clear();
     edges.clear();
   }
   void add(int from, int to, int cap, int cost) {
     edges.emplace_back(Edge(from, to, cap, 0, cost));
     edges.emplace_back(Edge(to, from, 0, 0, -cost));
     m = int(edges.size());
     G[from].emplace_back(m - 2);
     G[to].emplace_back(m - 1);
   }
   bool spfa(int s, int t, int &flow, int &cost) {
     for (int i = 1; i <= n; ++i) d[i] = INF;
     memset(inq, 0, sizeof(inq));
     d[s] = 0;
     inq[s] = 1;
     p[s] = 0;
     queue<int> q;
     a[s] = INF;
     q.push(s);
     while (!q.empty()) {
       int u = q.front();
       q.pop();
       inq[u] = 0;
       for (int i = 0; i < int(G[u].size()); ++i) {
         Edge &e = edges[G[u][i]];
         if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
           d[e.to] = d[u] + e.cost;
           p[e.to] = G[u][i];
           a[e.to] = min(a[u], e.cap - e.flow);
           if (!inq[e.to]) {
             q.push(e.to);
             inq[e.to] = 1;
           }
         }
       }
     }
     if (d[t] == INF) return false;
     flow += a[t];
     cost += d[t] * a[t];
     for (int u = t; u != s; u = edges[p[u]].from) {
       edges[p[u]].flow += a[t];
       edges[p[u] ^ 1].flow -= a[t];
     }
     return true;
   }
   int MincostMaxflow(int s, int t, int &cost) {
     int flow = 0;
     cost = 0;
     while (spfa(s, t, flow, cost))
       ;
     return flow;
   }
 } mcmf;
 int n, m, mincost;
 void solve() {
   string S;
   cin >> S;
   cin >> n;
   map<int, int> num, mp[n + 1];
   vector<int> a(n + 1);
   for (int i = 0; i < S.length(); i++) {
     num[S[i] - 'a' + 1]++;
   }
   for (int i = 1; i <= n; i++) {
     string T;
     cin >> T >> a[i];
     for (int j = 0; j < T.length(); j++) {
       mp[i][T[j] - 'a' + 1]++;
     }
   }
   int s = 0, t = n + 26 + 1;
   mcmf.init(t);
   for (int i = 1; i <= 26; i++) {
     if (!num[i]) continue;
     mcmf.add(s, i, num[i], 0);
     for (int j = 1; j <= n; j++) {
       if (!mp[j][i]) continue;
       mcmf.add(i, j + 26, mp[j][i], j);
     }
   }
   for (int i = 1; i <= n; i++) {
     mcmf.add(i + 26, t, a[i], 0);
   }
   int maxflow = mcmf.MincostMaxflow(s, t, mincost);
   if (maxflow == S.length())
     cout << mincost << '
';
   else
     cout << -1 << '
';
 }
 signed main() {
   ios_base::sync_with_stdio(false);
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 1e6 + 5, mod = 1e9 + 7;
 long long gcd(long long x, long long y) {
   if (y == 0) return x;
   return gcd(y, x % y);
 }
 int main() {
   int t;
   cin >> t;
   while (t--) {
     int n, x, y, d;
     cin >> n >> x >> y >> d;
     if (x == y) {
       cout << 0 << '
';
       continue;
     } else if (x < y) {
       int mx = 1e9 + 5;
       int ans1 = mx, ans2 = mx, ans3 = mx;
       if ((y - x) % d == 0) ans1 = (y - x) / d;
       if ((y - 1) % d == 0)
         ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d;
       if ((n - y) % d == 0)
         ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d;
       int ans = min(min(ans1, ans2), ans3);
       cout << ((ans != mx) ? ans : -1) << '
';
     } else {
       int mx = 1e9 + 5;
       int ans1 = mx, ans2 = mx, ans3 = mx;
       if ((x - y) % d == 0) ans1 = (x - y) / d;
       if ((y - 1) % d == 0)
         ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d;
       if ((n - y) % d == 0)
         ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d;
       int ans = min(min(ans1, ans2), ans3);
       cout << ((ans != mx) ? ans : -1) << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<long long> g[200000];
 long long c[200000];
 long long cnt;
 void dfs(long long v, long long p = -1) {
   cnt++;
   long long cnt1 = cnt;
   for (long long to : g[v]) {
     if (to != p) {
       dfs(to, v);
     }
   }
   c[v] = cnt - cnt1 + 1;
 }
 signed main() {
   long long t;
   cin >> t;
   for (long long q = 0; q < t; q++) {
     long long k;
     cin >> k;
     k *= 2;
     vector<pair<long long, long long>> edge;
     long long t[k];
     for (long long i = 0; i < k - 1; i++) {
       long long a, b;
       cin >> a >> b >> t[i];
       a--;
       b--;
       g[a].push_back(b);
       g[b].push_back(a);
       edge.push_back({a, b});
     }
     dfs(0);
     long long mnsum = 0, mxsum = 0;
     for (long long i = 0; i < k - 1; i++) {
       long long c1 = min(c[edge[i].first], c[edge[i].second]);
       mnsum += c1 % 2 * t[i];
       mxsum += min(c1, k - c1) * t[i];
     }
     cout << mnsum << ' ' << mxsum << '
';
     for (long long i = 0; i < k; i++) {
       g[i].clear();
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 410;
 const int inf = 0x7fffffff;
 int a[maxn];
 int n;
 int main() {
   int ans = inf;
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> a[i];
   for (int i = 2; i <= n; i++)
     for (int j = i; j <= n; j++) {
       int x = a[i - 1] - 1, y = 0;
       for (int k = i; k <= j; k++) {
         int t = x / a[k];
         y += t;
         x -= t * a[k];
       }
       x = a[i - 1] - 1 - x + a[j];
       y++;
       if (x < ans) {
         int o = x;
         for (int k = 1; k <= n; k++) {
           int t = x / a[k];
           y -= t;
           x -= t * a[k];
         }
         if (y < 0) ans = o;
       }
     }
   if (ans == inf)
     puts('-1');
   else
     printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int prime[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
 int main() {
   int n;
   cin >> n;
   vector<int> res;
   int limit = 2 * n * n;
   for (int i = 0; i < 15; i++) {
     res.clear();
     res.push_back(1);
     for (int j = 0; j <= i; j++) {
       int p = prime[j];
       int sz = res.size();
       for (int k = 0; k < sz; k++) {
         int x = res[k];
         while (x * p <= limit) {
           x *= p;
           res.push_back(x);
         }
       }
     }
     if (res.size() >= n) {
       break;
     }
   }
   sort(res.begin(), res.end(), greater<int>());
   for (int i = 0; i < n; i++) {
     cout << res[i] << ' ';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long nax = 1e5 + 7;
 const long long mod = 1e9 + 7;
 const long long inf = numeric_limits<long long>::max();
 long long n, m;
 bool check(vector<long long> &vec) {
   vector<long long> freq(n + 1, 0);
   for (long long i = 0; i < n; i++) {
     if (vec[i] > n) return false;
     freq[vec[i]]++;
     if (freq[vec[i]] > 1) {
       return false;
     }
   }
   for (long long i = 0; i < n; i++) {
     if (freq[i + 1] != 1) return false;
   }
   return true;
 }
 void test() {
   cin >> n;
   vector<long long> vec;
   vec.push_back(0);
   long long first = 0;
   for (long long i = 1; i < n; i++) {
     long long t;
     cin >> t;
     vec.push_back(vec.back() + t);
     first = min(first, vec.back());
   }
   first = 1 - first;
   for (long long &x : vec) x += first;
   if (check(vec))
     for (long long x : vec) cout << x << ' ';
   else
     cout << -1;
 }
 signed main() {
   ios_base ::sync_with_stdio(false);
   cin.tie(NULL);
   long long T = 1;
   while (T--) {
     test();
     cout << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int test;
   cin >> test;
   while (test--) {
     int n;
     cin >> n;
     vector<vector<int>> v(n, vector<int>(n, 0));
     for (int i = 0; i < n; i++) {
       v[i][i] = 1;
       v[(i + 1) % n][i] = 1;
     }
     for (int i = 0; i < n; i++) {
       for (int j = 0; j < n; j++) cout << v[i][j] << ' ';
       cout << '
';
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int inf = (long long int)1e18 + 19;
 const long long int mod = (long long int)1e9 + 7;
 void cazz() {
   long long int t = 1, i;
   long double j, n;
   cin >> t;
   while (t--) {
     cin >> n;
     i = 3, j = -1;
     while (j < n) {
       j = (((long double)180 * (i - 2)) / i);
       i++;
     }
     if (j == n) {
       cout << 'YES
';
       continue;
     }
     cout << 'NO
';
   }
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   ;
   cazz();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1000 * 1000 + 10;
 vector<int> vec[N];
 vector<int> d[2];
 bool mark[N];
 void dfs(int v, int hh) {
   mark[v] = true;
   d[hh].push_back(v);
   for (int u : vec[v])
     if (!mark[u]) dfs(u, 1 - hh);
 }
 int main() {
   int o;
   cin >> o;
   while (o--) {
     int n, m;
     cin >> n >> m;
     for (int i = 0; i < n; i++) vec[i].clear(), mark[i] = false;
     ;
     d[0].clear();
     d[1].clear();
     for (int i = 0; i < m; i++) {
       int a, b;
       cin >> a >> b;
       a--, b--;
       vec[a].push_back(b);
       vec[b].push_back(a);
     }
     dfs(0, 0);
     if ((int)d[0].size() > (int)d[1].size()) swap(d[0], d[1]);
     cout << (int)d[0].size() << endl;
     for (int a : d[0]) cout << a + 1 << ' ';
     cout << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 const int inf = 1000000007;
 const double pi = 2 * acos(0.0);
 using namespace std;
 int gcd(int a, int b) {
   a = abs(a);
   b = abs(b);
   while (b) {
     a = a % b;
     swap(a, b);
   }
   return a;
 }
 int main() {
   map<long long, int> m;
   long long n, k;
   scanf('%lld%lld', &n, &k);
   long long arr[n];
   for (long long i = 0; i < n; i++) {
     scanf('%lld', &arr[i]);
     m.insert(make_pair(arr[i], 1));
   }
   vector<long long> ans;
   long long sum = 0;
   for (int i = 1; i < 1e9; i++) {
     if (sum > k) {
       ans.pop_back();
       break;
     }
     if (m[i] != 1) {
       sum += i;
       ans.push_back(i);
     }
   }
   printf('%d
', ans.size());
   for (long long i : ans) {
     printf('%d ', i);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 500005;
 int ult;
 int cont;
 int prim;
 char cad[MAXN];
 bool Solve(int a, int n, int T) {
   int c = 0, time = 0;
   queue<int> s;
   prim = -1;
   for (int i = 0; i < n && c < cont; i++, time++) {
     if (cad[i] == 'H' && a > 0) {
       c++;
       a--;
       if (prim == -1) prim = i;
     } else if (cad[i] == 'H') {
       s.push(i);
       if (prim == -1) prim = i;
     } else if (cad[i] == 'S') {
       a++;
       if (a >= s.size()) {
         if (!s.empty()) {
           int aux = abs(s.front() - i);
           time += aux;
           s.pop();
           a--;
           c++;
           while (!s.empty()) {
             c++;
             a--;
             s.pop();
           }
           if (c < cont) time += aux;
         }
       }
     }
     if (c == cont) ult = i;
   }
   time = min(time, ult + 1 + (ult - prim));
   return (time <= T && c == cont);
 }
 int main() {
   int n;
   long long int T;
   scanf('%d %lld', &n, &T);
   scanf('
%s', cad);
   for (int i = 0; i < n; i++)
     if (cad[i] == 'H') cont++;
   int ini = 0, fin = cont;
   while (ini < fin) {
     int med = (ini + fin) / 2;
     if (!Solve(med, n, T))
       ini = med + 1;
     else
       fin = med;
   }
   if (Solve(ini, n, T))
     printf('%d
', ini);
   else
     printf('-1
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long int i, j, k, m, n, a[100400], t, w, q, sum = 0;
   char s[105000];
   long double x, y;
   cin >> q;
   for (j = 0; j < q; j++) {
     cin >> n;
     cin >> s;
     if (n > 2) {
       cout << 'YES' << endl;
       cout << 2 << endl;
       cout << s[0] << ' ';
       for (i = 1; i < n; i++) cout << s[i];
       cout << endl;
     } else {
       if (s[0] - '0' < s[1] - '0') {
         cout << 'YES' << endl;
         cout << 2 << endl;
         cout << s[0] << ' ' << s[1] << endl;
       } else
         cout << 'NO' << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 long long fact(int i) {
   if (i <= 1)
     return 1;
   else
     return i * fact(i - 1);
 }
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     int arr[4];
     for (int i = 0; i < 4; i++) {
       cin >> arr[i];
     }
     int minus = arr[0] + arr[1];
     string final[4];
     if (minus % 2 == 0) {
       final[0] = 'Tidak';
       final[1] = 'Tidak';
       if (arr[2] == 0) {
         if (arr[1] == 0) {
           final[2] = 'Tidak';
         } else {
           final[2] = 'Ya';
         }
       } else {
         final[2] = 'Ya';
       }
       if (arr[3] == 0) {
         if (arr[0] == 0) {
           final[3] = 'Tidak';
         } else {
           final[3] = 'Ya';
         }
       } else {
         final[3] = 'Ya';
       }
     } else {
       final[2] = 'Tidak';
       final[3] = 'Tidak';
       if (arr[0] == 0) {
         if (arr[3] == 0) {
           final[0] = 'Tidak';
         } else {
           final[0] = 'Ya';
         }
       } else {
         final[0] = 'Ya';
       }
       if (arr[1] == 0) {
         if (arr[2] == 0) {
           final[1] = 'Tidak';
         } else {
           final[1] = 'Ya';
         }
       } else {
         final[1] = 'Ya';
       }
     }
     for (int i = 0; i < 4; i++) {
       cout << final[i] << ' ';
     }
     cout << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> a;
 void SieveOfEratosthenes(int n) {
   bool prime[n + 1];
   memset(prime, true, sizeof(prime));
   for (int p = 2; p * p <= n; p++) {
     if (prime[p] == true) {
       for (int i = p * p; i <= n; i += p) prime[i] = false;
     }
   }
   for (int p = 2; p <= n; p++)
     if (prime[p]) a.push_back(p);
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n;
   cin >> n;
   SieveOfEratosthenes(100000);
   for (int i = 2; i <= n; i++) {
     for (int j = 0; j < a.size(); j++) {
       if (i % a[j] == 0) {
         cout << j + 1 << ' ';
         break;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 void mini(T& a, T b) {
   a = min(a, b);
 }
 template <class T>
 void maxi(T& a, T b) {
   a = max(a, b);
 }
 template <class T1, class T2>
 ostream& operator<<(ostream& out, pair<T1, T2> a) {
   return out << a.first << ' ' << a.second;
 }
 template <class T>
 ostream& operator<<(ostream& out, vector<T> a) {
   for (auto& i : a) out << i << ' ';
   return out;
 }
 template <class T>
 ostream& operator<<(ostream& out, set<T> a) {
   for (auto& i : a) out << i << ' ';
   return out;
 }
 template <class T>
 ostream& operator<<(ostream& out, multiset<T> a) {
   for (auto& i : a) out << i << ' ';
   return out;
 }
 template <class T1, class T2>
 ostream& operator<<(ostream& out, map<T1, T2> a) {
   for (auto& i : a) out << i << '
';
   return out;
 }
 template <class T1, class T2>
 ostream& operator<<(ostream& out, unordered_map<T1, T2> a) {
   for (auto& i : a) out << i << '
';
   return out;
 }
 template <class T>
 ostream& operator<<(ostream& out, queue<T> a) {
   while (!a.empty()) {
     cout << a.front() << ' ';
     a.pop();
   }
   return out;
 }
 template <class T>
 ostream& operator<<(ostream& out, deque<T> a) {
   while (!a.empty()) {
     cout << a.front() << ' ';
     a.pop_front();
   }
   return out;
 }
 template <class T>
 ostream& operator<<(ostream& out, priority_queue<T> a) {
   while (!a.empty()) {
     cout << a.top() << '
';
     a.pop();
   }
   return out;
 }
 template <class T>
 void out(T a) {
   cout << a << endl;
 }
 template <class T1, class T2>
 void out(T1 a, T2 b) {
   cout << a << ' ' << b << endl;
 }
 template <class T1, class T2, class T3>
 void out(T1 a, T2 b, T3 c) {
   cout << a << ' ' << b << ' ' << c << endl;
 }
 void out() { cout << 'OK' << endl; }
 template <class T>
 void sort(vector<T>& a) {
   sort(a.begin(), a.end());
 }
 template <class T>
 void uniq(vector<T>& a) {
   sort(a);
   a.erase(unique(a.begin(), a.end()), a.end());
 }
 template <class T>
 vector<T> set_int(vector<T> a, vector<T> b) {
   sort(a);
   sort(b);
   vector<T> res;
   set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
   return res;
 }
 istream& operator>>(istream& in, __int128& a) {
   int64_t b;
   cin >> b;
   a = b;
   return in;
 }
 ostream& operator<<(ostream& out, const __int128 a) {
   unsigned __int128 b = a < 0 ? -a : a;
   char buf[128];
   char* c = end(buf);
   do {
     --c;
     *c = '0123456789'[b % 10];
     b /= 10;
   } while (b);
   if (a < 0) {
     --c;
     *c = '-';
   }
   int len = end(buf) - c;
   out.rdbuf()->sputn(c, len);
   return out;
 }
 const int N = 1e5 + 5;
 const int inf = 1e9;
 vector<int> dp, pr;
 vector<bool> used;
 vector<int> a[N];
 void Solve() {
   int n, k;
   cin >> n >> k;
   int h = 5 * sqrt(k);
   int ans = 0;
   for (int i = 1; i <= k; i++) {
     int r, c;
     cin >> r >> c;
     if (n - r < h)
       a[c - 1].push_back(n - r);
     else
       ans += 3;
   }
   dp.resize(h, 0);
   pr.resize(h, 0);
   used.resize(h, 0);
   for (int i = n - 1; i >= 0; i--) {
     for (int j = 0; j <= h - 1; j++) used[j] = 0;
     for (int j : a[i]) used[j] = 1;
     for (int& j : dp) j = inf;
     int suf = 0;
     for (int j = h - 1; j >= 0; j--) {
       if (used[j]) suf += 3;
       int cur = suf + pr[max(j - 1, 0)];
       mini(dp[j], cur);
       if (j) cur += 2 + j * (j + 1) / 2;
       mini(dp[0], cur);
     }
     for (int j = 0; j <= h - 2; j++) mini(dp[j + 1], dp[j]);
     swap(dp, pr);
   }
   ans += pr[0];
   out(ans);
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   Solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool comp(int a, int b) { return (a > b); }
 int main() {
   int a, b, n, l, t, m, x, ans, r;
   long long cek;
   scanf('%d %d %d', &a, &b, &n);
   for (int i = 1; i <= n; i++) {
     scanf('%d %d %d', &l, &t, &m);
     if (t < a + (long long)(l - 1) * b)
       printf('-1
');
     else {
       x = 1;
       ans = l;
       while (x > 0) {
         r = ans + x;
         cek = ((a + (long long)(r - 1) * b + a + (long long)(l - 1) * b) *
                (r - l + 1)) /
               2;
         cek = max(cek / m + (cek % m == 0 ? 0 : 1), a + (long long)(r - 1) * b);
         if (cek <= t)
           ans += x, x *= 2;
         else
           x /= 2;
       }
       printf('%d
', ans);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int fun(string s, int i) {
   int k = 0;
   while (s[i] == '0' & i >= 0) {
     k++;
     i--;
   }
   return k;
 }
 int main(int argc, char const *argv[]) {
   string s;
   int a, k = 0, count = 1;
   cin >> a;
   cin >> s;
   string ans = '';
   int i = s.size() - 1;
   if (s[i] == '0') {
     while (s[i] == '0') {
       ans = ans + '0';
       i--;
     }
   }
   while (i >= 0) {
     k = 0;
     if (s[i] == '1') {
       while (s[i] == '1' && i >= 0) {
         k++;
         i--;
       }
       ans = ans + char('0' + k);
     } else if (s[i] == '0') {
       int t = fun(s, i);
       i = i - t;
       while (t > 1) {
         ans = ans + '0';
         t = t - 1;
       }
     }
   }
   reverse(ans.begin(), ans.end());
   int myint1 = stoi(ans);
   cout << myint1;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAX_N = 55;
 int n, m, dp[MAX_N][MAX_N][MAX_N][MAX_N];
 char a[MAX_N][MAX_N];
 int dfs(int x1, int y1, int x2, int y2) {
   if (dp[x1][y1][x2][y2] >= 0) {
     return dp[x1][y1][x2][y2];
   }
   if (x1 == x2 && y1 == y2) {
     return dp[x1][y1][x2][y2] = a[x1][y1] == '#';
   }
   int &ans = dp[x1][y1][x2][y2];
   ans = max(x2 - x1 + 1, y2 - y1 + 1);
   for (int i = x1; i < x2; ++i) {
     ans = min(ans, dfs(x1, y1, i, y2) + dfs(i + 1, y1, x2, y2));
   }
   for (int j = y1; j < y2; ++j) {
     ans = min(ans, dfs(x1, y1, x2, j) + dfs(x1, j + 1, x2, y2));
   }
   return ans;
 }
 int main() {
   scanf('%d', &n);
   memset(dp, -1, sizeof dp);
   for (int i = 1; i <= n; ++i) {
     scanf('%s', a[i] + 1);
   }
   printf('%d
', dfs(1, 1, n, n));
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int delta = 1e9 + 5;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ;
   int x;
   int d;
   cin >> x >> d;
   vector<long long int> ans;
   long long int cur = 1;
   for (int i = 31; i >= 0; i--) {
     long long int cnt = (1LL << i);
     while (x >= cnt) {
       x -= cnt;
       for (int j = 0; j < i; j++) {
         ans.push_back(cur);
       }
       cur += d;
       ans.push_back(cur);
       cur += d;
     }
   }
   cout << ans.size() << '
';
   for (auto a : ans) cout << a << ' ';
   cout << endl;
   ;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   string str;
   cin >> str;
   int n = str.length();
   map<char, int> mp;
   for (int i = 0; i < n; i++) {
     mp[str[i]]++;
   }
   if (mp.size() % 2 == 1)
     cout << 'IGNORE HIM!';
   else
     cout << 'CHAT WITH HER!';
   cout << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long MOD = 998244353LL, INV2 = (MOD + 1) / 2;
 const int MAXN = 500;
 long long DP[MAXN + 1][MAXN + 1];
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   int N, K;
   cin >> N >> K;
   for (int k = 1; k <= min(N, K); ++k) {
     DP[k][0] = 2;
     for (int n = 1; n <= N; ++n) {
       for (int m = n - 1; m >= 0 && n - m <= k; --m) DP[k][n] += DP[k][m];
       DP[k][n] %= MOD;
     }
   }
   long long ans = 0;
   for (int k = 1; k <= N && k < K; ++k) {
     long long aux = (DP[k][N] + MOD - DP[k - 1][N]) *
                     DP[min((K + k - 1) / k - 1, N)][N] % MOD;
     aux = aux * INV2 % MOD;
     ans += aux;
     ans %= MOD;
   }
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long cubes[100001], x;
 long long my_pow(long long x) { return x * x * x; }
 pair<long long, long long> best;
 void rec(long long m, long long steps, long long subtracted) {
   if (m == 0) {
     best = max(best, make_pair(steps, subtracted));
     return;
   }
   long long x = 1;
   while (my_pow(x + 1) <= m) ++x;
   rec(m - my_pow(x), steps + 1, subtracted + my_pow(x));
   if (x - 1 >= 0)
     rec(my_pow(x) - 1 - my_pow(x - 1), steps + 1, subtracted + my_pow(x - 1));
 }
 int main() {
   long long m;
   scanf('%I64d', &m);
   rec(m, 0, 0);
   printf('%I64d %I64d', best.first, best.second);
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('Ofast')
 using namespace std;
 const long long infl = 0x3f3f3f3f3f3f3f3fLL;
 const long long infi = 0x3f3f3f3f;
 mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
 const long long mod = 1e9 + 7;
 const long long N = 1e6 + 5;
 void solve() {
   long long m;
   cin >> m;
   cout << (m / 4 + 1) % mod * ((m / 2 - m / 4 + 1) % mod + mod) % mod << '
';
 }
 signed main() {
   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
   cout << fixed << setprecision(15);
   long long t = 1;
   cin >> t;
   while (t--) solve();
   return 0;
 }
 long long powm(long long a, long long b) {
   long long res = 1;
   while (b) {
     if (b & 1) res = (res * a) % mod;
     a = (a * a) % mod;
     b >>= 1;
   }
   return res;
 }
 long long divide(long long a, long long b) {
   return (a % mod) * powm(b, mod - 2) % mod;
 }
 long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }


#include <bits/stdc++.h>
 using namespace std;
 int ABS(int a) {
   if (a < 0) return (-a);
   return a;
 }
 void input() {}
 int n, table;
 int a[55];
 double dp[55][55][55][55];
 int aim;
 double f(int idx, int sum, int k, int cnt) {
   if (idx == n) {
     if (sum != aim || cnt == 0) return 0;
     return (k * cnt * 1.0) / ((double)(n)-k);
   }
   double &res = dp[idx][sum][k][cnt];
   if (res > -0.5) return res;
   double invK = (k + 1.0) / (idx + 1.0);
   double invNK = (idx - k + 1.0) / (idx + 1.0);
   res = 0.0;
   if (sum + a[idx] <= aim) {
     res += invK * f(idx + 1, sum + a[idx], k + 1, cnt);
   }
   if (aim + a[idx] > table) cnt++;
   res += invNK * f(idx + 1, sum, k, cnt);
   return res;
 }
 int main() {
   input();
   cin >> n;
   int sum = 0;
   for (int i = (0); i < (n); ++i) {
     cin >> a[i];
     sum += a[i];
   }
   cin >> table;
   if (sum <= table) {
     printf('%d
', n);
     return 0;
   }
   double ans = 0.0;
   for (int i = (1); i < (table + 1); ++i) {
     for (int I = (0); I < (55); ++I)
       for (int j = (0); j < (55); ++j)
         for (int k = (0); k < (55); ++k)
           for (int l = (0); l < (55); ++l) dp[I][j][k][l] = -1.0;
     aim = i;
     ans += f(0, 0, 0, 0);
   }
   printf('%.12lf
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 100010;
 const int MAXK = 20;
 int times[MAXN];
 int dp[MAXN][MAXK];
 int subtree[MAXN];
 int marc[MAXN];
 int prof[MAXN];
 vector<int> graph[MAXN];
 vector<pair<int, int>> arcs;
 void init_dfs(int u) {
   marc[u] = 1;
   for (int v : graph[u]) {
     if (marc[v]) continue;
     dp[v][0] = u;
     for (int i = 1; i < MAXK; ++i) dp[v][i] = dp[dp[v][i - 1]][i - 1];
     prof[v] = prof[u] + 1;
     init_dfs(v);
   }
 }
 inline int lca(int a, int b) {
   if (prof[a] > prof[b]) swap(a, b);
   int d = prof[b] - prof[a];
   for (int i = 0; i < MAXK; ++i)
     if (d & (1 << i)) b = dp[b][i];
   if (a == b) return a;
   for (int i = MAXK - 1; i >= 0; --i)
     if (dp[a][i] != dp[b][i]) {
       a = dp[a][i];
       b = dp[b][i];
     }
   return dp[a][0];
 }
 inline void add_path(int u, int v) {
   ++times[u];
   --times[v];
 }
 inline void add_arc(int u, int v) {
   graph[u].push_back(v);
   graph[v].push_back(u);
   arcs.push_back({u, v});
 }
 void final_dfs(int u) {
   marc[u] = 2;
   subtree[u] = times[u];
   for (int v : graph[u]) {
     if (marc[v] == 2) continue;
     final_dfs(v);
     subtree[u] += subtree[v];
   }
 }
 int main() {
   int n;
   scanf('%d', &n);
   for (int i = 0; i < n - 1; ++i) {
     int u, v;
     scanf('%d%d', &u, &v);
     add_arc(u, v);
   }
   init_dfs(1);
   int k;
   scanf('%d', &k);
   for (int i = 0; i < k; ++i) {
     int u, v;
     scanf('%d%d', &u, &v);
     int w = lca(u, v);
     add_path(u, w);
     add_path(v, w);
   }
   final_dfs(1);
   for (auto [u, v] : arcs) {
     if (prof[u] < prof[v]) swap(u, v);
     printf('%d ', subtree[u]);
   }
   printf('
');
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<pair<double, double>> v[4];
 double x[3], y[3], r[3];
 void add_point(int p, double x, double y) {
   for (int t = 0; t < v[p].size(); t++)
     if (fabs(x - v[p][t].first) < 1e-7 && fabs(y - v[p][t].second) < 1e-7)
       return;
   v[p].push_back(make_pair(x, y));
 }
 int main() {
   int n, ans = 0;
   cin >> n;
   for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> r[i];
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
       double a = 2 * (x[j] - x[i]), b = 2 * (y[j] - y[i]),
              c = (a * a + b * b) / 4 + r[i] * r[i] - r[j] * r[j];
       double d = a * a + b * b, x0 = a * c / d + x[i], y0 = b * c / d + y[i],
              md = r[i] * r[i] * d - c * c;
       if (i == j || md < 0) continue;
       b *= sqrt(md) / d, a *= sqrt(md) / d;
       for (int k = 0, p[3] = {3, i, j}; k < 3; k++)
         add_point(p[k], x0 + b, y0 - a), add_point(p[k], x0 - b, y0 + a);
     }
     ans += v[i].size() ? v[i].size() : 1;
   }
   cout << ans + 2 - (v[3].size() ? v[3].size() : 1) << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long Mod = 1000000007;
 long long fp(long long a, long long k) {
   long long res = 1;
   while (k) {
     if (k & 1) res = res * a % Mod;
     a = a * a % Mod;
     k >>= 1;
   }
   return res;
 }
 int main() {
   long long n, m;
   scanf('%I64d%I64d', &n, &m);
   long long res = (m == 1 ? n + 1
                           : ((m * fp(2 * m - 1, n) - fp(m, n)) % Mod + Mod) *
                                 fp(m - 1, Mod - 2) % Mod);
   printf('%I64d', res);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long inf = 1e9 + 7;
 void solve() {
   long long n, m;
   cin >> n >> m;
   if (m == 1) {
     long long a = n * (n - 1) / 2;
     cout << a << ' ' << a << endl;
   } else {
     long long aux = n - (m - 1);
     long long maxi = aux * (aux - 1) / 2;
     long long mini = 0;
     aux = n / m;
     long long left = n - m * aux;
     if (left != 0) {
       mini = (aux + 1) * aux / 2 * left + (m - left) * (aux * (aux - 1)) / 2;
     } else {
       mini = (aux * (aux - 1)) / 2 * m;
     }
     cout << mini << ' ' << maxi << endl;
   }
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   int t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 25;
 long long n, ans[N];
 pair<int, int> arr[N];
 int main() {
   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
   cin >> n;
   for (int i = 0; i < n; i++) cin >> arr[i].first, arr[i].second = i;
   sort(arr, arr + n);
   for (int i = 0; i < n; i++) ans[arr[(i + 1) % n].second] = arr[i].first;
   for (int i = 0; i < n; i++) cout << ans[i] << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, l, r;
 long long gerar(long long n) {
   if (n == 0) {
     return 0;
   }
   return 2 * gerar(n / 2) + 1;
 }
 long long solve(long long esq, long long dir, long long l, long long r,
                 long long valor) {
   long long mid = (esq + dir) / 2;
   long long res = 0;
   if (valor == 1) {
     return 1;
   }
   if (valor == 0) {
     return 0;
   }
   if (mid >= l && mid <= r) {
     res += valor % 2ll;
   }
   if (mid - 1 >= l) {
     res += solve(esq, mid - 1, l, r, valor / 2ll);
   }
   if (mid + 1 <= r) {
     res += solve(mid + 1, dir, l, r, valor / 2ll);
   }
   return res;
 }
 int main() {
   scanf('%lld %lld %lld', &n, &l, &r);
   long long qtd = gerar(n);
   printf('%lld
', solve(1, qtd, l, r, n));
 }


#include <bits/stdc++.h>
 using namespace std;
 long long add(long long a, long long b) {
   a += b;
   if (a >= 998244353) {
     a -= 998244353;
   }
   return a;
 }
 long long sub(long long a, long long b) {
   a -= b;
   if (a < 0) {
     a += 998244353;
   }
   return a;
 }
 long long mul(long long a, long long b) { return (a * b) % 998244353; }
 void add_self(long long& a, long long b) { a = add(a, b); }
 void sub_self(long long& a, long long b) { a = sub(a, b); }
 void mul_self(long long& a, long long b) { a = mul(a, b); }
 const long long MAXN = 200010;
 long long fpower(long long x, long long y, long long p) {
   long long res = 1;
   x = x % p;
   while (y > 0) {
     if (y & 1) res = (res * x) % p;
     y = y >> 1;
     x = (x * x) % p;
   }
   return res;
 }
 int main() {
   long long x, y;
   cin >> x >> y;
   cout << fpower(2, x + y, 998244353) << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int num[500005];
 int maps[500005];
 int to[500005];
 int main() {
   ios::sync_with_stdio(0);
   int n, m;
   int l, r;
   scanf('%d%d', &n, &m);
   for (int i = 1; i <= n; i++) {
     scanf('%d', &num[i]);
     to[num[i]] = i;
   }
   for (int i = 1; i <= m; i++) {
     scanf('%d%d', &l, &r);
     if (to[r] < to[l]) {
       int tmp = r;
       r = l;
       l = tmp;
     }
     maps[to[r]] = max(to[l], maps[to[r]]);
   }
   l = 1;
   long long ans = 0;
   for (int i = 1; i <= n; i++) {
     if (maps[i] >= l) l = maps[i] + 1;
     ans += i - l + 1;
   }
   cout << ans << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     long long n, k;
     cin >> n >> k;
     long long i = 1, cnt = 0;
     while (i < k && i < n) {
       cnt++;
       i = i * 2;
     }
     if (i < n) {
       cnt += (n - i) / k;
       if ((n - i) % k) cnt++;
     }
     cout << cnt << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using UL = unsigned int;
 using ULL = unsigned long long;
 using LL = long long;
 int N;
 int CB[300000];
 int CN[300000];
 int Bbuf = 0, Nbuf = 0;
 int F(int cn, int cb) {
   int ans = 0;
   for (int i = 0; i < (N); i++) {
     ans = max(ans, abs(cn - CN[i]));
     ans = max(ans, abs(cb - CB[i]));
     ans = max(ans, abs((cn - CN[i]) - (cb - CB[i])));
   }
   return ans;
 }
 int FF(int cb) {
   int lN, rN;
   lN = -1;
   rN = 500010;
   if (cb == 0) lN = 0;
   while (lN + 2 < rN) {
     int m1N = (lN + lN + rN) / 3, m2N = (lN + rN + rN) / 3;
     if (F(m1N, cb) < F(m2N, cb))
       rN = m2N;
     else
       lN = m1N;
   }
   Nbuf = lN + 1;
   return F(Nbuf, cb);
 }
 int main() {
   cin >> N;
   for (int i = 0; i < (N); i++) {
     CB[i] = CN[i] = 0;
     string S;
     cin >> S;
     for (char c : S)
       if (c == 'B')
         CB[i]++;
       else if (c == 'N')
         CN[i]++;
   }
   int lB, rB;
   lB = -1;
   rB = 500010;
   while (lB + 2 < rB) {
     int m1B = (lB + lB + rB) / 3, m2B = (lB + rB + rB) / 3;
     if (FF(m1B) < FF(m2B))
       rB = m2B;
     else
       lB = m1B;
   }
   Bbuf = lB + 1;
   FF(Bbuf);
   printf('%d
', F(Nbuf, Bbuf));
   for (int i = 0; i < (Bbuf); i++) printf('B');
   for (int i = 0; i < (Nbuf); i++) printf('N');
   printf('
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 set<int> S;
 int n, a[500005], b[500005], q[500005], r;
 int main() {
   scanf('%d', &n);
   n *= 2;
   int tot = 0;
   for (int i = 1; i <= n; i++) {
     char c = getchar();
     while (c != '+' && c != '-') c = getchar();
     if (c == '+')
       q[++r] = ++tot, b[i] = 0;
     else {
       int x;
       scanf('%d', &x);
       b[i] = x;
       if (!r) return puts('NO'), 0;
       a[q[r]] = x;
       r--;
     }
   }
   if (r) return puts('NO'), 0;
   tot = 0;
   for (int i = 1; i <= n; i++) {
     if (!b[i]) {
       ++tot;
       S.insert(a[tot]);
     } else {
       int p = (*S.begin());
       S.erase(S.begin());
       if (p != b[i]) return puts('NO'), 0;
     }
   }
   puts('YES');
   for (int i = 1; i <= n / 2; i++) printf('%d ', a[i]);
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   scanf('%d', &t);
   while (t--) {
     int n;
     scanf('%d', &n);
     int a[n], b[n];
     for (int i = 0; i < n; i++) scanf('%d%d', &a[i], &b[i]);
     if (a[0] < b[0])
       printf('NO
');
     else {
       int f = 1;
       for (int i = 1; i < n; i++) {
         if (a[i] < a[i - 1] || b[i] < b[i - 1]) {
           f = 0;
           break;
         }
         if (a[i] - a[i - 1] < b[i] - b[i - 1]) {
           f = 0;
           break;
         }
       }
       if (f)
         printf('YES
');
       else
         printf('NO
');
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double pi = acos(-1.0);
 const int inf = (int)1e9;
 struct vert {
   int to, z, num;
 };
 struct ans {
   int x, y, z;
 };
 const int N = (int)(1e5) + 5;
 int n, m;
 vector<vert> g[N];
 vector<int> d(N, inf);
 vector<int> dp(N, inf);
 vector<int> pr(N, -1);
 vector<bool> road(N);
 void go() {
   queue<int> q;
   q.push(0);
   d[0] = 0;
   while (!q.empty()) {
     int v = q.front();
     q.pop();
     for (int i = 0; i < (int)g[v].size(); ++i) {
       int to = g[v][i].to;
       if (d[to] == inf) {
         d[to] = d[v] + 1;
         q.push(to);
       }
     }
   }
 }
 void getAns() {
   queue<int> q;
   q.push(n - 1);
   dp[n - 1] = 0;
   while (!q.empty()) {
     int v = q.front();
     q.pop();
     for (int i = 0; i < (int)g[v].size(); ++i) {
       int to = g[v][i].to;
       int z = 1 - g[v][i].z;
       if (d[to] + 1 == d[v]) {
         if (dp[to] > dp[v] + z) {
           dp[to] = dp[v] + z;
           pr[to] = v;
         }
         q.push(to);
       }
     }
   }
 }
 int main() {
   cin >> n >> m;
   vert p;
   int kol = 0;
   for (int i = 0; i < m; ++i) {
     int x, y, z;
     scanf('%d %d %d', &x, &y, &z);
     --x;
     --y;
     p.z = z;
     p.num = i;
     if (z == 0) {
       ++kol;
     }
     p.to = y;
     g[x].push_back(p);
     p.to = x;
     g[y].push_back(p);
   }
   go();
   getAns();
   vector<ans> out;
   ans a;
   int v = 0;
   for (int to = pr[0]; to != -1; to = pr[to]) {
     vert now;
     for (int k = 0; k < g[v].size(); ++k) {
       if (g[v][k].to == to) {
         now = g[v][k];
         break;
       }
     }
     road[now.num] = true;
     if (now.z == 0) {
       a.x = v + 1;
       a.y = to + 1;
       a.z = 1;
       out.push_back(a);
     }
     v = to;
   }
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < (int)g[i].size(); ++j) {
       if (road[g[i][j].num]) {
         continue;
       }
       if (g[i][j].z == 1) {
         a.x = i + 1;
         a.y = g[i][j].to + 1;
         a.z = 0;
         road[g[i][j].num] = true;
         out.push_back(a);
       }
     }
   }
   cout << out.size() << '
';
   for (int i = 0; i < out.size(); ++i) {
     cout << out[i].x << ' ' << out[i].y << ' ' << out[i].z << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int mod = (int)1e9 + 7;
 struct matrix {
   int A[2][2] = {0};
   matrix operator*(matrix B) {
     matrix C;
     for (int i = 0; i < 2; i++)
       for (int j = 0; j < 2; j++)
         for (int k = 0; k < 2; k++)
           C[i][j] = (C[i][j] + 1ll * A[i][k] * B[k][j]) % mod;
     return C;
   }
   matrix operator+(matrix B) {
     matrix C;
     for (int i = 0; i < 2; i++)
       for (int j = 0; j < 2; j++) C[i][j] = (A[i][j] + B[i][j]) % mod;
     return C;
   }
   matrix pow(int n) {
     matrix B, C = *this;
     for (int i = 0; i < 2; i++) B[i][i] = 1;
     while (n) {
       if (n & 1) B = B * C;
       C = C * C;
       n >>= 1;
     }
     return B;
   }
   int *operator[](int i) { return A[i]; }
 };
 matrix fib;
 struct segment_tree {
   struct node {
     node *node_l, *node_r;
     int l, r;
     matrix sum, mul;
   } * root;
   node *build(vector<int> &a, int l, int r) {
     node *res = new node;
     res->l = l;
     res->r = r;
     res->mul[0][0] = res->mul[1][1] = 1;
     if (l == r) {
       res->sum = fib.pow(a[l]);
       res->node_l = res->node_r = nullptr;
     } else {
       int m = (l + r) >> 1;
       res->node_l = build(a, l, m);
       res->node_r = build(a, m + 1, r);
       res->sum = res->node_l->sum + res->node_r->sum;
     }
     return res;
   }
   void build(vector<int> &a) { root = build(a, 0, a.size() - 1); }
   void push(node *root) {
     if (root->mul[1][0] + root->mul[0][0] + root->mul[0][1] + root->mul[1][1] ==
         2)
       return;
     if (root->node_l) {
       root->node_l->mul = root->node_l->mul * root->mul;
       root->node_l->sum = root->node_l->sum * root->mul;
     }
     if (root->node_r) {
       root->node_r->mul = root->node_r->mul * root->mul;
       root->node_r->sum = root->node_r->sum * root->mul;
     }
     root->mul[0][0] = root->mul[1][1] = 1;
     root->mul[1][0] = root->mul[0][1] = 0;
   }
   void update(node *root, int l, int r) {
     if (root->l > r || root->r < l) return;
     if (root->l >= l && root->r <= r) {
       root->sum = root->sum * fib;
       root->mul = root->mul * fib;
     } else {
       push(root);
       update(root->node_l, l, r);
       update(root->node_r, l, r);
       root->sum = root->node_l->sum + root->node_r->sum;
     }
   }
   void update(int l, int r) { update(root, l, r); }
   matrix query(node *root, int l, int r) {
     push(root);
     if (root->l > r || root->r < l) return matrix();
     if (root->l >= l && root->r <= r) return root->sum;
     return query(root->node_l, l, r) + query(root->node_r, l, r);
   }
   matrix query(int l, int r) { return query(root, l, r); }
 };
 int main() {
   fib[0][1] = fib[1][0] = fib[0][0] = 1;
   int n, m;
   scanf('%d %d', &n, &m);
   vector<int> v(n);
   for (int &p : v) scanf('%d', &p);
   segment_tree sg;
   sg.build(v);
   for (int i = 0; i < m; i++) {
     int t, l, r;
     scanf('%d %d %d', &t, &l, &r);
     l--, r--;
     if (t == 1) {
       int x;
       scanf('%d', &x);
       matrix tmp = fib;
       fib = fib.pow(x);
       sg.update(l, r);
       fib = tmp;
     } else {
       printf('%d
', sg.query(l, r)[0][1]);
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int a[110], b[110];
 int main() {
   int n;
   cin >> n;
   for (int i = 1; i <= n; ++i) cin >> a[i];
   sort(a + 1, a + 1 + n);
   int aa = a[n];
   cin >> n;
   for (int i = 1; i <= n; ++i) cin >> b[i];
   sort(b + 1, b + 1 + n);
   int bb = b[n];
   cout << aa << ' ' << bb << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = 0x3f3f3f3f;
 const long long LINF = 0x3f3f3f3f3f3f3f3f;
 int v[1010];
 bool compare(pair<int, int> a, pair<int, int> b) {
   if (a.second != b.second) return a.second > b.second;
   if (v[a.first] != v[b.first]) return v[a.first] < v[b.first];
   return a.first < b.first;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   int n;
   cin >> n;
   for (int i = 0; i < n; i++) cin >> v[i];
   vector<pair<int, int> > ret;
   for (int i = 0; i < n; i++) {
     for (int j = i + 1; j < n; j++) {
       if (v[j] < v[i]) ret.push_back({i, j});
     }
   }
   sort(ret.begin(), ret.end(), compare);
   cout << ret.size() << '
';
   for (auto x : ret) {
     cout << x.first + 1 << ' ' << x.second + 1 << '
';
   }
   exit(0);
 }


#include <bits/stdc++.h>
 using namespace std;
 void fast() {
   ios_base::sync_with_stdio(0);
   cin.tie(NULL), cout.tie(NULL);
 }
 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
 int lcm(int a, int b) { return a * (b / gcd(a, b)); }
 char maze[1001][1001];
 int main() {
   fast();
   int num, one = 0, two = 0, there = 0, six = 0, fou = 0;
   cin >> num;
   ;
   for (int i = 0; i < num; i++) {
     int x;
     cin >> x;
     one += bool(x == 1);
     two += bool(x == 2);
     there += bool(x == 3);
     fou += bool(x == 4);
     six += bool(x == 6);
   }
   if ((fou + six) != num / 3 || (one) != num / 3 || (two + there) != num / 3) {
     cout << -1 << endl;
     return 0;
   }
   vector<string> vs;
   for (int i = 0; i < num / 3; i++) {
     string s = '';
     s += '1';
     s += ' ';
     if (there == six && six && there) {
       s += '3', s += ' ', s += '6';
       six--, there--;
     } else if (two) {
       s += '2';
       s += ' ';
       if (fou)
         s += '4', fou--;
       else if (six)
         s += '6', six--;
       else {
         cout << -1 << endl;
         return 0;
       }
       two--;
     } else {
       cout << -1 << endl;
       return 0;
     }
     vs.push_back(s);
   }
   for (int i = 0; i < vs.size(); i++) {
     cout << vs[i] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   const int maxN = 2 * 1e5;
   int answers[maxN];
   int n, m, x, neg = 0, pos = 0;
   cin >> n >> m;
   for (int i = 0; i < n; i++) {
     cin >> x;
     if (x == -1)
       neg++;
     else
       pos++;
   }
   int li, ri, range;
   for (int i = 0; i < m; i++) {
     cin >> li >> ri;
     range = ri - li + 1;
     if (range % 2 != 0)
       answers[i] = 0;
     else if (pos >= range / 2 && neg >= range / 2) {
       answers[i] = 1;
     } else {
       answers[i] = 0;
     }
   }
   for (int i = 0; i < m; i++) {
     cout << answers[i] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 * 4;
 inline int get() {
   int x;
   cin >> x;
   return x;
 }
 inline long long getlong() {
   long long x;
   cin >> x;
   return x;
 }
 inline string getString() {
   string s;
   cin >> s;
   return s;
 }
 template <typename T>
 inline vector<T> getvector(int len) {
   vector<T> a(len);
   for (auto &it : a) cin >> it;
   return a;
 }
 inline vector<int> getvector(int len) {
   vector<int> a(len);
   for (auto &it : a) cin >> it;
   return a;
 }
 inline vector<pair<int, int>> getvector_andPairCnt(int len) {
   vector<pair<int, int>> a(len);
   int i = 1;
   for (auto &it : a) {
     it.second = i;
     i++;
     cin >> it.first;
   }
   return a;
 }
 double power_log(double power, double val) { return log(val) / log(power); }
 template <typename T>
 T sqr(T x) {
   return x * x;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const vector<T> &v) {
   for (auto it : v) os << it << ' ';
   os << '
';
   return os;
 }
 template <typename T, typename H>
 inline vector<pair<T, H>> getvector_andBiPair(int len) {
   vector<pair<T, H>> a(len);
   for (auto &it : a) {
     cin >> it.first >> it.second;
   }
   return a;
 }
 template <typename T>
 inline set<T> vector_to_set(const vector<T> &vec) {
   set<T> s;
   for (auto &it : vec) s.insert(it);
   return s;
 }
 int main() {
   auto n = getlong(), k = getlong();
   int t = get();
   if (t <= k) {
     cout << t;
   } else {
     if (t >= n) {
       cout << (n + k) - t;
     } else {
       cout << k;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void split(string &s, vector<string> &v, string &sep) {
   int current, previous = 0;
   current = s.find_first_of(sep);
   while (current != string::npos) {
     v.push_back(s.substr(previous, current - previous));
     previous = current + 1;
     current = s.find_first_of(sep, previous);
   }
   v.push_back(s.substr(previous, current - previous));
 }
 int main() {
   int n, flag = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
     int temp;
     cin >> temp;
     if (temp) {
       flag = 1;
     }
   }
   if (flag) {
     cout << 'HARD';
     cout << endl;
   } else {
     cout << 'EASY';
     cout << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int has[205][205];
 int vis[205][205];
 vector<pair<int, int> > q;
 int gcd(int x, int y) {
   if (x < y) swap(x, y);
   while (y) {
     int t = x % y;
     x = y;
     y = t;
   }
   return x;
 }
 int main() {
   int n;
   scanf('%d', &n);
   for (int i = 0; i < n; i++) {
     int x, y;
     scanf('%d%d', &x, &y);
     q.push_back(make_pair(y + 100, x + 100));
     has[x + 100][y + 100] = 1;
   }
   sort(q.begin(), q.end());
   if (n < 3) {
     printf('0
');
     return 0;
   }
   long long ans1 = 0;
   int hehe = 0;
   int L = q.size();
   for (int i = 1; i <= 200; i++) {
     for (int j = 1; j <= 200; j++) {
       if (gcd(i, j) != 1) continue;
       memset(vis, 0, sizeof(vis));
       for (int k = 0; k < L; k++) {
         int yy = q[k].first;
         int xx = q[k].second;
         if (vis[xx][yy]) continue;
         int res = 0;
         while (xx <= 200 && yy <= 200) {
           vis[xx][yy] = 1;
           res += has[xx][yy];
           xx += i;
           yy += j;
         }
         if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6;
       }
       memset(vis, 0, sizeof(vis));
       for (int k = 0; k < L; k++) {
         int yy = q[k].first;
         int xx = q[k].second;
         if (vis[xx][yy]) continue;
         int res = 0;
         while (xx >= 0 && yy <= 200) {
           vis[xx][yy] = 1;
           res += has[xx][yy];
           xx -= i;
           yy += j;
         }
         if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6;
       }
     }
   }
   for (int i = 0; i <= 200; i++) {
     int xx = i;
     int yy = 0;
     int res = 0;
     while (xx <= 200 && yy <= 200) {
       res += has[xx][yy];
       yy += 1;
     }
     if (res >= 3) {
       ans1 += (long long)res * (res - 1) * (res - 2) / 6;
     }
   }
   for (int i = 0; i <= 200; i++) {
     int xx = 0;
     int yy = i;
     int res = 0;
     while (xx <= 200 && yy <= 200) {
       res += has[xx][yy];
       xx += 1;
     }
     if (res >= 3) {
       ans1 += (long long)res * (res - 1) * (res - 2) / 6;
     }
   }
   long long ans = (long long)n * (n - 1) * (n - 2) / 6 - ans1;
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long N;
   scanf('%I64d', &N);
   int l = 1, r = 20000000, ans = 0;
   while (l <= r) {
     int mid = (l + r) >> 1;
     if ((long long)mid * (mid + 1) < N * 2)
       ans = mid, l = mid + 1;
     else
       r = mid - 1;
   }
   printf('%I64d', N - (long long)ans * (ans + 1) / 2);
   return 0;
 }


#include <bits/stdc++.h>
 #pragma comment(linker, '/STACK:96777216')
 using namespace std;
 const double NOT_FOUND = -1000000000;
 const double EPS = 0.00000001;
 const double PI = acos(-1.0);
 struct point {
   double x, y;
 };
 point make_point(double a, double b) {
   point c;
   c.x = a;
   c.y = b;
   return c;
 }
 struct line {
   double A, B, C;
 };
 line make_line(double a, double b, double c) {
   line p;
   p.A = a;
   p.B = b;
   p.C = c;
   return p;
 }
 struct circle {
   double R;
   point center;
   vector<double> intersects;
 };
 circle make_circle(point c, double r) {
   circle C;
   C.R = r;
   C.center = c;
   return C;
 }
 int main() {
   int N, K;
   cin >> N >> K;
   if (K > N) {
     cout << -1;
     return 0;
   }
   if (K == 1 && N > 1) {
     cout << -1;
     return 0;
   }
   if (K == N) {
     char p = 'a';
     for (int i = 0; i < N; i++) {
       cout << p;
       p++;
     }
     return 0;
   }
   string S;
   for (char c = 'a' + K - 1; c > 'b'; c--) {
     S += c;
     N--;
   }
   if (N % 2) {
     S += 'a';
     N--;
   }
   for (int i = 0; i < N; i += 2) S += 'ba';
   for (int i = (int)S.length() - 1; i >= 0; i--) cout << S[i];
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int prim[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
 int n;
 long long ans;
 void DFS(int a, int b, long long temp) {
   if (temp < ans && a == n) ans = temp;
   if (a >= n) return;
   for (int i = 1; i <= 64; i++) {
     if (ans / prim[b] < temp) break;
     temp *= prim[b];
     DFS(a * (i + 1), (b + 1), temp);
   }
 }
 int main() {
   while (scanf('%d', &n) != EOF) {
     ans = LONG_LONG_MAX;
     DFS(1, 0, 1);
     printf('%lld
', ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int ABS(int a) {
   if (a < 0) return (-a);
   return a;
 }
 void input() {}
 int a[100005];
 int seg[100005];
 int nxt[100005];
 int hop[100005];
 void readInt(int &n) {
   n = 0;
   char ch = getchar();
   while (!isdigit(ch)) ch = getchar();
   while (isdigit(ch)) {
     n = n * 10 + ch - 48;
     ch = getchar();
   }
 }
 int main() {
   input();
   int n, q;
   readInt(n);
   readInt(q);
   int sq = sqrt(n);
   int c = 0, cnt = 0;
   for (int i = (0); i < (n); ++i) {
     readInt(a[i]);
     seg[i] = cnt;
     c++;
     if (c == sq) c = 0, cnt++;
   }
   for (int i = (n - 1); i >= (0); --i) {
     int to = i + a[i];
     hop[i] = 1;
     if (to >= n)
       nxt[i] = -1;
     else if (seg[to] != seg[i])
       nxt[i] = to;
     else {
       nxt[i] = nxt[to];
       hop[i] += hop[to];
     }
   }
   while (q--) {
     int type;
     readInt(type);
     if (type == 0) {
       int x, y;
       readInt(x);
       readInt(y);
       x--;
       int i = x;
       a[x] = y;
       while (i >= 0) {
         if (seg[i] != seg[x]) break;
         int to = i + a[i];
         hop[i] = 1;
         if (to >= n)
           nxt[i] = -1;
         else if (seg[to] != seg[i])
           nxt[i] = to;
         else {
           nxt[i] = nxt[to];
           hop[i] += hop[to];
         }
         i--;
       }
     } else {
       int x;
       readInt(x);
       x--;
       int ans = 0;
       int last = x;
       while (x != -1) {
         ans += hop[x];
         last = x;
         x = nxt[x];
       }
       while (last + a[last] < n) last = last + a[last];
       printf('%d %d
', last + 1, ans);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int mx = 200100;
 struct node {
   int x, y, z;
 } a[mx];
 struct nod {
   int id, x;
 } b[mx];
 int fa[mx];
 long long num[mx];
 long long ans[mx];
 int n, m;
 bool cmp(node x, node y) { return x.z < y.z; }
 bool cmp1(nod x, nod y) { return x.x < y.x; }
 int find(int x) {
   if (x != fa[x]) {
     return fa[x] = find(fa[x]);
   }
   return fa[x];
 }
 int main() {
   cin >> n >> m;
   for (int i = 1; i <= n; i++) fa[i] = i, num[i] = 1;
   for (int i = 1; i < n; i++) {
     cin >> a[i].x >> a[i].y >> a[i].z;
   }
   for (int i = 1; i <= m; i++) {
     cin >> b[i].x;
     b[i].id = i;
   }
   sort(a + 1, a + n, cmp);
   sort(b + 1, b + m + 1, cmp1);
   int l = 1;
   long long sum = 0;
   for (int i = 1; i < n; i++) {
     while (l <= m && a[i].z > b[l].x) {
       ans[b[l].id] = sum;
       l++;
     }
     int x = find(a[i].x);
     int y = find(a[i].y);
     sum += num[y] * num[x];
     fa[x] = y;
     num[y] += num[x];
   }
   while (l <= m) {
     if (a[n - 1].z <= b[l].x) {
       ans[b[l].id] = sum;
     }
     l++;
   }
   for (int i = 1; i <= m; i++) cout << ans[i] << '
'[i == m];
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 int main() {
   int T;
   cin >> T;
   while (T--) {
     int n;
     cin >> n;
     long double theta = 3.14159265358979323846 / (2 * n);
     long double answer = 1 / tan(theta);
     cout << setprecision(15) << answer << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 signed main() {
   ios_base::sync_with_stdio(false);
   cin.tie();
   cout.tie();
   long long t;
   cin >> t;
   while (t--) {
     long long n;
     cin >> n;
     string a;
     cin >> a;
     string last = a;
     for (long long i = 0; i <= n; ++i) {
       string s;
       s = a;
       for (long long k = 1; k < n; ++k) {
         if (a[k - 1] == 'A') {
           s[k] = 'A';
         }
       }
       a = s;
       if (a == last) {
         cout << i << '
';
         break;
       }
       last = a;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string s;
   cin >> s;
   int n = s.length();
   vector<int> P(n, 0);
   int C = 0;
   int R = 0;
   for (int i = 1; i < n - 1; i++) {
     int mirror = 2 * C - i;
     if (R > i) {
       P[i] = min(R - i, P[mirror]);
     } else {
       P[i] = 0;
     }
     while ((i + 1 + P[i] < n) && (i - 1 - P[i] >= 0) &&
            (s[i + 1 + P[i]] == s[i - 1 - P[i]])) {
       P[i]++;
     }
     if (i + P[i] > R) {
       C = i;
       R = i + P[i];
     }
   }
   string rev(s);
   reverse(rev.begin(), rev.end());
   vector<int> T(n, 0);
   T[0] = -1;
   int pos = 2;
   int cnd = 0;
   while (pos < n) {
     if (rev[pos - 1] == rev[cnd]) {
       cnd++;
       T[pos] = cnd;
       pos++;
     } else if (cnd > 0) {
       cnd = T[cnd];
     } else {
       T[pos] = 0;
       pos++;
     }
   }
   int m = 0;
   int revI = 0;
   vector<int> found(n + 1, -1);
   found[0] = 0;
   while (revI < n && m + revI < n) {
     if (rev[revI] == s[m + revI]) {
       if (found[revI + 1] == -1) {
         found[revI + 1] = m;
       }
       revI++;
     } else {
       m = m + revI - T[revI];
       if (T[revI] > -1) {
         revI = T[revI];
       } else {
         revI = 0;
       }
     }
   }
   vector<int> fend(n + 1, -1);
   int fendMax = 0;
   for (int i = 0; i <= n; i++) {
     if (found[i] != -1) {
       fend[i] = found[i] + i;
       fendMax = i;
     }
   }
   int best = 0;
   int bestMidSt = 0;
   int bestMidL = 0;
   int bestLeftSt = 0;
   int bestSideL = 0;
   for (int i = 0; i < n; i++) {
     int midStart = i - P[i];
     int midEnd = i + P[i];
     int lLow = 0;
     int lHigh = min(n - midEnd, fendMax + 1);
     int lCur = (lLow + lHigh) / 2;
     while (lHigh - lLow > 1) {
       if (fend[lCur] <= midStart) {
         lLow = lCur;
       } else {
         lHigh = lCur;
       }
       lCur = (lLow + lHigh) / 2;
     }
     int curBest = 2 * P[i] + 1 + 2 * lCur;
     if (curBest > best) {
       best = curBest;
       bestMidSt = midStart;
       bestMidL = 2 * P[i] + 1;
       bestLeftSt = found[lCur];
       bestSideL = lCur;
     }
   }
   if (bestSideL == 0) {
     cout << 1 << endl;
     cout << (bestMidSt + 1) << ' ' << bestMidL << endl;
   } else {
     cout << 3 << endl;
     cout << (bestLeftSt + 1) << ' ' << bestSideL << endl;
     cout << (bestMidSt + 1) << ' ' << bestMidL << endl;
     cout << (n - bestSideL + 1) << ' ' << bestSideL << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool ck(int x) {
   int t[50], l = 0;
   while (x) t[l++] = x & 1, x >>= 1;
   if (l & 1) {
     int lim = l - 1 >> 1;
     for (int i = 0; i < lim; i++)
       if (t[i]) return 0;
     for (int i = lim; i < l; i++)
       if (!t[i]) return 0;
   } else
     return 0;
   return 1;
 }
 int main() {
   int n;
   cin >> n;
   for (int i = n; i >= 1; i--) {
     if (n % i == 0 && ck(i)) {
       printf('%d
', i);
       return 0;
     }
   }
 }


#include <bits/stdc++.h>
 const int N = 2010;
 std::set<std::pair<long long, int>> set;
 long long a[N][N], dis[N];
 bool vis[N];
 int n;
 int main() {
   scanf('%d', &n);
   memset(dis, 0x7f, sizeof(dis));
   long long mine = LLONG_MAX;
   for (int i = 0; i < n; ++i) {
     for (int j = i + 1; j < n; ++j) {
       scanf('%I64d', &a[i][j]);
       mine = std::min(mine, a[j][i] = a[i][j]);
     }
   }
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < n; ++j) {
       if (i == j) {
         continue;
       }
       a[i][j] -= mine;
       dis[i] = std::min(dis[i], 2 * a[i][j]);
     }
   }
   for (int i = 0; i < n; ++i) {
     set.insert({dis[i], i});
   }
   for (int i = 0; i < n; ++i) {
     std::pair<long long, int> p = *(set.begin());
     set.erase(set.begin());
     vis[p.second] = true;
     for (int j = 0; j < n; ++j) {
       if (!vis[j] && p.second != j) {
         set.erase({dis[j], j});
         dis[j] = std::min(dis[p.second] + a[p.second][j], dis[j]);
         set.insert({dis[j], j});
       }
     }
   }
   for (int i = 0; i < n; ++i) {
     printf('%I64d
', dis[i] + (n - 1) * mine);
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 #pragma GCC target('avx2')
 using namespace std;
 void solve() {
   int q;
   cin >> q;
   unordered_map<string, string> map;
   while (q--) {
     string a, b;
     cin >> a >> b;
     if (map.find(a) != map.end()) {
       string s = map[a];
       map.erase(a);
       map[b] = s;
       continue;
     }
     map[b] = a;
   }
   cout << map.size() << '
';
   for (auto i : map) {
     cout << i.second << ' ' << i.first << '
';
   }
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int t;
   t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }