#include <bits/stdc++.h>
 using namespace std;
 int n, a[100000], outpalsz, inn, b[100000];
 long long result;
 int inicount[100001], iniodd;
 int ccount[100001], codd;
 int main() {
   cin.sync_with_stdio(false);
   cin >> n;
   for (int i = 0; i < n; i++) cin >> a[i];
   for (int i = 0; i < n - 1 - i && a[i] == a[n - 1 - i]; i++) outpalsz++;
   for (int i = outpalsz; i < n - outpalsz; i++) {
     inicount[a[i]]++;
     if (inicount[a[i]] % 2)
       iniodd++;
     else
       iniodd--;
     b[inn++] = a[i];
   }
   if (inn <= 1) {
     cout << 1LL * n * (n + 1) / 2 << '
';
     return 0;
   }
   if (iniodd <= 1) result = 1LL * (outpalsz + 1) * (outpalsz + 1);
   for (int i = 0; i <= 100000; i++) ccount[i] = inicount[i];
   codd = iniodd;
   for (int i = 0; i < inn; i++) {
     ccount[b[i]] -= 2;
     if (i * 2 + 1 == inn) ccount[b[i]]++;
     if (i > inn - 1 - i && b[i] == b[inn - 1 - i]) ccount[b[i]] += 2;
     if (ccount[b[i]] < 0) break;
     if (codd <= 1) result += outpalsz + 1;
   }
   for (int i = 0; i <= 100000; i++) ccount[i] = inicount[i];
   codd = iniodd;
   for (int i = inn - 1; i >= 0; i--) {
     ccount[b[i]] -= 2;
     if (i * 2 + 1 == inn) ccount[b[i]]++;
     if (i < inn - 1 - i && b[i] == b[inn - 1 - i]) ccount[b[i]] += 2;
     if (ccount[b[i]] < 0) break;
     if (codd <= 1) result += outpalsz + 1;
   }
   cout << result << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long get(string a) {
   long long len = a.length();
   long long b = 0;
   long long cnt = 1;
   for (int i = len - 1; i >= 0; i--) {
     b += ((a[i] - '0') % 2) * cnt;
     cnt *= 2;
   }
   return b;
 }
 int p[262150];
 int main() {
   int t;
   cin >> t;
   while (t--) {
     char c;
     cin >> c;
     string a;
     cin >> a;
     long long b = get(a);
     switch (c) {
       case '+': {
         p[b]++;
         break;
       }
       case '-': {
         p[b]--;
         break;
       }
       case '?': {
         cout << p[b] << endl;
         break;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MOD = (int)1e9 + 7;
 const int INF = (int)1e9;
 const long long LINF = (long long)1e18;
 const long double PI = acos((long double)-1);
 const long double EPS = 1e-9;
 inline long long gcd(long long a, long long b) {
   long long r;
   while (b) {
     r = a % b;
     a = b;
     b = r;
   }
   return a;
 }
 inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
 inline long long fpow(long long n, long long k, int p = MOD) {
   long long r = 1;
   for (; k; k >>= 1) {
     if (k & 1) r = r * n % p;
     n = n * n % p;
   }
   return r;
 }
 template <class T>
 inline int chkmin(T& a, const T& val) {
   return val < a ? a = val, 1 : 0;
 }
 template <class T>
 inline int chkmax(T& a, const T& val) {
   return a < val ? a = val, 1 : 0;
 }
 template <class T>
 inline T isqrt(T k) {
   T r = sqrt(k) + 1;
   while (r * r > k) r--;
   return r;
 }
 template <class T>
 inline T icbrt(T k) {
   T r = cbrt(k) + 1;
   while (r * r * r > k) r--;
   return r;
 }
 inline void addmod(int& a, int val, int p = MOD) {
   if ((a = (a + val)) >= p) a -= p;
 }
 inline void submod(int& a, int val, int p = MOD) {
   if ((a = (a - val)) < 0) a += p;
 }
 inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
 inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
 const int maxn = 2000 + 10;
 const int maxm = 500000 + 10;
 int n, m;
 int a[maxm];
 int b[maxm];
 bitset<maxn << 1> mat[maxn];
 void solve() {
   cin >> n >> m;
   for (int i = (0); i < (m); i++) {
     cin >> a[i] >> b[i], a[i]--, b[i]--;
     mat[b[i]][a[i]] = 1;
   }
   for (int i = (0); i < (n); i++)
     for (int j = (0); j < (n); j++) mat[i][j + n] = i == j;
   for (int i = (0); i < (n); i++) {
     if (!mat[i][i]) {
       for (int j = (i + 1); j < (n); j++)
         if (mat[j][i]) {
           mat[i] ^= mat[j];
           break;
         }
     }
     for (int j = (0); j < (n); j++) {
       if (j == i) continue;
       if (mat[j][i]) {
         mat[j] ^= mat[i];
       }
     }
   }
   for (int i = (0); i < (m); i++) {
     if (!mat[a[i]][b[i] + n]) {
       cout <<  YES
 ;
     } else {
       cout <<  NO
 ;
     }
   }
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string str;
   cin >> str;
   int ans(0);
   for (int i = 0; i < str.size(); ++i)
     if (str[i] != str[str.size() - 1 - i]) ++ans;
   if (ans == 2 || (ans == 0 && str.size() % 2 == 1))
     cout <<  YES  << endl;
   else
     cout <<  NO  << endl;
   return 0;
 }


#include <bits/stdc++.h>
 #pragma comment(linker,  /STACK:256000000 )
 using namespace std;
 int main() {
   srand(1373737);
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n, t, k, d, q;
   cin >> n >> q;
   vector<int> arr(n);
   for (int i = 0; i < (int)q; i++) {
     cin >> t >> k >> d;
     int free = 0;
     for (int j = 0; j < (int)n; j++) {
       if (arr[j] <= t) free++;
     }
     if (free < k) {
       cout << -1 << '
';
       continue;
     }
     int res = 0;
     for (int j = 0; j < n && k; j++) {
       if (arr[j] <= t) {
         arr[j] = t + d;
         k--;
         res += j + 1;
       }
     }
     cout << res << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline bool uin(T& a, T b) {
   return a > b ? (a = b, true) : false;
 }
 template <class T>
 inline bool uax(T& a, T b) {
   return a < b ? (a = b, true) : false;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   long long N, K;
   cin >> N >> K;
   if (K == 1) {
     cout << N << '
';
     return 0;
   }
   string be, en;
   cin >> be >> en;
   long long ans = 0, cur = 1;
   for (int i = 0; i < N; ++i) {
     cur *= 2LL;
     if (be[i] == 'b') --cur;
     if (en[i] == 'a') --cur;
     uin(cur, K);
     ans += cur;
   }
   cout << ans << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 #pragma GCC optimize( O2 )
 #pragma GCC optimize( unroll-loops )
 struct POINT {
   int x, y;
   POINT() : x(0), y(0) {}
   POINT(int x, int y) : x(x), y(y) {}
   POINT operator+(const POINT &t) const { return POINT(x + t.x, y + t.y); }
   POINT operator-(const POINT &t) const { return POINT(x - t.x, y - t.y); }
   POINT operator!() const { return POINT(-y, x); }
 };
 inline long long dot(const POINT &a, const POINT &b) {
   return (long long)a.x * b.x + (long long)a.y * b.y;
 }
 inline long long cross(const POINT &a, const POINT &b) {
   return (long long)a.x * b.y - (long long)a.y * b.x;
 }
 inline long long cross(const POINT &a, const POINT &b, const POINT &c) {
   return cross(a - c, b - c);
 }
 inline bool halfPlane(const POINT &p) {
   if (p.y == 0) return p.x < 0;
   return p.y < 0;
 }
 inline bool angSortCmp(const POINT &a, const POINT &b) {
   bool ha = halfPlane(a);
   bool hb = halfPlane(b);
   if (ha != hb) return ha < hb;
   return cross(a, b) > 0;
 }
 const int N = 2000;
 int n;
 long long S;
 POINT tab[N + 7];
 int poz[N + 7];
 POINT stab[N + 7];
 struct kierunek {
   POINT wek;
   int p, q;
 };
 int main() {
   ios_base::sync_with_stdio(0);
   cin >> n >> S;
   S *= 2;
   for (int i = 1; i <= n; ++i) cin >> tab[i].x >> tab[i].y;
   sort(tab + 1, tab + n + 1, [](const POINT &a, const POINT &b) -> bool {
     if (a.x == b.x) return a.y > b.y;
     return a.x < b.x;
   });
   for (int i = 1; i <= n; ++i) {
     poz[i] = i;
     stab[i] = tab[i];
   }
   vector<kierunek> kol((n * (n - 1)) / 2);
   int it = 0;
   for (int i = 1; i <= n; ++i) {
     for (int j = i + 1; j <= n; ++j) {
       kol[it].wek = !(tab[j] - tab[i]);
       kol[it].p = i;
       kol[it].q = j;
       it++;
     }
   }
   sort(kol.begin(), kol.end(),
        [](const kierunek &a, const kierunek &b) -> bool {
          return angSortCmp(a.wek, b.wek);
        });
   for (kierunek u : kol) {
     int p = u.p;
     int q = u.q;
     swap(stab[poz[p]], stab[poz[q]]);
     swap(poz[p], poz[q]);
     int l = 1;
     int r = n;
     while (l <= r) {
       int s = (l + r) / 2;
       long long ww = cross(tab[q], stab[s], tab[p]);
       if (ww == S) {
         cout <<  Yes  << endl;
         cout << tab[p].x << ' ' << tab[p].y << endl;
         cout << tab[q].x << ' ' << tab[q].y << endl;
         cout << stab[s].x << ' ' << stab[s].y << endl;
         exit(0);
       }
       if (ww > S)
         r = s - 1;
       else
         l = s + 1;
     }
     l = 1;
     r = n;
     while (l <= r) {
       int s = (l + r) / 2;
       long long ww = cross(tab[q], stab[s], tab[p]);
       if (ww == -S) {
         cout <<  Yes  << endl;
         cout << tab[p].x << ' ' << tab[p].y << endl;
         cout << tab[q].x << ' ' << tab[q].y << endl;
         cout << stab[s].x << ' ' << stab[s].y << endl;
         exit(0);
       }
       if (ww > -S)
         r = s - 1;
       else
         l = s + 1;
     }
   }
   cout <<  No  << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   int i, j, n, m, k, l, r;
   cin >> n >> m;
   int a[n][m];
   pair<int, int> start_end[n][m];
   for (i = 0; i < n; i += 1)
     for (j = 0; j < m; j += 1) {
       cin >> a[i][j];
       start_end[i][j].first = i;
       start_end[i][j].second = i;
     }
   for (j = 0; j < m; j += 1)
     for (i = 1; i < n; i += 1)
       if (a[i][j] >= a[i - 1][j])
         start_end[i][j].first = start_end[i - 1][j].first;
   int min_l[n];
   for (i = 0; i < n; i++) min_l[i] = 100010;
   for (i = 0; i < n; i += 1)
     for (j = 0; j < m; j += 1)
       if (start_end[i][j].first < min_l[i]) min_l[i] = start_end[i][j].first;
   cin >> k;
   while (k--) {
     cin >> l >> r;
     cout << ((min_l[r - 1] <= l - 1) ?  Yes
  :  No
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 void read(T &x) {
   x = 0;
   char ch = getchar();
   long long fh = 1;
   while (ch < '0' || ch > '9') {
     if (ch == '-') fh = -1;
     ch = getchar();
   }
   while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
   x *= fh;
 }
 template <typename T>
 void write(T x) {
   if (x < 0) x = -x, putchar('-');
   if (x > 9) write(x / 10);
   putchar(x % 10 + '0');
 }
 template <typename T>
 void writeln(T x) {
   write(x);
   puts(  );
 }
 long long n, a[100005];
 signed main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   long long t;
   cin >> t;
   while (t--) {
     cin >> n;
     for (long long i = 1; i <= n; i++) cin >> a[i];
     long long x = 0;
     for (long long i = n; i >= 1; i--)
       if (a[i] < 0)
         x += -a[i];
       else if (x >= a[i])
         x -= a[i];
       else
         x = 0;
     cout << x << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int n, m, h;
   cin >> n >> m >> h;
   vector<int> ms(m), ns(n);
   vector<vector<int> > ok(n, vector<int>(m));
   for (int i = 0; i < m; i++) {
     cin >> ms[i];
   }
   for (int i = 0; i < n; i++) {
     cin >> ns[i];
   }
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       cin >> ok[i][j];
     }
   }
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       if (ok[i][j] == 0) {
         cout << 0 <<    ;
       } else {
         cout << min(ms[j], ns[i]) <<    ;
       }
     }
     cout << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int r, g, j, f[200001], sc, kq, mod = round(1e9) + 7;
 long long i, h;
 int main() {
   cin >> r >> g;
   f[0] = 1;
   for (i = 1; i * (i + 1) / 2 <= r + g; i++) {
     for (j = r; j >= 0; j--) {
       if (j >= i) f[j] = (f[j] + f[j - i]) % mod;
     }
   }
   h = i - 1;
   for (i = 0; i <= r; i++)
     if (h * (h + 1) / 2 - i <= g) kq = (kq + f[i]) % mod;
   cout << kq;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 + 7;
 vector<pair<int, int> > s;
 int co[maxn], deg[maxn];
 int main() {
   int n;
   cin >> n;
   for (int i = 0; i < n - 1; i++) {
     pair<int, int> q;
     cin >> q.first >> q.second;
     s.push_back(q);
   }
   for (int i = 1; i <= n; i++) {
     cin >> co[i];
   }
   int x = 0;
   for (auto i : s) {
     if (co[i.first] != co[i.second]) {
       deg[i.first]++;
       deg[i.second]++;
       x++;
     }
   }
   int flag = 0;
   for (int i = 1; i <= n; i++) {
     if (deg[i] == x) {
       flag = i;
     }
   }
   if (flag) {
     printf( YES
%d
 , flag);
   } else {
     printf( NO
 );
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void solve() {
   long long n, k, kol = 0;
   cin >> n >> k;
   string s;
   cin >> s;
   vector<long long> a(n);
   for (long long i = 0; i < n; i++) {
     a[i] = s[i] - '0';
   }
   for (long long i = 0; i < n - 1 && k; i++) {
     if (a[i] == 4 && a[i + 1] == 7) {
       if (i % 2 == 0) {
         a[i + 1] = 4;
         k--;
       } else {
         if (i > 0 && a[i - 1] == 4) {
           if (k % 2 == 1) {
             a[i] = 7;
           }
           break;
         }
         a[i] = a[i + 1] = 7;
         k--;
       }
     }
   }
   for (long long i = 0; i < n; i++) {
     cout << a[i];
   }
 }
 signed main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   long long t = 1;
   while (t--) {
     solve();
   }
 }


#include <bits/stdc++.h>
 #pragma warning(disable : 4996)
 using namespace std;
 int n, l;
 long long a[300000][3];
 long long b[300000][3];
 long long t[300000][2];
 vector<int> c[3000000];
 vector<pair<long long, long long> > g;
 long long p[3000000];
 long long dab[3000000][2];
 void sayno() {
   printf( NO );
   exit(0);
 }
 void calc() {
   int i, j, k;
   long long d = -1;
   for (i = 0; i < l; i++) {
     if (c[i].size() >= 2) {
       d = (t[c[i][1]][1] - t[c[i][0]][1]) /
           ((t[c[i][1]][0] - t[c[i][0]][0]) / l);
       break;
     }
   }
   if (d == -1) {
     int past = 0;
     g.clear();
     for (i = 1; i <= l; i++) {
       if (!c[i % l].size()) continue;
       int x = c[past][0], y = c[i % l][0];
       if (t[x][0] < t[y][0]) {
         long long u = (t[y][0] - t[x][0]) / l;
         long long v = t[y][1] - t[x][1];
         long long w = (t[y][0] - t[x][0]) % l;
         if (u != 0)
           g.push_back(pair<long long, long long>((v - w + u - 1) / u, v / u));
       } else {
         long long u = (t[x][0] - t[y][0]) / l + 1;
         long long v = t[x][1] - t[y][1];
         long long w = t[y][0] + u * l - t[x][0];
         if (u != 0)
           g.push_back(pair<long long, long long>((v + u - 1) / u, (v + w) / u));
       }
       past = i;
     }
     sort(g.begin(), g.end());
     for (auto e : g) {
       if (e.second < g.back().first) sayno();
     }
     d = g.back().first;
   }
   memset(p, -1, sizeof(p));
   for (i = 0; i < n; i++) {
     long long u = t[i][1] - d * (t[i][0] / l);
     long long v = t[i][0] % l;
     if (p[v] == -1)
       p[v] = u;
     else if (p[v] != u)
       sayno();
   }
   p[l] = d;
   int past = 0;
   for (i = 1; i <= l; i++) {
     if (p[i] == -1) continue;
     if (p[i] - p[past] < 0 || p[i] - p[past] > i - past) sayno();
     for (j = 0; j < p[i] - p[past]; j++) {
       dab[past + j][1] = 1;
     }
     for (j = p[i] - p[past]; j < i - past; j++) {
       dab[past + j][1] = -1;
     }
     past = i;
   }
 }
 int main() {
   int i, j, k;
   cin >> n >> l;
   for (i = 0; i <= n; i++) {
     if (i != 0) scanf( extraextraextra , &a[i][0], &a[i][1], &a[i][2]);
     b[i][0] = a[i][0], b[i][1] = a[i][1] + a[i][2], b[i][2] = a[i][1] - a[i][2];
     if ((b[i][1] + b[i][0]) % 2 == 1 || (b[i][2] + b[i][0]) % 2 == 1) sayno();
     b[i][1] = (b[i][1] + b[i][0]) / 2, b[i][2] = (b[i][2] + b[i][0]) / 2;
     c[b[i][0] % l].push_back(i);
   }
   n++;
   for (i = 0; i < n; i++) t[i][0] = b[i][0], t[i][1] = b[i][1];
   calc();
   for (i = 0; i < l; i++) dab[i][0] = dab[i][1];
   for (i = 0; i < n; i++) t[i][0] = b[i][0], t[i][1] = b[i][2];
   calc();
   for (i = 0; i < l; i++) {
     if (dab[i][0] == -1 & dab[i][1] == -1)
       printf( L );
     else if (dab[i][0] == 1 && dab[i][1] == 1)
       printf( R );
     else if (dab[i][0] == -1 && dab[i][1] == 1)
       printf( D );
     else
       printf( U );
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<vector<long long> > matmul(vector<vector<long long> > &a,
                                   vector<vector<long long> > &b) {
   int i, j, k, n = a.size();
   vector<vector<long long> > ans(n, vector<long long>(n));
   for (i = 0; i < n; i++)
     for (j = 0; j < n; j++) {
       long long temp = 0;
       for (k = 0; k < n; k++)
         temp = (temp + a[i][k] * b[k][j]) % (1000000007LL);
       ans[i][j] = temp;
     }
   return ans;
 }
 vector<vector<long long> > matpwr(vector<vector<long long> > &a, long long p) {
   int n = a.size();
   vector<vector<long long> > ans(n, vector<long long>(n));
   for (int i = 0; i < n; i++) ans[i][i] = 1;
   while (p) {
     if (p & 1) ans = matmul(ans, a);
     a = matmul(a, a);
     p >>= 1;
   }
   return ans;
 }
 long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
   long long ans = 1;
   while (p) {
     if (p & 1) ans = (ans * base) % mod;
     base = (base * base) % mod;
     p /= 2;
   }
   return ans;
 }
 long long gcd(long long a, long long b) {
   if (b == 0) return a;
   return gcd(b, a % b);
 }
 long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
 long long invMod(long long n, long long mod = (1000000007LL)) {
   return pwr(n, mod - 2);
 }
 int n, arr[2000003];
 int main() {
   ios_base::sync_with_stdio(0);
   cin >> n;
   for (int i = 1; i <= n; i++) arr[i] = i;
   int start = 1;
   for (int iter = 2; iter <= n; iter++) {
     int last = arr[start];
     for (int j = start + iter; j < start + n; j += iter) {
       int temp = last;
       last = arr[j];
       arr[j] = temp;
     }
     arr[start + n] = last;
     start++;
   }
   for (int i = start; i < start + n; i++) cout << arr[i] <<    ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, a[110], b[110], sum = 0;
 pair<int, int> dp[110][11000], q = {-1, -1};
 pair<int, int> Rec(int i, int W) {
   if (i == n) {
     if (W >= sum)
       return {0, 0};
     else
       return {1e9, 1e9};
   }
   if (dp[i][W] != q) return dp[i][W];
   pair<int, int> Res1 = {1e9, 1e9}, Res2 = {1e9, 1e9};
   Res1 = {0 + Rec(i + 1, W).first, a[i] + Rec(i + 1, W).second};
   Res2 = {1 + Rec(i + 1, W + b[i]).first, 0 + Rec(i + 1, W + b[i]).second};
   return dp[i][W] = min(Res1, Res2);
 }
 int main() {
   memset(dp, -1, sizeof(dp));
   cin >> n;
   for (int i = 0; i < n; i++) cin >> a[i], sum += a[i];
   for (int i = 0; i < n; i++) cin >> b[i];
   cout << Rec(0, 0).first <<     << Rec(0, 0).second;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s[200005];
 int n;
 int mem[200005][27];
 int dirr[200005][27];
 bool vis[200005][27];
 char other(char c, char d) {
   if ('a' != c && 'a' != d) return 'a';
   if ('b' != c && 'b' != d) return 'b';
   return 'c';
 }
 int solve(int ind, int lst) {
   if (ind >= n) return 0;
   if (vis[ind][lst]) return mem[ind][lst];
   vis[ind][lst] = 1;
   int ans1 = -1, ans2 = -1, ans = -1, dir = -1;
   if (s[ind] - 'a' != lst) ans1 = solve(ind + 1, s[ind] - 'a');
   ans2 = 1 + solve(ind + 1, other(s[ind + 1], lst + 'a') - 'a');
   if (ans1 == -1 || ans2 <= ans1)
     dir = 1, ans = ans2;
   else
     dir = 0, ans = ans1;
   dirr[ind][lst] = dir;
   return mem[ind][lst] = ans;
 }
 int main() {
   ios::sync_with_stdio(false);
   scanf( %s , s);
   n = strlen(s);
   solve(0, 26);
   int lst = 26;
   for (int i = 0; i < n; i++)
     if (dirr[i][lst] == 0)
       lst = s[i] - 'a';
     else
       s[i] = lst = other(s[i + 1], lst + 'a'), lst -= 'a';
   puts(s);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, t1, t2;
 vector<int> q1, q2;
 vector<vector<int> > Visted;
 int des(int point1, int point2, int point3, int point4) {
   return abs(point1 - point3) + abs(point2 - point4);
 }
 int main() {
   cin >> n >> m >> t1;
   Visted.resize(n + 10, vector<int>(m + 10));
   q1.resize(t1);
   for (int i = 0; i < t1; i++) cin >> q1[i];
   cin >> t2;
   q2.resize(t2);
   for (int i = 0; i < t2; i++) cin >> q2[i];
   sort(q1.begin(), q1.end());
   sort(q2.begin(), q2.end());
   for (int i = 0; i < t1; i++) {
     bool take = false;
     int desEnd = -1e9, X, Y;
     for (int j = 1; j <= n; j++) {
       for (int z = 1; z <= m; z++) {
         if (!Visted[j][z] && des(0, 0, j, z) <= q1[i] &&
             des(0, m + 1, j, z) > desEnd) {
           desEnd = des(0, m + 1, j, z);
           take = true;
           X = j, Y = z;
         }
       }
     }
     if (take) {
       Visted[X][Y] = 1;
     } else {
       cout <<  NO ;
       return 0;
     }
   }
   for (int i = 0; i < t2; i++) {
     bool take = false;
     int desEnd = 1e9, X, Y;
     for (int j = 1; j <= n; j++) {
       for (int z = 1; z <= m; z++) {
         if (!Visted[j][z] && des(0, m + 1, j, z) <= q2[i] &&
             des(0, m + 1, j, z) < desEnd) {
           take = true;
           X = j, Y = z;
         }
       }
     }
     if (take) {
       Visted[X][Y] = 1;
     } else {
       cout <<  NO ;
       return 0;
     }
   }
   cout <<  YES ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class TH>
 void _dbg(const char *sdbg, TH h) {
   cerr << sdbg << '=' << h << endl;
 }
 template <class TH, class... TA>
 void _dbg(const char *sdbg, TH h, TA... a) {
   while (*sdbg != ',') cerr << *sdbg++;
   cerr << '=' << h << ',';
   _dbg(sdbg + 1, a...);
 }
 template <class T>
 ostream &operator<<(ostream &os, vector<T> V) {
   os <<  [ ;
   for (auto vv : V) os << vv <<  , ;
   return os <<  ] ;
 }
 template <class L, class R>
 ostream &operator<<(ostream &os, pair<L, R> P) {
   return os <<  (  << P.first <<  ,  << P.second <<  ) ;
 }
 template <class T>
 using min_heap = priority_queue<T, vector<T>, greater<T>>;
 template <class T>
 using max_heap = priority_queue<T>;
 const int mod = 1e9 + 7;
 long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
 struct PairHash {
   template <typename T1, typename T2>
   std::size_t operator()(const pair<T1, T2> &p) const {
     return hash<T1>()(p.first) ^ hash<T2>()(p.second);
   }
 };
 void solve(int ncase) {
   int a, b, c, d;
   cin >> a >> b >> c >> d;
   int minx = min({a, b, c});
   a -= minx;
   b -= minx;
   c -= minx;
   cout << (((a % 2 + b % 2 + c % 2 + (d + 3 * 1LL * minx) % 2) <= 1) ||
                    (minx >= 1 && ((a + 1) % 2 + (b + 1) % 2 + (c + 1) % 2 +
                                   (d + 3 * 1LL * minx - 3) % 2) <= 1)
                ?  Yes
                :  No )
        << endl;
 }
 void solve_all_cases() {
   int T = 1;
   cin >> T;
   int ncase = 0;
   while (T--) {
     solve(++ncase);
   }
 }
 int main() {
   cout << std::fixed;
   cout << setprecision(6);
   solve_all_cases();
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   scanf( %d , &n);
   int a[n];
   for (int i = 0; i < n; i++) cin >> a[i];
   for (int i = 0; i < n - 1; i++) cout << (a[i] + a[i + 1]) << ' ';
   cout << a[n - 1];
 }


#include <bits/stdc++.h>
 using namespace std;
 int j, i, a, b, c, k, d, e, f[1005], n, mi, ma;
 int main() {
   cin >> n;
   for (i = 0; i < n; i++) {
     cin >> f[i];
     ma = max(f[i], ma);
   }
   for (i = 0; i < n - 1; i++)
     for (j = i + 1; j < n; j++) {
       c = 0;
       for (k = i; k <= j; k++) c = f[k] ^ c;
       ma = max(ma, c);
     }
   cout << ma;
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize( Ofast )
 #pragma GCC target( \n      avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )
 using namespace std;
 long long MOD = 1e9 + 7;
 long long k;
 vector<long long> v, w, num;
 vector<pair<long long, pair<long long, long long> > > zp;
 vector<vector<long long> > dp;
 long long st(long long t, long long id, long long mod) {
   long long res = 1, a = t, n = id;
   while (n) {
     if (n & 1) res = (res * a) % mod;
     a = (a * a) % mod;
     n >>= 1;
   }
   return res % mod;
 }
 void add(long long id) {
   vector<long long> mem(k + 1);
   dp.push_back(mem);
   long long lst = dp.size() - 1;
   for (int i = 0; i < k + 1; i++) {
     dp[lst][i] = dp[lst - 1][i];
     if (i >= w[id])
       dp[lst][i] = max(dp[lst - 1][i], dp[lst - 1][i - w[id]] + v[id]);
   }
 }
 void del() { dp.pop_back(); }
 long long ans() {
   long long lst = dp.size() - 1;
   long long anss = 0;
   for (int i = 1; i < k + 1; i++) {
     anss = ((10000019 * (long long)anss) + (long long)dp[lst][k - i + 1]) % MOD;
     if (anss > MOD) anss -= MOD;
   }
   return anss;
 }
 void get(long long l, long long r) {
   if (r == l + 1) {
     if (zp[l].first == 3) {
       zp[l].second.first = ans();
     }
     return;
   }
   long long mid = (l + r) / 2;
   long long kol = 0;
   for (int i = mid; i < r; i++) {
     if (zp[i].first == 2) {
       if (num[zp[i].second.second] < l) {
         add(zp[i].second.second);
         kol++;
       }
     }
   }
   get(l, mid);
   while (kol--) del();
   kol = 0;
   for (int i = l; i < mid; i++) {
     if (zp[i].first == 1) {
       if (zp[i].second.second >= r) {
         add(zp[i].second.first);
         kol++;
       }
     }
   }
   get(mid, r);
   while (kol--) del();
 }
 int main() {
   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
       cout << fixed << setprecision(20);
   long long n;
   cin >> n >> k;
   v.resize(n), w.resize(n), num.resize(n);
   vector<long long> mem(k + 1);
   dp.push_back(mem);
   for (int i = 0; i < n; i++) {
     cin >> v[i] >> w[i];
     zp.push_back({1, {i, MOD}});
     num[i] = i;
   }
   long long q;
   cin >> q;
   while (q--) {
     long long sz = zp.size();
     zp.push_back({0, {num.size(), MOD}});
     cin >> zp[zp.size() - 1].first;
     if (zp.back().first == 1) {
       long long vv, ww;
       cin >> vv >> ww;
       v.push_back(vv);
       w.push_back(ww);
       num.push_back(sz);
     }
     if (zp.back().first == 2) {
       long long x;
       cin >> x;
       x--;
       zp[num[x]].second.second = sz;
       zp[sz].second.second = x;
     }
   }
   get(0, zp.size());
   for (auto i : zp) {
     if (i.first == 3) {
       cout << i.second.first << endl;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 long long read() {
   long long w = 0;
   char c = getchar();
   while (c < '0' || c > '9') c = getchar();
   while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
   return w;
 }
 long long n, s[100005], e[100005];
 bool f[100005][2];
 bool check(long long x, long long y, long long op) {
   if ((y & 1) && (!op)) return (y - x) & 1;
   long long h = y >> 1;
   if (x > h) return op ? 1 : (y - x) & 1;
   return check(x, h, op ^ 1);
 }
 signed main() {
   n = read();
   for (long long i = 1; i <= n; i++)
     s[i] = read(), e[i] = read(), f[i][0] = check(s[i], e[i], 0),
     f[i][1] = check(s[i], e[i], 1);
   bool need = f[n][0];
   for (long long i = n - 1; i > 0; i--) need = f[i][need];
   printf(need ?  1   :  0  );
   need = f[n][1];
   for (long long i = n - 1; i > 0; i--) need = f[i][need];
   printf(need ?  1  :  0 );
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 16, Inf = 1e9, lastS = (1 << N) - 1;
 static int n, sqrn, R, B, ans, lasts;
 struct TCard {
   bool c;
   int r, b;
 } card[N];
 static int f[lastS][N * N + 1][2], cardr[lastS], cardb[lastS];
 inline int GetF(int s, int rem, bool c) {
   rem = min(rem, sqrn);
   if (s == lasts) return 0;
   auto &fs = f[s][rem][c];
   if (fs != -1) return fs;
   fs = Inf;
   for (int i = 0; i < n; ++i) {
     if (s & 1 << i) continue;
     int R = max(0, card[i].r - cardr[s]), B = max(0, card[i].b - cardb[s]),
         turn, _r, _b;
     if (c) {
       turn = max(R - rem, B);
       _r = rem + turn - R;
       _b = turn - B;
     } else {
       turn = max(B - rem, R);
       _b = rem + turn - B;
       _r = turn - R;
     }
     if (_r)
       fs = min(fs, turn + GetF(s ^ 1 << i, _r, 1));
     else
       fs = min(fs, turn + GetF(s ^ 1 << i, _b, 0));
   }
   return fs;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cin >> n;
   sqrn = n * n;
   lasts = (1 << n) - 1;
   for_each(card, card + n, [](TCard &C) {
     char tmp;
     cin >> tmp >> C.r >> C.b;
     C.c = tmp == 'R';
     if (C.r > n) R += C.r - n, C.r = n;
     if (C.b > n) B += C.b - n, C.b = n;
   });
   for (int s = 1; s <= lasts; ++s) {
     int i = __builtin_ctz(s);
     cardr[s] = cardr[s ^ 1 << i] + card[i].c;
     cardb[s] = cardb[s ^ 1 << i] + !card[i].c;
   }
   memset(f, -1, sizeof f);
   if (R > B)
     ans = R + GetF(0, R - B, 0);
   else
     ans = B + GetF(0, B - R, 1);
   cout << ans + n;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename... Args>
 void logger(string vars, Args&&... values) {}
 template <typename T>
 inline void maxa(T& a, T b) {
   a = max(a, b);
 }
 template <typename T>
 inline void mina(T& a, T b) {
   a = min(a, b);
 }
 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 const int mxN = 2e5 + 5;
 const int mod = 1e9 + 7;
 int main() {
   cin.sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   int n;
   cin >> n;
   vector<int> a(n);
   vector<char> ans(n);
   for (int& i : a) cin >> i;
   string s;
   cin >> s;
   sort(a.begin(), a.end());
   int cnt = 0;
   for (int i = 1; i < n; i++) cnt += (s[i] != s[i - 1]);
   int l = n - cnt - 1;
   int r = n - cnt - 1;
   cout << a[l] <<     << s[0] <<
 ;
   ans[l] = s[0];
   for (int i = 1; i < (int)(s.size()); i++) {
     if (s[i] != s[i - 1]) {
       ans[++r] = ans[r] == 'L' ? 'R' : 'L';
       cout << a[r] <<     << ans[r] <<
 ;
     } else {
       ans[--l] = ans[l] == 'L' ? 'R' : 'L';
       cout << a[l] <<     << ans[l] <<
 ;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 2000 + 100;
 map<pair<long long, long long>, int> rat;
 int n;
 pair<long long, long long> px[maxn], py[maxn];
 long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
 int main() {
   cin >> n;
   for (int i = 1, a, b, c; i <= n; i++) {
     cin >> a >> b >> c;
     px[i] = make_pair(c * b, a * a + b * b);
     py[i] = make_pair(a * c, a * a + b * b);
   }
   long long ans = 0;
   for (int i = 1; i <= n; i++) {
     rat.clear();
     long long two0 = 0;
     for (int j = i + 1; j <= n; j++) {
       long long a = px[j].first * px[i].second - px[j].second * px[i].first;
       long long b = py[j].first * py[i].second - py[j].second * py[i].first;
       long long g = gcd(abs(a), abs(b));
       if (g != 0) a /= g, b /= g;
       if (a == 0 && b == 0) {
         two0++;
         ans += j - i - 1;
       } else {
         if (a == 0) b = 1;
         if (b == 0) a = 1;
         if (a < 0 && b < 0) a *= -1, b *= -1;
         if (a > 0 && b < 0) a *= -1, b *= -1;
         ans += two0;
         if (rat.count(make_pair(a, b))) {
           ans += rat[make_pair(a, b)];
           rat[make_pair(a, b)]++;
         } else
           rat[make_pair(a, b)] = 1;
       }
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 25;
 int dp[MAXN][MAXN][MAXN];
 int n, t;
 int main() {
   scanf( %d%d , &n, &t);
   t = 2 * t;
   dp[1][0][1] = dp[1][0][2] = dp[1][0][3] = 1;
   for (int i = 1; i < n; ++i)
     for (int j = 0; j <= t; ++j)
       for (int k = 1; k <= 4; ++k)
         for (int l = 1; l <= 4; ++l) {
           if ((i == 1 && l > k) || (i != 1 && l != k)) {
             int nj = j;
             if (j % 2 && l < k)
               ++nj;
             else if (j % 2 == 0 && l > k)
               ++nj;
             dp[i + 1][nj][l] += dp[i][j][k];
           }
         }
   int ans = 0;
   for (int i = 1; i <= 4; ++i) ans += dp[n][t][i];
   printf( %d
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   multiset<long long int> s;
   map<string, int> mp;
   while (t--) {
     char ch;
     long long int x;
     cin >> ch >> x;
     string str(18, '0');
     string temp = to_string(x);
     int j = 17;
     for (int i = ((int)(temp).size()) - 1; i >= 0; i--) {
       if ((temp[i] - '0') % 2 != 0) {
         str[j] = '1';
       }
       j--;
     }
     if (ch == '+') {
       mp[str]++;
     } else if (ch == '-') {
       mp[str]--;
     } else {
       cout << mp[str] <<
 ;
     }
   }
 }


#include <bits/stdc++.h>
 #pragma warning(disable : 4996)
 #pragma comment(linker,  /STACK:666000000 )
 using namespace std;
 const int INF = (1 << 30) - 1;
 const long double EPS = 1e-9;
 const long double PI = fabs(atan2(0.0, -1.0));
 int n;
 void load() { cin >> n; }
 const int SZ = 1010;
 int sv[SZ];
 int rec(const int v) {
   if (v < SZ && -1 != sv[v]) return sv[v];
   bool can[10];
   int p9;
   memset(can, false, sizeof(can));
   {
     int p = 0;
     int tmp = v;
     while (tmp != 0) {
       can[tmp % 10] = true;
       if (9 == tmp % 10) p9 = p;
       tmp /= 10;
       p *= 10;
     }
   }
   if (v >= SZ) {
     if (can[9] && p9 > 10) {
       int cnt = 9 * (p9 / 10) - 8;
       return cnt + rec(v - cnt * 9);
     }
     for (int i = 9; i >= 1; i--)
       if (can[i]) return 1 + rec(v - i);
   }
   int res = INT_MAX;
   for (int i = 1; i <= 9; i++)
     if (can[i]) res = min(res, 1 + rec(v - i));
   if (v < SZ) sv[v] = res;
   return res;
 }
 void solve() {
   memset(sv, -1, sizeof(sv));
   sv[0] = 0;
   int res = rec(n);
   cout << res;
 }
 int main() {
   load();
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e5;
 map<int, int> dic;
 int n, a[N + 10], ma[N + 10], ma2[N + 10];
 int main() {
   ios::sync_with_stdio(0), cin.tie(0);
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> a[i];
   ma[1] = a[1];
   ma2[1] = -1;
   for (int i = 2; i <= n; i++) {
     if (a[i] > ma[i - 1]) {
       ma2[i] = ma[i - 1];
       ma[i] = a[i];
     } else {
       ma[i] = ma[i - 1];
       if (ma2[i - 1] < a[i]) {
         ma2[i] = a[i];
       } else
         ma2[i] = ma2[i - 1];
     }
     if (a[i] != ma[i] && a[i] == ma2[i]) {
       dic[ma[i]]++;
     }
   }
   int tot = -1e6, idx = -1;
   for (int i = 1; i <= n; i++) {
     int temp = dic[a[i]];
     if (a[i] == ma[i]) temp--;
     if (temp > tot) {
       tot = temp;
       idx = a[i];
     } else if (temp == tot) {
       idx = min(idx, a[i]);
     }
   }
   cout << idx << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long a[1000005] = {0};
 long long b[1000005] = {0};
 long long c[1000005] = {0};
 long long d[1000005] = {0};
 long long s;
 long long i, j = 0, l;
 long long count1 = 0;
 long long count2 = 0;
 long long count3 = 0;
 long long count4 = 0;
 long long ans = 0;
 bool cmp(long long aa, long long bb) { return aa > bb; }
 int main() {
   long long n;
   scanf( %lld
 , &n);
   for (i = 0; i < n; i++) {
     cin >> s >> j;
     if (s == 11) {
       a[count1++] = j;
       ans += j;
     } else if (s == 10) {
       b[count2++] = j;
     } else if (s == 1) {
       c[count3++] = j;
     } else {
       d[count4++] = j;
     }
   }
   sort(b, b + count2, cmp);
   sort(c, c + count3, cmp);
   for (i = 0; i < min(count2, count3); i++) ans += (b[i] + c[i]);
   for (i = 0; i < abs(count2 - count3); i++) {
     if (count3 > count2)
       d[count4 + i] = c[min(count2, count3) + i];
     else if (count3 < count2)
       d[count4 + i] = b[min(count2, count3) + i];
   }
   count4 += abs(count2 - count3);
   sort(d, d + count4, cmp);
   for (i = 0; i < count1 && i < count4; i++) ans += d[i];
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int mod = 1e9 + 7;
 int32_t main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   long long int n, k;
   cin >> n >> k;
   long long int ans = 1;
   while (k % 2 == 0) {
     k = k / 2;
     ans++;
   }
   cout << ans << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e4 + 10, mod = 1e9 + 7, M = 510;
 const double PI = 3.1415926535;
 long long res[40], p[40], ei[40], sum[N], cnt;
 double pre[N];
 int path[100];
 void dfs(int u, int s, long long va) {
   if (u == -1) {
     sum[s] = ((long long)sum[s] + va) % mod;
     return;
   }
   for (int i = 0; i <= cnt; i++) {
     path[i]++;
     if (res[i] - path[i] + 1)
       dfs(u - 1, s + i, (long long)va * (res[i] - path[i] + 1) % mod);
     path[i]--;
   }
 }
 int main() {
   int n;
   scanf( %d , &n);
   vector<int> temp;
   int t = n;
   while (t) t /= 10, cnt++;
   p[1] = 1;
   for (int i = 2; i <= 11; i++) p[i] = p[i - 1] * 10;
   ei[0] = 1;
   for (int i = 1; i < 11; i++) ei[i] = ei[i - 1] * 8;
   for (int i = 0; i < (1 << cnt); i++) {
     vector<int> temp;
     for (int j = 0; j < cnt; j++)
       if (i >> j & 1) temp.push_back(j);
     int len = temp.size();
     for (int j = 0; j < 1 << len; j++) {
       int a[15] = {0};
       for (int k = 0; k < len; k++)
         if (j >> k & 1)
           a[temp[k]] = 7;
         else
           a[temp[k]] = 4;
       long long f = p[cnt], va = 0;
       for (long long k = cnt - 1, r = 1; k >= 0; k--, r *= 10)
         va += (long long)a[k] * r;
       if (va > n) continue;
       for (int k = 0; k < cnt; k++) {
         if (a[k]) {
           f /= 10;
           continue;
         }
         for (int r = 9; r >= 0; r--)
           if ((long long)r * f + va <= n) {
             a[k] = r, va = r * f + va;
             break;
           }
         f /= 10;
       }
       long long num = 0, ans = 0;
       for (int k = 0; k < cnt; k++) {
         if (num < temp.size() && temp[num] == k) {
           num++;
           if (k == cnt - 1) ans++;
           continue;
         }
         if (a[k] == 7 || a[k] == 4) {
           int sum = cnt - 1 - k;
           int ts = temp.size() - num;
           int tnum = a[k] + 1;
           if (a[k] == 4)
             tnum--;
           else
             tnum -= 2;
           ans = ans + ei[sum - ts] * (long long)tnum;
           break;
         } else {
           int sum = cnt - 1 - k;
           int ts = temp.size() - num;
           int tnum = a[k] + 1;
           if (a[k] > 7)
             tnum -= 3;
           else if (a[k] > 4)
             tnum -= 2;
           else
             tnum--;
           if (k == cnt - 1) tnum++;
           ans = ans + ei[sum - ts] * (long long)tnum;
         }
       }
       res[len] = (res[len] + (long long)ans);
     }
   }
   res[0]--;
   dfs(5, 0, 1);
   long long ans = 0, pre = 0;
   for (int i = 1; i <= cnt; i++) {
     pre = pre + sum[i - 1];
     ans = (ans + pre * res[i]) % mod;
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int N, good;
 bool prime[100010];
 vector<int> fact, num, lis;
 int main() {
   memset(prime, true, sizeof(prime));
   for (int i = 2; i * i <= 100000; i++)
     if (prime[i])
       for (int j = 2; i * j <= 100000; j++) prime[i * j] = false;
   for (int i = 2; i <= 100000; i++)
     if (prime[i]) {
       num.push_back(i);
       lis.push_back(0);
     }
   scanf( %d , &N);
   while (N--) {
     scanf( %d , &good);
     int temp = good;
     fact.clear();
     int longest = 0;
     for (int i = 0; i < num.size() && num[i] * num[i] <= good; i++)
       if (good % num[i] == 0) {
         fact.push_back(i);
         longest = max(longest, lis[i] + 1);
         while (temp % num[i] == 0) temp /= num[i];
       }
     if (temp > 1) {
       int i = lower_bound(num.begin(), num.end(), temp) - num.begin();
       fact.push_back(i);
       longest = max(longest, lis[i] + 1);
     }
     for (int i = 0; i < fact.size(); i++)
       lis[fact[i]] = max(lis[fact[i]], longest);
   }
   int ans = 1;
   for (int i = 0; i < num.size(); i++) ans = max(ans, lis[i]);
   printf( %d
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2300;
 int dp[N], a[N], res = 0, n;
 void dfs(int u, int d) {
   if (d == n) {
     dp[u] = 0;
     return;
   }
   int l = u * 2, r = l + 1;
   dfs(l, d + 1);
   dfs(r, d + 1);
   int x = dp[l] + a[l], y = dp[r] + a[r];
   if (x < y) swap(x, y);
   dp[u] = x;
   res += x - y;
 }
 int main() {
   cin >> n;
   for (int i = 2; i < (1 << n + 1); i++) {
     scanf( %d , &a[i]);
   }
   dfs(1, 0);
   cout << res << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, root, val, rem;
 int main() {
   cin >> n;
   root = floor((sqrt(1.0 + 8.0 * n) - 1.0) / 2.0);
   val = root * (root + 1) / 2;
   if (n == val) --root;
   val = root * (root + 1) / 2;
   rem = n - val;
   cout << rem << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long N = 1e5 + 2;
 const long long M = 1e6 + 2;
 const long long mod = 998244353;
 long long ar[N], fact[M], inv[M], cnt[N];
 long long binpow(long long x, long long y) {
   long long tich = 1;
   while (y) {
     if (y & 1) {
       tich *= x;
       tich %= mod;
     }
     x *= x;
     x %= mod;
     y >>= 1;
   }
   return tich;
 }
 long long C(long long x, long long y) {
   return ((fact[y] * inv[y - x]) % mod * inv[x]) % mod;
 }
 signed main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   long long n, i, j, k, l, ans = 0, sum = 0;
   cin >> n;
   for (i = 1; i <= n; i++) {
     cin >> ar[i];
     cnt[ar[i] % n]++;
     sum += ar[i];
   }
   fact[0] = 1;
   for (i = 1; i < M - 1; i++) {
     fact[i] = (fact[i - 1] * i) % mod;
   }
   inv[M - 2] = binpow(fact[M - 2], mod - 2);
   for (i = M - 2; i > 0; i--) {
     inv[i - 1] = (inv[i] * i) % mod;
   }
   sort(ar + 1, ar + 1 + n);
   long long pos = 0;
   for (i = 1; i < n; i++) {
     if (ar[i + 1] < i) {
       pos = i;
       break;
     }
   }
   if (pos) {
     j = 1;
     for (i = 0; i < pos; i++) {
       while (ar[j] < i) {
         j++;
       }
       ans += C(n - 1, i - (j - 1) + n - 1);
       if (ans >= mod) {
         ans -= mod;
       }
     }
     cout << ans;
   } else {
     for (i = 1; i <= n; i++) {
       sum -= ar[i] % n;
     }
     sum /= n;
     for (i = 1; i <= n; i++) {
       sum = sum + cnt[n - i] - 1;
       if (sum >= 0) {
         ans += C(n - 1, sum + n - 1);
         if (ans >= mod) {
           ans -= mod;
         }
       }
     }
     cout << ans;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   vector<pair<int, int>> v;
   for (int i = 0; i < m; i++) {
     int a, b;
     cin >> a >> b;
     v.push_back(make_pair(a, b));
   }
   int x1 = v[0].first;
   int y1 = v[0].second;
   vector<int> p1, p2;
   for (int i = 1; i < m; i++) {
     int a = v[i].first;
     int b = v[i].second;
     if (x1 != a && x1 != b) {
       p1.push_back(i);
     }
     if (y1 != a && y1 != b) {
       p2.push_back(i);
     }
   }
   if (p1.size() == 0 || p2.size() == 0) {
     cout <<  YES  << endl;
     return 0;
   }
   x1 = v[p1[0]].first;
   y1 = v[p1[0]].second;
   int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
   for (int i = 1; i < p1.size(); i++) {
     int a = v[p1[i]].first;
     int b = v[p1[i]].second;
     if (x1 != a && x1 != b) {
       c1++;
     }
     if (y1 != a && y1 != b) {
       c2++;
     }
   }
   x1 = v[p2[0]].first;
   y1 = v[p2[0]].second;
   for (int i = 1; i < p2.size(); i++) {
     int a = v[p2[i]].first;
     int b = v[p2[i]].second;
     if (x1 != a && x1 != b) {
       c3++;
     }
     if (y1 != a && y1 != b) {
       c4++;
     }
   }
   if (c1 == 0 || c2 == 0) {
     cout <<  YES  << endl;
   } else if (c3 == 0 || c4 == 0) {
     cout <<  YES  << endl;
   } else {
     cout <<  NO  << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline bool chmax(T &a, T b) {
   return a < b ? (a = b, 1) : 0;
 }
 template <class T>
 inline bool chmin(T &a, T b) {
   return a > b ? (a = b, 1) : 0;
 }
 int main() {
   int n, m;
   cin >> n >> m;
   map<int, vector<int>> obs;
   int left = n;
   while (m--) {
     int x, y;
     cin >> x >> y;
     if (x == 1)
       chmin(left, y - 1);
     else
       obs[x].emplace_back(y);
   }
   vector<pair<int, int>> segs = {{1, left}};
   int prv = 1;
   for (auto &p : obs) {
     auto &v = p.second;
     sort(begin(v), end(v));
     if (p.first != prv + 1) {
       segs.assign(1, {segs.begin()->first, n});
     }
     vector<pair<int, int>> segs2;
     auto it = segs.begin();
     int p_v = 0, sz_v = v.size();
     int lst = -1;
     while (it != segs.end() && p_v < sz_v) {
       chmax(it->first, lst);
       if (it->first > it->second) {
         it++;
         continue;
       }
       if (v[p_v] <= it->first) {
         it->first += v[p_v++] == it->first;
         continue;
       }
       segs2.emplace_back(it->first, v[p_v] - 1);
       lst = v[p_v++] + 1;
     }
     if (p_v == sz_v) {
       while (it != segs.end()) {
         chmax(it->first, lst);
         if (it->first <= it->second) break;
         it++;
       }
       if (it != segs.end()) {
         segs2.emplace_back(it->first, n);
       }
     }
     if ((segs = segs2).empty()) return !(cout << -1 << endl);
     prv = p.first;
   }
   if (prv < n) {
     segs.assign(1, {segs.begin()->first, n});
   }
   cout << (segs.rbegin()->second == n ? (n - 1) * 2 : -1) << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   int t;
   cin >> t;
   while (t--) {
     long long n, k;
     cin >> n >> k;
     if (n > k)
       cout << k << endl;
     else {
       int ne = (k - 1) / (n - 1);
       cout << k + ne << endl;
     }
   }
 }


#include <bits/stdc++.h>
 int mat[110][110];
 int main() {
   int n, k, i, j;
   scanf( %d %d , &n, &k);
   if (n * n < k) {
     printf( -1
 );
     return 0;
   }
   for (i = 0; i < n; i++) {
     for (j = i; j < n; j++) {
       if (i == j) {
         if (k > 0) {
           k--;
           mat[i][j] = 1;
         } else
           break;
       } else {
         if (k >= 2) {
           mat[i][j] = 1;
           mat[j][i] = 1;
           k = k - 2;
         } else if (k == 0)
           break;
       }
     }
   }
   for (i = 0; i < n; i++) {
     for (j = 0; j < n; j++) {
       printf( %d  , mat[i][j]);
     }
     printf(
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 int n, k;
 bool visited[101][101][1010];
 double ar[101], dp[101][101][1010];
 double F(int i, int j, int k) {
   if (i > j) return 1.0 - F(j, i, k);
   if (k == 0) return 1.0;
   if (visited[i][j][k]) return dp[i][j][k];
   int a, b, c, d, l;
   double x, y, p = 1.0 / ((n * (n + 1)) >> 1), res = 0.0;
   for (a = 0; a < n; a++) {
     for (b = a; b < n; b++) {
       c = i, d = j;
       if (c >= a && c <= b) c = b - (c - a);
       if (d >= a && d <= b) d = b - (d - a);
       res += (p * F(c, d, k - 1));
     }
   }
   visited[i][j][k] = true;
   return (dp[i][j][k] = res);
 }
 int main() {
   int i, j, l;
   while (scanf( %d %d , &n, &k) != EOF) {
     if (k > 1000) k = 1000;
     for (i = 0; i < n; i++) scanf( %d , &ar[i]);
     memset(visited, 0, sizeof(visited));
     double res = 0.0;
     for (i = 0; i < n; i++) {
       for (j = i + 1; j < n; j++) {
         if (ar[i] < ar[j])
           res += (1.0 - F(i, j, k));
         else
           res += F(i, j, k);
       }
     }
     printf( %0.12f
 , res + 1e-15);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 T gcd(T a, T b) {
   return b ? gcd(b, a % b) : a;
 }
 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 const double EPS = 1e-9;
 const double PI = acos(-1.);
 const long long LL_INF = 1e17 + 16;
 const int INF = 1e9 + 10;
 const long long MOD = 1e9 + 7;
 const int MAXN = 20;
 set<long long> goods;
 long long f[MAXN];
 void gen() {
   for (int i = (1); i <= (int)(10); i++) {
     for (int j = (0); j < (int)(1 << i); j++) {
       long long v = 0;
       long long st10 = 1;
       for (int k = (0); k < (int)(i); k++) {
         if (j & (1 << k)) {
           v += 7 * st10;
         } else {
           v += 4 * st10;
         }
         st10 *= 10;
       }
       goods.insert(v);
     }
   }
 }
 int per[MAXN];
 int used[MAXN];
 void solve() {
   gen();
   long long n, k;
   cin >> n >> k;
   f[0] = 1;
   int m;
   for (m = 1;; m++) {
     f[m] = f[m - 1] * m;
     if (f[m] >= k) {
       break;
     }
   }
   if (m > n) {
     printf( -1
 );
     return;
   }
   int ans = 0;
   int offset = n - m;
   for (__typeof((goods).begin()) it = (goods).begin(); it != (goods).end();
        it++) {
     if ((*it) <= offset) {
       ans++;
     }
   }
   k--;
   int top = 1;
   for (int i = m; i >= 1; i--) {
     int v = k / f[i - 1] + 1;
     int cnt = 0;
     for (int j = (1); j <= (int)(m); j++) {
       if (!used[j]) {
         cnt++;
         if (cnt == v) {
           per[top++] = j;
           used[j] = 1;
           break;
         }
       }
     }
     k %= f[i - 1];
   }
   for (int i = (1); i <= (int)(m); i++) {
     if (goods.count(i + offset) && goods.count(per[i] + offset)) {
       ans++;
     }
   }
   cout << ans << endl;
 }
 int main() {
   int t = 1;
   while (t--) {
     solve();
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void dfs(set<int> &temp, list<int> adj[], vector<bool> &v, int s, int &t1,
          int &t2, vector<int> b, vector<int> w) {
   v[s] = true;
   list<int>::iterator it;
   for (it = adj[s].begin(); it != adj[s].end(); it++) {
     if (!v[*it]) {
       temp.insert(*it);
       t1 += b[*it];
       t2 += w[*it];
       dfs(temp, adj, v, *it, t1, t2, b, w);
     }
   }
 }
 list<int> adj[1001];
 int main() {
   int n, m, weight;
   cin >> n >> m >> weight;
   vector<int> b(n + 1);
   vector<int> w(n + 1);
   for (int i = 1; i <= n; i++) cin >> w[i];
   for (int i = 1; i <= n; i++) cin >> b[i];
   for (int i = 0; i < m; i++) {
     int a, b;
     cin >> a >> b;
     adj[a].push_back(b);
     adj[b].push_back(a);
   }
   vector<bool> v(n + 1, false);
   vector<set<int> > arr;
   vector<int> tw;
   vector<int> tb;
   for (int i = 1; i <= n; i++) {
     if (!v[i]) {
       set<int> temp;
       temp.insert(i);
       int t1 = b[i], t2 = w[i];
       dfs(temp, adj, v, i, t1, t2, b, w);
       arr.push_back(temp);
       tb.push_back(t1);
       tw.push_back(t2);
     }
   }
   vector<vector<int> > dp(arr.size() + 1, vector<int>(weight + 1, 0));
   for (int i = 1; i <= arr.size(); i++) {
     for (int j = 1; j <= weight; j++) {
       int max = 0;
       if (dp[i - 1][j] > max) max = dp[i - 1][j];
       if (j - tw[i - 1] >= 0)
         if (dp[i - 1][j - tw[i - 1]] + tb[i - 1] > max)
           max = dp[i - 1][j - tw[i - 1]] + tb[i - 1];
       set<int>::iterator it;
       for (it = arr[i - 1].begin(); it != arr[i - 1].end(); it++) {
         if (j - w[*it] >= 0)
           if (dp[i - 1][j - w[*it]] + b[*it] > max)
             max = dp[i - 1][j - w[*it]] + b[*it];
       }
       dp[i][j] = max;
     }
   }
   int max = 0;
   for (int i = 0; i <= weight; i++)
     if (dp[arr.size()][i] > max) max = dp[arr.size()][i];
   cout << max;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, m, ans, x, i, j, s;
 double a[100], b[100];
 map<double, pair<long long, long long> > mp;
 map<double, pair<long long, long long> >::iterator it, it2;
 long long num(long long x) {
   s = 0;
   while (x) {
     s++;
     x -= x & -x;
   }
   return s;
 }
 int main() {
   cin >> n >> m;
   for (i = 0; i < n; i++) cin >> a[i];
   for (i = 0; i < m; i++) cin >> b[i];
   for (i = 0; i < n; i++)
     for (j = 0; j < m; j++) {
       mp[(a[i] + b[j]) / 2.0].first |= (1LL << i);
       mp[(a[i] + b[j]) / 2.0].second |= (1LL << j);
     }
   for (it = mp.begin(); it != mp.end(); it++)
     for (it2 = it; it2 != mp.end(); it2++) {
       x = num((*it).second.first | (*it2).second.first) +
           num((*it).second.second | (*it2).second.second);
       if (x > ans) ans = x;
     }
   cout << ans;
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   long long n, k;
   scanf( extra , &n);
   while (n % 3 == 0) n /= 3;
   printf( extra , 1 + n / 3);
   getchar();
   getchar();
 }


#include <bits/stdc++.h>
 using namespace std;
 constexpr long long INF = numeric_limits<long long>::max() / 4;
 constexpr long long n_max = 2e5 + 10;
 template <typename A, typename B>
 string to_string(pair<A, B> p);
 string to_string(const string &s) { return ' ' + s + ' '; }
 string to_string(const char c) { return to_string((string)&c); }
 string to_string(bool b) { return (b ?  true  :  false ); }
 template <size_t N>
 string to_string(bitset<N> v) {
   string res =   ;
   for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
   return res;
 }
 template <typename A>
 string to_string(A v) {
   bool first = true;
   string res =  { ;
   for (const auto &x : v) {
     if (!first) res +=  ,  ;
     first = false;
     res += to_string(x);
   }
   res +=  } ;
   return res;
 }
 template <typename A, typename B>
 string to_string(pair<A, B> p) {
   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;
 }
 void debug_out() { cerr << endl; }
 template <typename Head, typename... Tail>
 void debug_out(Head H, Tail... T) {
   cerr <<     << to_string(H);
   debug_out(T...);
 }
 template <class T>
 bool chmax(T &a, T b) {
   if (a < b) {
     a = b;
     return true;
   }
   return false;
 }
 template <class T>
 bool chmin(T &a, T b) {
   if (a > b) {
     a = b;
     return true;
   }
   return false;
 }
 signed main() {
   cin.tie(nullptr);
   ios::sync_with_stdio(false);
   long long t;
   cin >> t;
   for (long long _ = 0; _ < (long long)(t); _++) {
     long long n;
     cin >> n;
     vector<long long> a(n);
     for (long long i = 0; i < (long long)(n); i++) cin >> a[i];
     vector<long long> dp(n + 1);
     for (long long i = 1; i < (long long)(n) + 1; i++) {
       dp[i]++;
       for (long long j = i * 2; j <= n; j += i) {
         if (a[j - 1] > a[i - 1]) chmax(dp[j], dp[i]);
       }
     }
     long long ans = 0;
     for (long long i = 0; i < (long long)(n + 1); i++) chmax(ans, dp[i]);
     cout << ans <<
 ;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int test = 1;
   cin >> test;
   while (test--) {
     long long int n, m;
     cin >> n >> m;
     long long int sum1 = 0, sum2 = 0;
     long long int a[n], b[n], mina[n], mi = 0;
     for (int i = 0; i < n; i++) {
       cin >> a[i] >> b[i];
       sum1 += a[i];
       sum2 += b[i];
       mina[i] = max(0LL, m - b[i]);
       mi += mina[i];
       a[i] = min(a[i], m);
     }
     long long int ans = sum1 - sum2 + (n * m);
     long long int x = ans - 2LL * mi;
     for (int i = 0; i < n; i++) {
       if (x <= 0) break;
       long long int temp = min(2LL * (a[i] - mina[i]), x);
       temp /= 2;
       mina[i] += temp;
       x -= 2LL * temp;
     }
     cout << abs(x) <<
 ;
     for (int i = 0; i < n; i++) cout << mina[i] <<     << m - mina[i] <<
 ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma warning(disable : 4996)
 using namespace std;
 bool compare(int a, int b) { return a > b; }
 int main() {
   int t, p, f, cnts, cntw, s, w;
   scanf( %d , &t);
   while (t--) {
     scanf( %d%d , &p, &f);
     scanf( %d%d , &cnts, &cntw);
     scanf( %d%d , &s, &w);
     if (s > w) {
       int temp = s;
       s = w;
       w = temp;
       temp = cnts;
       cnts = cntw;
       cntw = temp;
     }
     int mx = min(cnts, p / s);
     int ans = 0;
     for (int s1 = 0; s1 <= mx; s1++) {
       int w1 = min(cntw, (p - s1 * s) / w);
       int s2 = min(cnts - s1, f / s);
       int w2 = min(cntw - w1, (f - s2 * s) / w);
       ans = max(ans, s1 + s2 + w1 + w2);
     }
     printf( %d
 , ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e3 + 10;
 long long n, k;
 int main() {
   scanf( extraextra , &n, &k);
   long long x;
   x = sqrt(9 + 8 * (n + k)) - 3;
   x /= 2;
   printf( extra
 , n - x);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long MOD = 998244353;
 const int N = 3e3;
 long long dmod(long long x) { return x % MOD; }
 int n, k;
 int h[N + 5];
 long long memo[N + 5][2 * N + 5];
 long long dp(int idx, int gap) {
   if (idx > n) {
     if (gap > 0) return 1;
     return 0;
   }
   long long &ret = memo[idx][gap + N];
   if (ret != -1) return ret;
   ret = 0ll;
   if (h[idx] == h[idx % n + 1]) {
     ret = dmod((long long)k * dp(idx + 1, gap));
   } else {
     ret = dp(idx + 1, gap + 1);
     ret = dmod(ret + dp(idx + 1, gap - 1));
     ret = dmod(ret + dmod((long long)(k - 2) * dp(idx + 1, gap)));
   }
   return ret;
 }
 int main() {
   memset(memo, -1, sizeof(memo));
   scanf( %d%d , &n, &k);
   for (int i = 1; i <= n; i++) {
     scanf( %d , &h[i]);
   }
   printf( %lld
 , dp(1, 0));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, x, m = 0, sum = 0;
   cin >> n >> x;
   int *arr = new int[n];
   for (long long i = 0; i < n; i++) cin >> arr[i], sum += arr[i];
   if (sum > 0)
     while (sum > 0) {
       sum -= x;
       m++;
     }
   else
     while (sum < 0) {
       sum += x;
       m++;
     }
   cout << m;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int brute(int s, int x) {
   int ans = 0;
   for (int i = 1; i <= s; ++i)
     for (int j = 1; j <= s; ++j)
       if (i + j == s && (i ^ j) == x) ++ans;
   return ans;
 }
 long long ways0(long long s, long long x) {
   if (s == x) return 2;
   return 0;
 }
 long long solve(long long s, long long x) {
   long long dp[60][2] = {};
   if (s % 2 != x % 2) {
     return 0;
   }
   if (x & (1LL << 0)) {
     dp[0][0] = 2;
   } else {
     dp[0][0] = 1;
     dp[0][1] = 1;
   }
   for (int i = 1; i < 60; ++i) {
     if (x & (1LL << i)) {
       if (s & (1LL << i)) {
         dp[i][0] += 2 * dp[i - 1][0];
       } else {
         dp[i][1] += 2 * dp[i - 1][1];
       }
     } else {
       if (s & (1LL << i)) {
         dp[i][0] += dp[i - 1][1];
         dp[i][1] += dp[i - 1][1];
       } else {
         dp[i][0] += dp[i - 1][0];
         dp[i][1] += dp[i - 1][0];
       }
     }
   }
   return dp[59][0] - ways0(s, x);
 }
 int main() {
   long long s, x;
   cin >> s >> x;
   cout << solve(s, x) << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline void dmin(T& x, T y) {
   y < x ? x = y : 0;
 }
 template <class T>
 inline void dmax(T& x, T y) {
   y > x ? x = y : 0;
 }
 template <class T>
 inline void dmin(T& x, vector<T> y) {
   for (auto t : y) t < x ? x = t : 0;
 }
 template <class T>
 inline void dmax(T& x, vector<T> y) {
   for (auto t : y) t > x ? x = t : 0;
 }
 const int N = 5 + 1e6;
 const long long INF = -2e18;
 int n, k;
 long long gk(int x, long long a[], int i, long long f) {
   return a[x] + (i - x) / 3 * f;
 }
 bool cmp(int x, int y, long long a[], int i, long long f) {
   return gk(x, a, i, f) > gk(y, a, i, f);
 }
 void opera(long long a[], int n, long long f, int k) {
   static long long b[N];
   static long long q[3][N], left[3], right[3];
   for (int i = 0; i < 3; ++i) left[i] = 1, right[i] = 0;
   for (int i = 0; i < n; ++i) b[i] = INF;
   for (int i = 0; i < n; ++i) {
     int t = i % 3;
     while (left[t] <= right[t] && q[t][left[t]] < i - 3 * k) ++left[t];
     while (left[t] <= right[t] && cmp(i, q[t][right[t]], a, i, f)) --right[t];
     q[t][++right[t]] = i;
     b[i] = gk(q[t][left[t]], a, i, f);
   }
   for (int i = 0; i < n; ++i) a[i] = b[i];
 }
 void opera2(long long a[], int n, long long f, int k) {
   static long long f1[N], f2[N];
   for (int i = 0; i < n; ++i) {
     f1[i] = f2[i] = a[i];
     for (int j = 1; j < 10 && i - j >= 0; ++j) f2[i] = max(f2[i], a[i - j]);
   }
   opera(f1, n, f, 3 * k);
   opera(f2, n, f, 3 * (k - 1));
   for (int i = 0; i < n; ++i) a[i] = max(f1[i], f2[i]);
 }
 long long a[N], b[N];
 int main() {
   int k;
   scanf( %d , &k);
   long long f[6];
   for (int i = 0; i < 6; ++i) scanf( %lld , &f[i]);
   a[0] = 0;
   n = 1;
   for (int i = 5; i >= 0; --i) {
     for (int j = 0; j < 10 * n; ++j) b[j] = INF;
     for (int j = 0; j < n; ++j) b[j * 10] = a[j];
     n *= 10;
     opera2(b, n, f[i], k);
     for (int j = 0; j < n; ++j) a[j] = b[j];
   }
   int q;
   scanf( %d , &q);
   while (q--) {
     int x;
     scanf( %d , &x);
     printf( %lld
 , a[x]);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     int arr[n];
     for (int i = 0; i < n; i++) {
       cin >> arr[i];
     }
     sort(arr, arr + n);
     bool f = true;
     for (int j = 1; j < n; j++) {
       int diff = abs(arr[j] - arr[j - 1]);
       if (diff > 1) {
         f = false;
       }
     }
     if (f) {
       cout <<  YES
 ;
     } else {
       cout <<  NO
 ;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int modulo = 1000000007;
 const int limite = 1000001;
 long long int elev[limite];
 int ocupado[limite];
 int main() {
   ios_base::sync_with_stdio(false);
   int n, m, k;
   cin >> n >> m >> k;
   elev[0] = 1;
   for (int i = 1; i < limite; i++) elev[i] = (elev[i - 1] * 2) % modulo;
   int ini = 0;
   int fin = n + 1;
   int inf = k + 2;
   int sup = n;
   for (int i = 0; i < m; i++) {
     int u, v;
     cin >> u >> v;
     if (v == u + 1)
       ;
     else if (v == u + k + 1) {
       ocupado[v] = 1;
       ini = max(ini, u);
       fin = min(fin, v);
       inf = max(inf, u + 1);
       sup = min(sup, v + k);
     } else {
       cout << 0 << endl;
       exit(0);
     }
   }
   if (fin <= ini) {
     cout << 0 << endl;
     exit(0);
   }
   int libres = 0;
   long long int maneras = 1;
   for (int i = inf; i <= sup; i++) {
     if (not ocupado[i]) {
       maneras = (maneras + elev[libres]) % modulo;
       libres++;
     }
     if (i - k >= inf and not ocupado[i - k]) libres--;
   }
   cout << maneras << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool v[6];
 int a[100009][6], maxx = -1, t;
 long long m = 1e9 + 7;
 struct pi {
   long long ah[60][60];
 };
 pi multi(pi x, pi y) {
   pi z;
   for (int i = 1; i <= t; i++) {
     for (int j = 1; j <= t; j++) {
       z.ah[i][j] = 0;
       for (int k = 1; k <= t; k++)
         z.ah[i][j] = (z.ah[i][j] % m + x.ah[i][k] % m * y.ah[k][j] % m) % m;
       z.ah[i][j] %= m;
     }
   }
   return z;
 }
 pi quimul(pi x, long long int n) {
   pi an;
   for (int i = 1; i <= t; i++) an.ah[i][i] = 1;
   for (; n; n >>= 1) {
     if (n & 1) an = multi(an, x);
     x = multi(x, x);
   }
   return an;
 }
 int main() {
   long long n;
   int x, v, p[600], zh[90][90];
   char c = 'a', u;
   for (int i = 1; i <= 52; i++)
     for (int j = 1; j <= 52; j++) zh[i][j] = 1;
   for (int i = 1; i <= 26; i++) p[(int)c + i - 1] = i;
   c = 'A';
   for (int i = 27; i <= 52; i++) p[(int)c + i - 27] = i;
   cin >> n >> t >> v;
   for (int i = 0; i < v; i++) {
     scanf(  %c %c , &c, &u);
     zh[p[(int)c]][p[(int)u]] = 0;
   }
   pi ans;
   for (int i = 1; i <= t; i++)
     for (int j = 1; j <= t; j++) ans.ah[i][j] = zh[i][j];
   ans = quimul(ans, n - 1);
   long long int pp = 0;
   for (int i = 1; i <= t; i++)
     for (int j = 1; j <= t; j++) pp = (pp + ans.ah[i][j]) % m;
   cout << (pp + m) % m << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int a, b, c, n, i, j, money;
   cin >> a >> b >> c;
   cin >> n;
   int m[n];
   money = 0;
   for (i = 0; i < n; i++) {
     cin >> m[i];
     if (m[i] > b && m[i] < c) money = money + 1;
   }
   if (a > b && a < c)
     cout << money;
   else
     cout << 0;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 void display(T a[], int size) {
   for (int i = 0; i < size; i++) {
     cout << a[i] <<    ;
   }
   cout << endl;
 }
 void display(vector<pair<int, int> > a) {
   for (int i = 0; i < a.size(); i++) {
     cout <<  (  << a[i].first <<  ,  << a[i].second <<  )
          <<    ;
   }
   cout << endl;
 }
 void display(const vector<int>& a) {
   for (int i = 0; i < a.size(); i++) {
     cout << a[i];
     if (i == a.size() - 1)
       cout << endl;
     else
       cout <<    ;
   }
 }
 template <typename T>
 void initialise(T a[], T value, int length) {
   for (int i = 0; i < length; i++) a[i] = value;
 }
 template <typename T>
 void initialise(vector<T>& a, T value) {
   for (int i = 0; i < a.size(); i++) a[i] = value;
 }
 bool compare(pair<int, int> a, pair<int, int> b) {
   if (a.first != b.first)
     return a.first < b.first;
   else
     return a.second < b.second;
 }
 int max(int a[], int n) {
   int max = -1000000000;
   for (int i = 0; i < n; i++) {
     if (a[i] > max) max = a[i];
   }
   return max;
 }
 bool find(string a[], string s, int n) {
   int left = 0;
   int right = n - 1;
   while (left < right) {
     int mid = (left + right) / 2;
     if (s.compare(a[mid]) == 0)
       return true;
     else if (s.compare(a[mid]) < 0) {
       right = mid;
     } else {
       left = mid + 1;
     }
   }
   return false;
 }
 void factor(int a[], int base, int num, int n) {
   for (int i = n - 1; i >= 0; i--) {
     a[i] = num % base;
     num = num / base;
   }
 }
 int findLength(int n, int base) {
   int i = 0;
   while (n > 0) {
     i++;
     n = n / base;
   }
   return i;
 }
 int gcd(int a, int b) {
   while (a % b != 0 && b % a != 0) {
     if (b > a) {
       b = b % a;
     } else if (a > b) {
       a = a % b;
     }
   }
   if (a <= b)
     return a;
   else
     return b;
 }
 int main() {
   int n;
   cin >> n;
   int dir[n];
   int step[n];
   for (int i = 0; i < n; i++) {
     char now;
     cin >> now;
     if (now == '>')
       dir[i] = 1;
     else
       dir[i] = -1;
   }
   for (int i = 0; i < n; i++) {
     cin >> step[i];
   }
   int trace[n];
   initialise(trace, 0, n);
   int curr = 0;
   while (true) {
     if (trace[curr] == 1) {
       cout <<  INFINITE  << endl;
       break;
     } else {
       trace[curr] = 1;
       curr = curr + dir[curr] * step[curr];
       if (curr >= n || curr < 0) {
         cout <<  FINITE  << endl;
         break;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, c = 0, d = 0, k;
   string str;
   cin >> n >> str;
   for (int i = 0; i < n; i++) {
     if (str[i] == '-') c++;
   }
   if (c == n) {
     cout << 0 << endl;
     return 0;
   } else if (str[0] == '-') {
     for (int i = 0; i < n; i++) {
       if (str[i] == '-') {
         continue;
       } else {
         k = i;
         break;
       }
     }
     for (int i = k; i < n; i++) {
       if (str[i] == '+')
         d += 1;
       else {
         d -= 1;
         if (d < 0) d = 0;
       }
     }
   } else {
     for (int i = 0; i < n; i++) {
       if (str[i] == '+')
         d += 1;
       else {
         d -= 1;
         if (d < 0) d = 0;
       }
     }
   }
   cout << d << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int M;
 const int MAX = 100001;
 vector<int> _size, marked, _pow, _ipow;
 vector<pair<int, int> > adjList[MAX + 1];
 int getSize(int u, int p = -1) {
   _size[u] = 1;
   for (auto [v, w] : adjList[u])
     if (v != p && !marked[v]) _size[u] += getSize(v, u);
   return _size[u];
 }
 int getCentroid(int u, int p, int n) {
   for (auto [v, w] : adjList[u])
     if (v != p && !marked[v] && _size[v] * 2 > n) return getCentroid(v, u, n);
   return u;
 }
 void add(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) {
   aux[rem]++;
   for (auto [v, w] : adjList[u])
     if (v != p && !marked[v])
       add(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M);
 }
 void remove(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) {
   aux[rem]--;
   for (auto [v, w] : adjList[u])
     if (v != p && !marked[v])
       remove(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M);
 }
 long long count(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) {
   int nxt = int(1LL * _ipow[lvl] * (M - rem) % M);
   long long ans = aux[nxt];
   for (auto [v, w] : adjList[u])
     if (v != p && !marked[v])
       ans += count(aux, v, u, lvl + 1, (rem * 10LL + w) % M);
   return ans;
 }
 long long solve(int u) {
   int N = getSize(u);
   u = getCentroid(u, -1, N);
   marked[u] = 1;
   unordered_map<int, int> aux;
   aux[0] = 1;
   for (auto [v, w] : adjList[u])
     if (!marked[v]) add(aux, v, u, 1, w % M);
   long long ans = aux[0] - 1;
   for (auto [v, w] : adjList[u])
     if (!marked[v]) {
       remove(aux, v, u, 1, w % M);
       ans += count(aux, v, u, 1, w % M);
       add(aux, v, u, 1, w % M);
     }
   aux.clear();
   for (auto [v, w] : adjList[u])
     if (!marked[v]) ans += solve(v);
   return ans;
 }
 int gcd(int a, int b, int &x, int &y) {
   if (b == 0) {
     x = 1;
     y = 0;
     return a;
   }
   int x1, y1;
   int d = gcd(b, a % b, x1, y1);
   x = y1;
   y = x1 - y1 * (a / b);
   return d;
 }
 int getInv() {
   int x, y;
   int g = gcd(10, M, x, y);
   assert(g == 1);
   return (x % M + M) % M;
 }
 int main() {
   ios_base ::sync_with_stdio(false);
   cin.tie(nullptr);
   int n;
   cin >> n >> M;
   _size = vector<int>(n), _pow = vector<int>(n + 1, 1),
   _ipow = vector<int>(n + 1, 1), marked = vector<int>(n + 1);
   _ipow[1] = getInv();
   _pow[1] = 10;
   for (int i = 2; i <= n; i++) {
     _pow[i] = int(1LL * _pow[i - 1] * 10 % M);
     _ipow[i] = int(1LL * _ipow[i - 1] * _ipow[1] % M);
   }
   for (int i = 0; i < n - 1; i++) {
     int u, v, w;
     cin >> u >> v >> w;
     adjList[u].push_back({v, w});
     adjList[v].push_back({u, w});
   }
   cout << solve(0) << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e6 + 100, M = 1e6 + 100;
 int n, up[N], down[M], nei[M][2];
 int lim;
 bool mark[M], bad[M];
 vector<int> vec;
 int f(int a) { return lower_bound(vec.begin(), vec.end(), a) - vec.begin(); }
 bool dfs(int v) {
   mark[v] = true;
   for (int i = 0; i < 2 && nei[v][i] <= lim; i++) {
     int u = nei[v][i];
     if (up[u] == -1 || (mark[up[u]] == false && dfs(up[u]))) {
       up[u] = v;
       down[v] = u;
       return true;
     }
   }
   return false;
 }
 bool ok(int X) {
   lim = X;
   fill(up, up + N, -1);
   fill(down, down + M, -1);
   memset(bad, false, sizeof bad);
   int matching = 0;
   bool done = false;
   for (int v = 0; v < n; v++)
     if (nei[v][0] > lim)
       return false;
     else if (nei[v][1] > lim) {
       if (up[nei[v][0]] != -1) return false;
       down[v] = nei[v][0];
       up[nei[v][0]] = v;
       bad[v] = true;
       matching++;
     }
   while (done == false) {
     done = true;
     copy(bad, bad + n, mark);
     for (int v = 0; v < n; v++)
       if (mark[v] == false && down[v] == -1 && dfs(v)) {
         matching++;
         done = false;
       }
   }
   return matching == n;
 }
 int main() {
   ios::sync_with_stdio(false), cin.tie(0);
   cin >> n;
   for (int i = 0; i < n; i++) {
     cin >> nei[i][0] >> nei[i][1];
     vec.push_back(nei[i][0]);
     vec.push_back(nei[i][1]);
   }
   sort(vec.begin(), vec.end());
   vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
   for (int i = 0; i < n; i++)
     nei[i][0] = f(nei[i][0]), nei[i][1] = f(nei[i][1]);
   if (ok(vec.size() - 1) == false) {
     cout << -1;
     return 0;
   }
   if (ok(vec.size() - 2) == false) {
     cout << vec.back();
     return 0;
   }
   int l = -1, r = vec.size() - 1;
   while (r - l > 1) {
     if (ok((l + r) >> 1))
       r = lim;
     else
       l = lim;
   }
   cout << vec[r];
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int val[200007], add[200007];
 int main() {
   int n, t, a, x, k, l = 1;
   long long sum = 0;
   scanf( %d , &n);
   for (int i = 0; i < n; ++i) {
     scanf( %d , &t);
     if (t == 1) {
       scanf( %d%d , &a, &x);
       add[a] += x;
       sum += a * x;
     } else if (t == 2) {
       scanf( %d , &k);
       val[++l] = k;
       sum += val[l];
     } else if (l > 1) {
       sum -= val[l];
       sum -= add[l];
       add[l - 1] += add[l];
       add[l] = 0;
       --l;
     }
     printf( %.6lf
 , (double)sum / l);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 constexpr int N = 2e5 + 10;
 long long n, w, k, happy[N], nice, t[N], mytime, ans;
 bool in[N];
 pair<int, int> lowest;
 set<pair<int, int>> st;
 queue<int> q;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> w >> k;
   for (int i = 0; i < n; i++) cin >> happy[i];
   for (int i = 0; i < n; i++) cin >> t[i];
   for (int i = n - 1; ~i; i--) {
     st.insert({t[i], -i});
     if ((int)st.size() == w) {
       lowest = *st.begin();
       mytime += (t[i] + 1) / 2;
       in[i] = true;
     } else if ((int)st.size() > w) {
       if (t[i] >= lowest.first) {
         in[-lowest.second] = false;
         mytime += (t[i] + 1) / 2;
         mytime -= (lowest.first + 1) / 2;
         mytime += lowest.first;
         lowest = *st.upper_bound(lowest);
         in[i] = true;
       } else
         mytime += t[i];
     } else {
       in[i] = true;
       mytime += (t[i] + 1) / 2;
     }
     nice += happy[i];
     q.push(i);
     while (mytime > k) {
       nice -= happy[q.front()];
       if (in[q.front()]) {
         mytime -= (t[q.front()] + 1) / 2;
         in[q.front()] = false;
         st.erase({t[q.front()], -q.front()});
         if ((int)st.size() >= w) {
           lowest = *(--st.lower_bound(lowest));
           in[-lowest.second] = true;
           mytime -= lowest.first;
           mytime += (lowest.first + 1) / 2;
         }
       } else {
         mytime -= t[q.front()];
         st.erase({t[q.front()], -q.front()});
       }
       q.pop();
     }
     ans = max(ans, nice);
   }
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline void speedUp() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
 }
 long long int t, n, m, p[100005], q[100005], Podd, Peven, Qodd, Qeven, tot;
 int main() {
   speedUp();
   int t, n;
   cin >> t;
   while (t--) {
     tot = Podd = Peven = Qodd = Qeven = 0;
     cin >> n;
     for (int i = 1; i <= n; i++) {
       cin >> p[i];
       if (p[i] % 2 == 0)
         Peven++;
       else
         Podd++;
     }
     cin >> m;
     for (int i = 1; i <= m; i++) {
       cin >> q[i];
       if (q[i] % 2 == 0)
         Qeven++;
       else
         Qodd++;
     }
     for (int i = 1; i <= n; i++) {
       if (p[i] % 2 == 0)
         tot += Qeven;
       else
         tot += Qodd;
     }
     cout << tot << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main(void) {
   int n;
   scanf( %d , &n);
   int v[n];
   for (int i = 0; i < n; i++) scanf( %d , v + i);
   if (n > 2) {
     for (int i = 0; i + 3 <= n; i++) {
       int a = v[i], b = v[i + 1], c = v[i + 2];
       int ans1 = -1, ans2 = -1;
       set<int> ms;
       ms.insert(a);
       ms.insert(b);
       ms.insert(c);
       if (ms.size() == 1)
         continue;
       else if (ms.size() == 3) {
         if (a == *ms.begin())
           ans1 = i, ans2 = i + 1;
         else if (a == *(++ms.begin()))
           ans1 = i, ans2 = i + 2;
         else if (a == *ms.rbegin())
           ans1 = i, ans2 = i + 1;
       } else if (ms.size() == 2) {
         if (a == c) {
           int d = -1;
           if (i > 0) {
             d = v[i - 1];
             if (d != a)
               ans1 = i - 1, ans2 = i;
             else
               ans1 = i, ans2 = i + 1;
           } else if (i + 3 < n) {
             d = v[i + 3];
             if (d != a)
               ans1 = i + 2, ans2 = i + 3;
             else
               ans1 = i + 1, ans2 = i + 2;
           } else
             continue;
         } else if (a == b)
           ans1 = i + 1, ans2 = i + 2;
         else if (b == c)
           ans1 = i, ans2 = i + 1;
       }
       printf( %d %d
 , ans1 + 1, ans2 + 1);
       return 0;
     }
   }
   printf( -1
 );
   return (0);
 }


#include <bits/stdc++.h>
 using namespace std;
 const double eps = 1e-2;
 struct cplx {
   double x, y;
   cplx(double x1 = 0, double x2 = 0) { x = x1, y = x2; }
 } g[2000001], f[2000001];
 cplx operator+(cplx a, cplx b) { return cplx(a.x + b.x, a.y + b.y); }
 cplx operator-(cplx a, cplx b) { return cplx(a.x - b.x, a.y - b.y); }
 cplx operator*(cplx a, cplx b) {
   return cplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
 }
 void swap(cplx &a, cplx &b) { swap(a.x, b.x), swap(a.y, b.y); }
 int a[2000001], b[2000001];
 int r[2000001];
 const double Pi = acos(-1);
 void fft(cplx f[], int op, int n) {
   for (int i = 0; i < n; i++)
     if (i < r[i]) swap(f[i], f[r[i]]);
   for (int w = 2; w <= n; w <<= 1) {
     int len = w / 2;
     cplx tmp(cos(Pi / len), op * sin(Pi / len));
     for (int k = 0; k < n; k += w) {
       cplx buf(1, 0);
       for (int l = k; l < k + len; l++) {
         cplx tt = f[len + l] * buf;
         f[len + l] = f[l] - tt;
         f[l] = f[l] + tt;
         buf = buf * tmp;
       }
     }
   }
   if (op == -1) {
     for (int i = 0; i < n; i++) f[i].x /= n, f[i].y /= n;
   }
 }
 char p[2000001];
 char q[2000001];
 int res[2000001];
 int l1, l2, k, n, m;
 void solve(char c) {
   for (int i = 0; i <= n; i++) f[i].x = f[i].y = g[i].x = g[i].y = 0;
   int far = -1;
   for (int i = 0; i < l1; i++) {
     if (p[i] == c) far = i + k;
     if (i <= far) f[i].x = 1;
   }
   far = l1;
   for (int i = l1 - 1; i >= 0; i--) {
     if (p[i] == c) far = i - k;
     if (i >= far) f[i].x = 1;
   }
   for (int i = 0; i < l2; i++)
     if (q[i] == c) g[i].x = 1;
   fft(f, 1, n);
   fft(g, 1, n);
   for (int i = 0; i < n; i++) f[i] = f[i] * g[i];
   fft(f, -1, n);
   for (int i = 0; i < n; i++) res[i] += (int)(f[i].x + eps);
 }
 signed main() {
   int ans = 0;
   scanf( %d%d%d , &l1, &l2, &k);
   cin >> p;
   cin >> q;
   reverse(p, p + l1);
   n = l1, m = l2;
   for (m += n, n = 1; n <= m; n <<= 1)
     ;
   for (int i = 0; i < n; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
   solve('A');
   solve('T');
   solve('G');
   solve('C');
   for (int i = 0; i <= l1 - l2; i++)
     if (res[l1 - i - 1] >= l2) ans++;
   printf( %d , ans);
   return 0;
 }


#include <bits/stdc++.h>
 struct edge {
   int to, nxt;
 } e[100005], e2[100005];
 int h[100005], n, cnt, hei[100005], gson[100005], node[100005], N, fa[100005];
 void ins(int x, int y) {
   e[++cnt].nxt = h[x];
   e[cnt].to = y;
   h[x] = cnt;
 }
 void dfs1(int x) {
   hei[x] = 1;
   for (int i = h[x]; i; i = e[i].nxt) {
     dfs1(e[i].to);
     hei[x] = std::max(hei[x], hei[e[i].to] + 1);
     if (hei[gson[x]] < hei[e[i].to]) gson[x] = e[i].to;
   }
 }
 void dfs2(int x) {
   node[++N] = x;
   for (int i = h[x]; i; i = e[i].nxt)
     if (e[i].to != gson[x]) dfs2(e[i].to);
   if (gson[x]) dfs2(gson[x]);
 }
 int main() {
   scanf( %d , &n);
   for (int i = 2; i <= n; i++) scanf( %d , &fa[i]), ins((++fa[i]), i);
   dfs1(1);
   dfs2(1);
   for (int i = 1; i <= n; i++) printf( %d  , node[i] - 1);
   printf(
%d
 , n - hei[1]);
   for (int i = 1, j = 1, k = 0; i <= n - hei[1]; i++) {
     while (j <= n && fa[node[j]] == k) k = node[j++];
     if (j > n) break;
     printf( %d  , node[j] - 1);
     k = fa[k];
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 char a[1005], b[1005];
 int main() {
   int s1 = 0, s2 = 0;
   scanf( %s %s , &a, &b);
   int len1 = strlen(a);
   int len2 = strlen(b);
   for (int i = 0; i < len1; i++)
     if (a[i] == '1') s1++;
   for (int i = 0; i < len2; i++)
     if (b[i] == '1') s2++;
   if (s1 & 1) s1++;
   if (s1 >= s2)
     puts( YES );
   else
     puts( NO );
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, Q, x, y, z, kk;
 int dis[500005], head[500005], father[500005], top[500005], q[500005];
 bool vis[500005];
 int far[500005], heavyson[500005], rak[500005], sum[500005], leaf[500005], ans,
     d[500005];
 struct Tree {
   int nxt, to, step;
 } e[500005];
 inline bool cmp(int u, int v) {
   return dis[u] - dis[father[top[u]]] > dis[v] - dis[father[top[v]]];
 }
 inline void link(int x, int y, int z) {
   e[++kk].nxt = head[x];
   e[kk].to = y;
   e[kk].step = z;
   head[x] = kk;
 }
 inline int bfs(int S) {
   int left1 = 1;
   int right1 = 1;
   q[left1] = S;
   memset(d, 0, sizeof(d));
   memset(vis, false, sizeof(vis));
   vis[S] = true;
   while (left1 <= right1) {
     int u = q[left1++];
     for (int i = head[u]; i; i = e[i].nxt) {
       int v = e[i].to;
       if (vis[v]) continue;
       vis[v] = true;
       q[++right1] = v;
       d[v] = d[u] + e[i].step;
     }
   }
   int kkk = 0;
   for (int i = 1; i <= n; i++) {
     if (d[i] > d[kkk]) kkk = i;
   }
   return kkk;
 }
 void dfs1(int u, int fa) {
   far[u] = dis[u];
   for (int i = head[u]; i; i = e[i].nxt) {
     int v = e[i].to;
     if (v == fa) continue;
     father[v] = u;
     dis[v] = dis[u] + e[i].step;
     dfs1(v, u);
     if (!heavyson[u] || far[u] < far[v]) heavyson[u] = v, far[u] = far[v];
   }
 }
 void dfs2(int u, int first) {
   top[u] = first;
   if (!heavyson[u]) {
     leaf[++leaf[0]] = u;
     return;
   }
   dfs2(heavyson[u], first);
   for (int i = head[u]; i; i = e[i].nxt) {
     int v = e[i].to;
     if (v == father[u] || v == heavyson[u]) continue;
     dfs2(v, v);
   }
 }
 inline int solve(int x, int y) {
   if (rak[top[x]] <= 2 * y - 1) return sum[min(y * 2 - 1, leaf[0])];
   int u = x;
   while (rak[top[u]] > y * 2 - 1) u = father[top[u]];
   return sum[y * 2 - 1] -
          min(min(sum[y * 2 - 1] - sum[y * 2 - 2], far[u] - dis[u]), dis[u]) +
          far[x] - dis[u];
 }
 int main() {
   scanf( %d%d , &n, &Q);
   for (int i = 1; i < n; i++) {
     scanf( %d%d%d , &x, &y, &z);
     link(x, y, z);
     link(y, x, z);
   }
   int first = bfs(1);
   int root = bfs(first);
   dfs1(root, -1);
   dfs2(root, root);
   sort(leaf + 1, leaf + leaf[0] + 1, cmp);
   for (int i = 1; i <= leaf[0]; i++) {
     rak[top[leaf[i]]] = i;
     sum[i] = sum[i - 1] + dis[leaf[i]] - dis[father[top[leaf[i]]]];
   }
   while (Q--) {
     scanf( %d%d , &x, &y);
     x = ((x + ans - 1) % n) + 1;
     y = ((y + ans - 1) % n) + 1;
     printf( %d
 , ans = solve(x, y));
   }
   return 0;
 }


#include<bits/stdc++.h>

 #define For(i, x, n) for(int i = x; i < n; i++)
 #define ROF(i,x,n) for(int i = n - 1; i >= x; i--)

 #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 #define F first
 #define S second
 #define pb push_back
 #define po pop_back
 #define mp make_pair
 #define En endl
 ///#define endl

 #define ALL(x) x.begin(), x.end()

 using namespace std;

 typedef double db;
 typedef long long LL;
 typedef string str;
 typedef pair<int, int> PII;
 typedef pair<LL, LL> PLL;

 typedef vector<int> VI;
 typedef vector<LL> VLL;
 typedef vector<bool> VB;
 typedef vector<char> VC;
 typedef vector<PII> VPII;
 typedef vector<string> VSTR;
 typedef set<int> SI;
 typedef set<LL> SLL;
 typedef set<PII> SPII;

 typedef const int CI;
 typedef const long long CLL;

 /// CI MAX_N = ;
 CI MOD = 1e9 + 7;

 int t, n, a;

 int main () {
     IOS;
     cin >> t;
     while (t--) {
         cin >> n;
         int sumz = 0;
         int sumf = 0;
         For (i, 0, 2 * n) {
             cin >> a;
             if (a % 2 == 0) sumz++;
             else sumf++;
         }
         if (sumz == sumf) cout <<  YES  << En;
         else cout <<  NO  << En;
     }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 200010;
 int n, m, X, a[maxn], b[maxn], c[maxn], d[maxn], tr[maxn];
 long long ans;
 vector<int> M[maxn];
 vector<pair<int, int>> Q[maxn];
 void add(int p) {
   for (; p <= X; p += p & -p) tr[p]++;
 }
 int sum(int p) {
   int s = 0;
   for (; p; p -= p & -p) s += tr[p];
   return s;
 }
 int main() {
   scanf( %d %d %d , &n, &m, &X);
   for (int i = 1; i <= n; i++) {
     scanf( %d , &a[i]), a[i] = min(a[i], X);
   }
   for (int i = 1; i <= m; i++) {
     scanf( %d , &b[i]), b[i] = min(b[i], X);
   }
   auto init = [&](int n, int *a, int *c) {
     static int mx[20][maxn], lb[maxn], rb[maxn];
     for (int i = 1; i <= n; i++) {
       mx[0][i] = a[i], lb[i] = rb[i] = 0;
     }
     for (int i = 1; i < 20; i++) {
       for (int j = 1; j + (1 << i) - 1 <= n; j++) {
         mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]);
       }
     }
     auto query = [&](int l, int r) {
       int k = log2(r - l + 1);
       return max(mx[k][l], mx[k][r - (1 << k) + 1]);
     };
     stack<int> st;
     for (int i = 1; i <= n; i++) {
       while (!st.empty() && a[st.top()] > a[i]) rb[st.top()] = i, st.pop();
       if (!st.empty()) lb[i] = st.top();
       st.push(i);
     }
     for (int i = 1; i <= n; i++) {
       c[i] = min(!lb[i] ? X : query(lb[i], i), !rb[i] ? X : query(i, rb[i]));
     }
   };
   init(n, a, c), init(m, b, d);
   for (int i = 1; i <= n; i++) {
     Q[X - c[i]].emplace_back(a[i], -1);
     Q[X - a[i]].emplace_back(a[i], 1);
   }
   for (int i = 1; i <= m; i++) {
     M[b[i]].push_back(X - d[i] + 1);
   }
   for (int i = 0; i < X; i++) {
     for (int d : M[i]) add(d);
     for (auto p : Q[i]) ans += p.second * sum(p.first);
   }
   printf( %lld
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int n;
   scanf( %d , &n);
   int a[n], b[n];
   long int s = 0;
   for (int i = 0; i < n; i++) scanf( %d%d , &a[i], &b[i]);
   for (int i = 1; i < n; i++) {
     if (b[i] >= b[i - 1]) b[i] = b[i - 1];
     s += (a[i] * b[i]);
   }
   printf( %ld
 , s + (a[0] * b[0]));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int a = 1023, b = 0, n, e;
   char op;
   cin >> n;
   while (n--) {
     cin >> op >> e;
     if (op == '|') {
       a |= e;
       b |= e;
     } else if (op == '&') {
       a &= e;
       b &= e;
     } else if (op == '^') {
       a ^= e;
       b ^= e;
     }
   }
   int ouxor = 0;
   int ou = 0;
   int exor = 0;
   e = 0;
   for (int i = 0; i < 10; i++) {
     int num = 1 << i;
     if ((a & num) && (b & num))
       ou |= num;
     else if ((b & num))
       exor |= num;
     else if (!(a & num) && !(b & num))
       ouxor |= num;
   }
   int count = 0;
   count += ouxor ? 2 : 0;
   count += ou ? 1 : 0;
   count += exor ? 1 : 0;
   cout << count << endl;
   if (ou) cout <<  |   << ou << endl;
   if (ouxor) {
     cout <<  |   << ouxor << endl;
     cout <<  ^   << ouxor << endl;
   }
   if (exor) cout <<  ^   << exor << endl;
 }


#include <bits/stdc++.h>
 int main() {
   int t;
   scanf( %d , &t);
   int a, b, c, d, k, x, y;
   while (t--) {
     scanf( %d %d %d %d %d , &a, &b, &c, &d, &k);
     if (a % c == 0) {
       x = a / c;
     } else {
       x = a / c + 1;
     }
     if (b % d == 0) {
       y = b / d;
     } else {
       y = b / d + 1;
     }
     if (x + y > k) {
       printf( -1
 );
     } else
       printf( %d %d
 , k - y, y);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long INF = (long long)1e15 + 696969;
 long long n, k;
 long long a[1LL << 20], b[1LL << 20];
 long long opt[20][1LL << 20];
 signed main() {
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   memset(opt, -1, sizeof opt);
   cin >> n;
   for (long long i = 1; i < n; i <<= 1) k++;
   long long ok = false;
   for (long long i = 0; i < n; i++) {
     long long x;
     cin >> x;
     if (x == -1)
       ok = true;
     else if (!ok)
       a[i] = 0;
     else
       a[i - 1] = x;
   }
   n--;
   reverse(a, a + n);
   for (long long i = k - 1, j = 0; i > 0; i--) {
     j += 1 << i;
     b[i] = j;
   }
   for (long long i = 0; i < n; i++) {
     opt[0][i] = a[i];
   }
   for (long long i = 1; i < k; i++) {
     long long mn = INF;
     for (long long j = n - 1; j >= 0; j--) {
       if (j > b[i])
         opt[i][j] = INF;
       else {
         opt[i][j] = mn + a[j];
         mn = min(mn, opt[i - 1][j]);
       }
     }
   }
   cout << opt[k - 1][0] << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     long long x1, y1, z1;
     long long x2, y2, z2;
     cin >> x1 >> y1 >> z1;
     cin >> x2 >> y2 >> z2;
     long long sum = 0;
     if (z2 > x1) {
       z2 -= x1;
       if (z2 > z1) {
         z2 -= z1;
         sum -= (2 * z2);
       } else {
         z1 -= z2;
         sum += 2 * min(z1, y2);
       }
     } else {
       sum += 2 * min(z1, y2);
     }
     cout << sum << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename Arg1>
 void __f(const char* name, Arg1&& arg1) {
   cerr << name <<   :   << arg1 << std::endl;
 }
 template <typename Arg1, typename... Args>
 void __f(const char* names, Arg1&& arg1, Args&&... args) {
   const char* comma = strchr(names + 1, ',');
   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;
   __f(comma + 1, args...);
 }
 const int N = 1510;
 int DP[27][N][N];
 int ans[27][N];
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n;
   cin >> n;
   string s;
   cin >> s;
   memset(DP, 0, sizeof(DP));
   memset(ans, 0, sizeof(ans));
   DP[s[0] - 'a'][0][0] = 1;
   for (int i = (int)1; i < (int)N; i++) DP[s[0] - 'a'][0][i] = 1;
   for (char ch = 'a'; ch <= 'z'; ch++) {
     if (s[0] == ch) continue;
     for (int i = (int)1; i < (int)N; i++) DP[ch - 'a'][0][i] = 1;
   }
   for (int i = (int)1; i < (int)(int)(s.size()); i++) {
     for (char ch = 'a'; ch <= 'z'; ch++) {
       if (s[i] == ch) {
         int len = 0;
         for (int k = i; k >= 0; k--) {
           if (s[k] != ch) break;
           len++;
         }
         DP[ch - 'a'][i][0] = len;
       }
       for (int j = (int)1; j < (int)N; j++) {
         if (ch == s[i])
           DP[ch - 'a'][i][j] = 1 + DP[ch - 'a'][i - 1][j];
         else
           DP[ch - 'a'][i][j] = 1 + DP[ch - 'a'][i - 1][j - 1];
       }
     }
   }
   for (char ch = 'a'; ch <= 'z'; ch++) {
     for (int j = 0; j < N; j++) {
       for (int i = (int)0; i < (int)(int)(s.size()); i++)
         ans[ch - 'a'][j] = max(ans[ch - 'a'][j], DP[ch - 'a'][i][j]);
     }
   }
   int q;
   cin >> q;
   for (int i = (int)0; i < (int)q; i++) {
     int m;
     char ch;
     cin >> m >> ch;
     cout << ans[ch - 'a'][m] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = INT_MAX;
 const long long INFL = LLONG_MAX;
 int N, ans, P[100100], ord[100100];
 int comp(int a, int b) { return P[a] > P[b]; }
 int main() {
   ios_base::sync_with_stdio(0);
   cout.precision(15);
   cin >> N;
   for (int(i) = 1; (i) <= (N); (i)++) {
     cin >> P[i];
     ord[i] = i;
   }
   sort(ord + 1, ord + 1 + N, comp);
   cout << ord[1] <<     << P[ord[2]] << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   vector<int> a(n);
   for (int i = 0; i < n; i++) {
     cin >> a[i];
   }
   int inv = 0;
   for (int i = 1; i < n; i++) {
     for (int j = 0; j < i; j++) {
       if (a[i] < a[j]) {
         inv++;
       }
     }
   }
   inv %= 2;
   int m;
   cin >> m;
   for (int i = 0; i < m; i++) {
     int l, r;
     cin >> l >> r;
     int len = r - l + 1;
     int di = (len * (len - 1)) / 2;
     inv += di;
     inv %= 2;
     if (inv == 1) {
       cout <<  odd  << endl;
     } else {
       cout <<  even  << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 const int MAXN = 23;
 int parseInt() {
   int d;
   scanf( %d , &d);
   ;
   return d;
 }
 int n;
 int a[MAXN], b[MAXN];
 vector<pair<int, int> > v;
 map<int, int> rang;
 int main() {
   cin >> n;
   for (int i = 0; i < n; ++i) {
     a[i] = parseInt();
     v.push_back(make_pair(a[i], i));
   }
   sort(v.begin(), v.end());
   for (int i = 0; i < n; ++i) {
     rang[v[i].first] = i;
     b[i] = v[i].first;
   }
   int rng;
   for (int i = 0; i < n; ++i) {
     rng = rang[a[i]];
     rng = (rng + 1) % n;
     cout << b[rng] <<    ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int P = 1e9 + 7, INF = 0x3f3f3f3f;
 long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
 long long qpow(long long a, long long n) {
   long long r = 1 % P;
   for (a %= P; n; a = a * a % P, n >>= 1)
     if (n & 1) r = r * a % P;
   return r;
 }
 long long inv(long long first) {
   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P;
 }
 inline int rd() {
   int first = 0;
   char p = getchar();
   while (p < '0' || p > '9') p = getchar();
   while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
   return first;
 }
 const int N = 1e6 + 10;
 int n, a, b;
 struct _ {
   long long a, b, c;
   void pr() { printf( a=%lld,b=%lld
 , a, b); }
   bool operator<(const _& rhs) const { return c > rhs.c; }
 } e[N];
 long long ans;
 int vis[N];
 int main() {
   scanf( %d%d%d , &n, &a, &b);
   for (int i = 1; i <= n; ++i) {
     scanf( %lld%lld , &e[i].a, &e[i].b);
     e[i].c = e[i].a - e[i].b;
   }
   for (int i = 1; i <= n; ++i) ans += e[i].b;
   if (!b) return printf( %lld
 , ans), 0;
   if (b == 1) {
     long long t = 0;
     for (int i = 1; i <= n; ++i) t = max(t, (e[i].a << a) - e[i].b);
     return printf( %lld
 , ans + t), 0;
   }
   sort(e + 1, e + 1 + n);
   for (int i = 1; i <= n; ++i)
     if (e[i].a > e[i].b) vis[i] = 1;
   int cnt = 0;
   for (int i = 1; i <= b; ++i)
     if (vis[i]) ans += e[i].a - e[i].b, ++cnt;
   long long t = 0;
   for (int i = 1; i <= b; ++i) {
     if (vis[i])
       t = max(t, (e[i].a << a) - e[i].a);
     else
       t = max(t, (e[i].a << a) - e[i].b);
   }
   long long ret = ans + t;
   t = 0;
   for (int i = b + 1; i <= n; ++i) t = max(t, (e[i].a << a) - e[i].b);
   if (cnt < b)
     ret = max(ret, ans + t);
   else
     ret = max(ret, ans + t - e[b].a + e[b].b);
   printf( %lld
 , ret);
 }


#include <bits/stdc++.h>
 using namespace std;
 int Length[1001], n, x, a[1001], Concern, Pos, Ppl;
 vector<int> Queues, adj[1001], Ans;
 int32_t main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ;
   cin >> n >> x;
   for (int i = 1; i <= n; ++i) {
     cin >> a[i];
     if (a[i]) adj[a[i]].push_back(i);
   }
   for (int i = 1; i <= n; ++i) {
     if (a[i] == 0) {
       Ppl = i;
       int len = 0;
       while (Ppl) {
         ++len;
         if (Ppl == x) Concern = i, Pos = len;
         if (adj[Ppl].size())
           Ppl = adj[Ppl][0];
         else
           Ppl = 0;
       }
       if (Concern != i) Queues.push_back(len);
     }
   }
   for (int i = Queues.size() - 1; i >= 0; --i) {
     vector<int> Add;
     for (int j = 0; j < Ans.size(); ++j) {
       if (!Length[Ans[j] + Queues[i]])
         Add.push_back(Ans[j] + Queues[i]), Length[Ans[j] + Queues[i]] = 1;
     }
     for (int j = 0; j < Add.size(); ++j) Ans.push_back(Add[j]);
     if (!Length[Queues[i]]) Ans.push_back(Queues[i]), Length[Queues[i]] = 1;
   }
   cout << Pos <<
 ;
   sort(Ans.begin(), Ans.end());
   for (int i = 0; i < Ans.size(); ++i) cout << Pos + Ans[i] <<
 ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int Min = -10000000;
 int Max = -Min;
 int A[8] = {1, 1, 0, 0, 1, 1, 1, 1};
 int B[8] = {0, 0, 1, 1, 1, 1, -1, -1};
 int C[8];
 struct Nodes {
   int x, y;
 } p[100010];
 bool operator<(Nodes &p1, Nodes p2) {
   if (p1.x == p2.x) return p1.y < p2.y;
   return p1.x < p2.x;
 }
 long long cross(long long x1, long long y1, long long x2, long long y2) {
   return x1 * y2 - y1 * x2;
 }
 int main() {
   int i, j, k;
   int n;
   long long a, b, c, res;
   while (scanf( %d , &n) != EOF) {
     for (i = 0; i < 8; i++) {
       if (i & 1) {
         C[i] = Max;
       } else
         C[i] = Min;
     }
     for (i = 1; i <= n; i++) {
       scanf( %d%d , &p[i].x, &p[i].y);
       C[0] = max(C[0], p[i].x);
       C[1] = min(C[1], p[i].x);
       C[2] = max(C[2], p[i].y);
       C[3] = min(C[3], p[i].y);
       C[4] = max(C[4], p[i].x + p[i].y);
       C[5] = min(C[5], p[i].x + p[i].y);
       C[6] = max(C[6], p[i].x - p[i].y);
       C[7] = min(C[7], p[i].x - p[i].y);
     }
     res = 0;
     for (i = 0; i < 8; i++) {
       Nodes p1, p2;
       p1.x = p1.y = Min;
       p2.x = p2.y = Max;
       for (j = 0; j < 8; j += 2) {
         Nodes tmp[2];
         for (k = j; k < j + 2; k++) {
           c = cross(A[i], B[i], A[k], B[k]);
           a = cross(A[i], C[i], A[k], C[k]);
           b = cross(C[i], B[i], C[k], B[k]);
           if (c == 0) break;
           if (b % c || a % c) break;
           tmp[k - j].x = b / c;
           tmp[k - j].y = a / c;
         }
         if (k < j + 2) continue;
         if (tmp[1] < tmp[0]) swap(tmp[0], tmp[1]);
         if (p1 < tmp[0]) p1 = tmp[0];
         if (tmp[1] < p2) p2 = tmp[1];
       }
       res += max(abs(p2.x - p1.x), abs(p2.y - p1.y));
     }
     cout << res + 4 << endl;
   }
   return 0;
 }