#include <bits/stdc++.h>
 using namespace std;
 template <class t>
 t mag(t a) {
   return a > 0 ? a : -a;
 }
 template <class T, class U>
 T cdiv(T num, U den) {
   return (num / den) + ((num % den) > 0);
 }
 std::ifstream term( /dev/tty );
 std::string tempstring;
 using ll = int_fast64_t;
 using ii = pair<int, int>;
 using vi = vector<int>;
 using vl = vector<ll>;
 using vii = vector<ii>;
 const int &sij = 400010;
 const ll &mod = 998244353ll;
 int n;
 ll add(ll a, ll b) { return (a + b) % mod; }
 int esc(int tcase) {
   cin >> n;
   vi vec(n);
   for (auto &e : vec) cin >> e;
   vl dp0(201), sdp0(201), dp1(201), sdp1(201);
   for (int i = 1; i <= 200; i++) {
     if (vec.back() == -1 or i == vec.back()) dp0[i] = 1;
   }
   partial_sum(dp0.begin(), dp0.end(), sdp0.begin(), add);
   partial_sum(dp1.begin(), dp1.end(), sdp1.begin(), add);
   ;
   auto get0 = [&](int l, int r) -> ll {
     if (r < l) return 0;
     return (sdp0[r] - sdp0[l - 1] + mod) % mod;
   };
   auto get1 = [&](int l, int r) -> ll {
     if (r < l) return 0;
     return (sdp1[r] - sdp1[l - 1] + mod) % mod;
   };
   for (auto i = n - 2; i >= 0; i--) {
     vl ndp0(201), ndp1(201);
     for (int x = 1; x <= 200; x++) {
       if (vec[i] == -1 or x == vec[i]) {
         ndp0[x] = (get0(1, x - 1) + get1(1, x - 1)) % mod;
         ndp1[x] = (get1(x + 1, 200) + get1(x, x) + get0(x, x)) % mod;
       }
     }
     partial_sum(ndp0.begin(), ndp0.end(), sdp0.begin(), add);
     partial_sum(ndp1.begin(), ndp1.end(), sdp1.begin(), add);
     ;
     ;
     ;
   }
   cout << get1(1, 200) << '
';
   return 0;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   int tc = 1;
   for (int i = 1; i <= tc; i++) esc(i);
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long INF = 1 << 28;
 const long long LINF = 1ll << 61;
 inline long long getnum() {
   register long long r = 0;
   register bool ng = 0;
   register char c;
   c = getchar();
   while (c != '-' && (c < '0' || c > '9')) c = getchar();
   if (c == '-') ng = 1, c = getchar();
   while (c != ' ' && c != '
') r = r * 10 + c - '0', c = getchar();
   if (ng) r = -r;
   return r;
 }
 template <class T>
 inline void putnum(T x) {
   if (x < 0) putchar('-'), x = -x;
   register short a[20] = {}, sz = 0;
   while (x > 0) a[sz++] = x % 10, x /= 10;
   if (sz == 0) putchar('0');
   for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
 }
 inline void putsp() { putchar(' '); }
 inline void putendl() { putchar('
'); }
 inline char mygetchar() {
   register char c = getchar();
   while (c == ' ' || c == '
') c = getchar();
   return c;
 }
 const int dx[] = {1, -1, 0, 0};
 const int dy[] = {0, 0, 1, -1};
 const int maxn = 1700;
 int w, h, c[maxn][maxn], ans[12345];
 bool g[maxn][maxn], ng[2][maxn][maxn], vis[maxn][maxn], p;
 void color(int x, int y, int id) {
   c[x][y] = id;
   for (int i = 0; i < 4; i++) {
     int nx = x + dx[i], ny = y + dy[i];
     if (nx > 0 && nx <= w && ny > 0 && ny <= h && g[nx][ny] && c[nx][ny] == 0) {
       color(nx, ny, id);
     }
   }
 }
 void dfs(int x, int y) {
   vis[x][y] = 1;
   for (int i = 0; i < 4; i++) {
     int nx = x + dx[i], ny = y + dy[i];
     if (nx > 0 && nx <= w && ny > 0 && ny <= h && g[nx][ny] && !vis[nx][ny]) {
       dfs(nx, ny);
     }
   }
 }
 void getsmaller() {
   p ^= 1;
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       ng[p][i][j] = ng[p ^ 1][i][j];
       for (int k = 0; k < 4; k++) {
         if (!ng[p ^ 1][i + dx[k]][j + dy[k]]) {
           ng[p][i][j] = 0;
         }
       }
     }
   }
 }
 void getbigger() {
   p ^= 1;
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       ng[p][i][j] = ng[p ^ 1][i][j];
       for (int k = 0; k < 4; k++) {
         if (ng[p ^ 1][i + dx[k]][j + dy[k]]) {
           ng[p][i][j] = 1;
         }
       }
     }
   }
 }
 void see() {
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       putnum(g[i][j] & (!vis[i][j])), putsp();
     }
     putendl();
   }
   putendl();
 }
 int main() {
   w = getnum(), h = getnum();
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       g[i][j] = getnum();
     }
   }
   int id = 0;
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       if (g[i][j] && c[i][j] == 0) {
         id++;
         color(i, j, id);
       }
     }
   }
   for (int i = 1; i <= w; i++)
     for (int j = 1; j <= h; j++) ng[p][i][j] = g[i][j];
   getsmaller();
   getsmaller();
   getsmaller();
   getbigger();
   getbigger();
   getbigger();
   getbigger();
   getbigger();
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       g[i][j] &= !ng[p][i][j];
     }
   }
   for (int i = 1; i <= w; i++)
     for (int j = 1; j <= h; j++) ng[p][i][j] = g[i][j];
   getsmaller();
   getbigger();
   for (int i = 1; i <= w; i++)
     for (int j = 1; j <= h; j++) g[i][j] = ng[p][i][j];
   for (int i = 1; i <= w; i++) {
     for (int j = 1; j <= h; j++) {
       if (g[i][j] && !vis[i][j]) {
         ans[c[i][j]]++;
         dfs(i, j);
       }
     }
   }
   sort(ans + 1, ans + id + 1);
   putnum(id), putsp(), putendl();
   for (int i = 1; i <= id; i++) putnum(ans[i]), putsp();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 class Solve {
  public:
   int num;
   int inputNum[2005];
   int inversion[2005][2];
   int sequence[2005];

  public:
   Solve() {
     num = 0;
     memset(inputNum, 0, sizeof(inputNum));
     memset(inversion, 0, sizeof(inversion));
     memset(sequence, 0, sizeof(sequence));
   }
   void stat() {
     for (int i = 0; i < num; ++i) {
       for (int j = 0; j < i; ++j) {
         if (inputNum[j] < inputNum[i]) {
           ++inversion[i][0];
         }
       }
     }
     for (int i = 0; i < num; ++i) {
       for (int j = i + 1; j < num; ++j) {
         if (inputNum[i] > inputNum[j]) {
           ++inversion[i][1];
         }
       }
     }
   }
   void calculate() {
     for (int i = 0; i < num; ++i) {
       if (inversion[i][0] < inversion[i][1]) {
         sequence[i] = -inputNum[i];
       } else {
         sequence[i] = inputNum[i];
       }
     }
   }
   int getResult() {
     int total = 0;
     for (int i = 0; i < num; ++i) {
       for (int j = i + 1; j < num; ++j) {
         if (sequence[i] > sequence[j]) {
           ++total;
         }
       }
     }
     return total;
   }
   void readData() {
     cin >> num;
     for (int i = 0; i < num; ++i) {
       cin >> inputNum[i];
       if (inputNum[i] < 0) {
         inputNum[i] = -1 * inputNum[i];
       }
     }
   }
   int facade() {
     readData();
     stat();
     calculate();
     return getResult();
   }
 };
 int main() {
   Solve solve;
   cout << solve.facade() << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m, x, y;
   cin >> n >> m;
   y = 3 * m;
   x = 2 * n;
   for (int i = 6; i <= x && i <= y; i += 6) {
     if (x <= y)
       x += 2;
     else
       y += 3;
   }
   cout << max(x, y);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 505;
 bool arr[N][N];
 int dp[N][N];
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n, m, k;
   cin >> n >> m >> k;
   for (int i = 1; i <= n; i++) {
     string s;
     cin >> s;
     for (int j = 1; j <= m; j++) {
       arr[i][j] = (int)(s[j - 1] - '0');
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= m; j++) {
       if (arr[i][j] && arr[i - 1][j] && arr[i + 1][j] && arr[i][j - 1] &&
           arr[i][j + 1]) {
         dp[i][j] = 1;
       }
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= m; j++) {
       dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
     }
   }
   long long int ans = 0;
   for (int a = 2; a < n; a++) {
     for (int b = a; b < n; b++) {
       int ptr = 1;
       for (int c = 2; c < m; c++) {
         while (ptr < m &&
                (dp[a - 1][c - 1] - dp[b][c - 1] - dp[a - 1][ptr] + dp[b][ptr] <
                 k)) {
           ptr++;
         }
         ans += (m - ptr);
       }
     }
   }
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 static long long mi[400005][20];
 static bool mark[400005][20];
 static long long psum[400005];
 long long f(int x, int k) {
   if (k == 0) return psum[x];
   if (mark[x][k]) return mi[x][k];
   mark[x][k] = 1;
   return mi[x][k] = min(f(x, k - 1), f(x + (1 << (k - 1)), k - 1));
 }
 long long qry(int l, int r) {
   int t = r - l + 1;
   int lg = 31 - __builtin_clz(t);
   return min(f(l, lg), f(r - (1 << lg) + 1, lg));
 }
 int main() {
   long long n, a, b, c, d, start, len;
   scanf( %lld %lld %lld %lld %lld %lld %lld , &n, &a, &b, &c, &d, &start, &len);
   vector<pair<long long, long long> > v;
   for (long long i = 0; i < n; i++) {
     long long x, y;
     scanf( %lld %lld , &x, &y);
     v.push_back(make_pair(y, x));
   }
   if (v[0].first == 0)
     psum[0] = -d;
   else
     psum[0] = c;
   for (int i = 1; i < n; i++) {
     if (v[i].first)
       psum[i] = psum[i - 1] + c;
     else
       psum[i] = psum[i - 1] - d;
   }
   psum[n] = 1000000000000000000;
   vector<long long> t;
   t.push_back(0);
   for (int i = 0; i < n; i++) {
     t.push_back(v[i].second + 1);
     if (v[i].second - len + 1 >= 0) t.push_back(v[i].second - len + 1);
   }
   sort(t.begin(), t.end());
   int l = 0, r = 0;
   long long at = start;
   for (int i = 0; i < t.size(); i++) {
     while (l < v.size() && v[l].second < t[i]) {
       if (v[l].first)
         at += a;
       else
         at -= b;
       if (at < 0) {
         printf( -1
 );
         return 0;
       }
       l++;
     }
     while (r < v.size() && v[r].second < t[i] + len) r++;
     long long low = qry(l, r - 1) - (l > 0 ? psum[l - 1] : 0) + at;
     if (low >= 0) {
       printf( %lld
 , t[i]);
       return 0;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int get() {
   char ch;
   while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
     ;
   if (ch == '-') {
     int s = 0;
     while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
     return -s;
   }
   int s = ch - '0';
   while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
   return s;
 }
 const int mo = 1e9 + 7;
 const int N = 2e6 + 5;
 int cnt[N];
 int val[N];
 int lst[N];
 int pri[N], k;
 int n;
 int tim[N];
 bool bz[N];
 int main() {
   n = get();
   for (int i = 1; i <= n; i++) {
     int x = get();
     cnt[x]++;
   }
   lst[1] = 1;
   for (int i = 2; i <= 2e6; i++) {
     if (!lst[i]) pri[++k] = lst[i] = i;
     for (int j = 1; j <= k; j++) {
       if (1ll * pri[j] * i > 2e6) break;
       lst[i * pri[j]] = pri[j];
       if (i % pri[j] == 0) break;
     }
   }
   for (int i = 1; i <= 2e6; i++) val[i] = 1;
   int C = 0;
   for (int i = 2e6; i >= 2; i--) {
     for (int T = 1; T <= cnt[i]; T++) {
       if (val[i] < i)
         bz[i] = 1, tim[i] = 1, val[i] = i;
       else {
         bz[i - 1] = 1;
         int x = i - 1;
         while (x > 1) {
           int v = lst[x];
           int t = 1;
           while (x % v == 0) {
             x /= v;
             t = t * v;
           }
           if (t > val[v]) {
             val[v] = t;
             tim[v] = 1;
           } else if (val[v] == t)
             tim[v]++;
         }
       }
     }
   }
   for (int i = 1; i <= 2e6; i++)
     if (bz[i]) {
       bool can = 1;
       for (int x = i; x > 1;) {
         int v = lst[x], t = 1;
         for (; x % v == 0;) x /= v, t *= v;
         if (val[v] == t && tim[v] == 1) can = 0;
       }
       if (can) {
         C = 1;
         break;
       }
     }
   int ans = 1;
   for (int i = 1; i <= 2e6; i++) ans = 1ll * ans * val[i] % mo;
   ans = (ans + C) % mo;
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool vis[50];
 bool OK(vector<set<int> > v) {
   for (int i = 0; i < (int)v.size(); i++)
     if ((int)v[i].size() > 3) return 1;
   return 0;
 }
 int Find(vector<set<int> > v, int x) {
   for (int i = 0; i < (int)v.size(); i++)
     if (find(v[i].begin(), v[i].end(), x) != v[i].end()) return i;
   return -1;
 }
 int main() {
   int n, q, x, y, ex, ey, c = 0;
   scanf( %d %d , &n, &q);
   if (n % 3) {
     cout << -1;
     return 0;
   }
   vector<set<int> > v(n / 3);
   while (q--) {
     scanf( %d %d , &x, &y);
     vis[x] = 1, vis[y] = 1;
     ex = Find(v, x), ey = Find(v, y);
     if (ex != -1)
       v[ex].insert(y);
     else if (ey != -1)
       v[ey].insert(x);
     else {
       if (c == n / 3) {
         cout << -1;
         return 0;
       }
       v[c].insert(x), v[c].insert(y), c++;
     }
   }
   if (OK(v)) {
     cout << -1;
     return 0;
   }
   c = 1;
   for (int i = 0; i < (int)v.size(); i++) {
     for (int j = 0; j < 3 - (int)v[i].size(); j++) {
       for (int k = c; k < n + 1; k++)
         if (!vis[k]) {
           v[i].insert(k);
           c = k + 1;
           j--;
           break;
         }
     }
   }
   for (int i = 0; i < (int)v.size(); i++) {
     for (set<int>::iterator it = v[i].begin(); it != v[i].end(); it++)
       printf( %d  , *it);
     printf(
 );
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long w1, h1, w2, h2;
   cin >> w1 >> h1 >> w2 >> h2;
   long long ans = h1 + h2 + 2;
   ans += h1 + h2 + 2 + (w1 - w2);
   ans += w1 + w2;
   cout << ans;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 100005;
 vector<int> cnt[N];
 int res[N];
 int main() {
   int n;
   while (cin >> n) {
     for (int i = 1; i < N; i++) cnt[i].clear();
     for (int i = 1; i <= n; i++) {
       int v;
       cin >> v;
       cnt[v].push_back(i);
     }
     bool ok = true;
     for (int i = 1; i < N; i++) {
       if (i > 1 && cnt[i].size() > cnt[i - 1].size()) ok = false;
       for (int j = 0; j < cnt[i].size(); j++) {
         res[cnt[i][j]] = j + 1;
       }
     }
     if (!ok) {
       printf( %d
 , -1);
     } else {
       printf( %d
 , cnt[1].size());
       for (int i = 1; i <= n; i++) printf( %d%c , res[i], i == n ? '
' : ' ');
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   while (n--) {
     int a, s, d;
     cin >> a >> s >> d;
     if (a > s) swap(a, s);
     if (a > d) swap(a, d);
     if (s > d) swap(s, d);
     if (s == d) {
       cout <<  YES  << endl;
       cout << a <<     << a <<     << d << endl;
     } else {
       cout <<  NO  << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1005;
 const int INF = 1e9;
 pair<int, int> NULLPOINT = make_pair(INF, INF);
 int n;
 int gcd(int x, int y) {
   while (y) {
     int t = x;
     x = y;
     y = t % y;
   }
   return x;
 }
 struct Line {
   int x, y, xx, yy;
   pair<int, int> slope() {
     pair<int, int> ret = {x - xx, y - yy};
     if (ret.second < 0) {
       ret.first *= -1;
       ret.second *= -1;
     }
     int g = gcd(abs(ret.first), ret.second);
     ret.first /= g, ret.second /= g;
     return ret;
   }
   pair<int, int> intersect(const Line &l) const {
     long long den =
         1LL * (x - xx) * (l.y - l.yy) - 1LL * (y - yy) * (l.x - l.xx);
     if (den == 0) return NULLPOINT;
     long long c1 = 1LL * x * yy - 1LL * y * xx;
     long long c2 = 1LL * l.x * l.yy - 1LL * l.y * l.xx;
     long long ix = c1 * (l.x - l.xx) - (x - xx) * c2;
     long long iy = c1 * (l.y - l.yy) - (y - yy) * c2;
     if (ix % den != 0) return NULLPOINT;
     if (iy % den != 0) return NULLPOINT;
     ix /= den;
     iy /= den;
     if (ix < min(x, xx) || ix > max(x, xx) || ix < min(l.x, l.xx) ||
         ix > max(l.x, l.xx))
       return NULLPOINT;
     if (iy < min(y, yy) || iy > max(y, yy) || iy < min(l.y, l.yy) ||
         iy > max(l.y, l.yy))
       return NULLPOINT;
     return pair<int, int>(ix, iy);
   }
 };
 Line lines[N];
 int main() {
   scanf( %d , &n);
   for (int i = 0; i < n; ++i) {
     scanf( %d %d %d %d , &lines[i].x, &lines[i].y, &lines[i].xx, &lines[i].yy);
   }
   long long ans = 0;
   for (int i = 0; i < n; ++i) {
     pair<int, int> slope = lines[i].slope();
     if (slope.first == 0)
       ans += abs((lines[i].y - lines[i].yy) / slope.second) + 1;
     else
       ans += abs((lines[i].x - lines[i].xx) / slope.first) + 1;
   }
   set<pair<int, int>> intersects;
   intersects.insert(NULLPOINT);
   for (int i = 0; i < n; ++i) {
     set<pair<int, int>> lineIntersects;
     lineIntersects.insert(NULLPOINT);
     for (int j = 0; j < n; ++j) {
       pair<int, int> p = lines[i].intersect(lines[j]);
       lineIntersects.insert(p);
       intersects.insert(p);
     }
     ans -= lineIntersects.size() - 1;
   }
   ans += intersects.size() - 1;
   printf( %lld
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   long long n, k;
   scanf( %lld%lld , &n, &k);
   if (k < 2) {
     printf( %lld , n);
     return 0;
   }
   long long s = 1;
   while (s - 1 < n) s *= 2;
   printf( %lld , s - 1);
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   cin.sync_with_stdio(0);
   cin.tie(0);
   cin.exceptions(cin.failbit);
   int n;
   cin >> n;
   vector<long long> a(n);
   long long s = 0;
   for (int i = 0; i < n; i++) {
     cin >> a[i];
     s += a[i];
   }
   sort(a.begin(), a.end());
   reverse(a.begin(), a.end());
   if (s % 2 == 1) {
     cout <<  NO  << endl;
     return 0;
   }
   if (a[0] <= s / 2) {
     cout <<  YES  << endl;
   } else {
     cout <<  NO  << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 160;
 const int mod = 1e9 + 7;
 int n, m, f[N][N][2][2], ans;
 void inc(int& a, int b) {
   if (a += b, a >= mod) a -= mod;
 }
 int main() {
   scanf( %d %d , &n, &m);
   for (int i = 1; i <= m; ++i)
     for (int j = i; j <= m; ++j) {
       f[i][j][1][1] = 1;
     }
   for (int h = 1; h <= n; ++h) {
     int tot = 0;
     for (int i = 1; i <= m; ++i)
       for (int j = 1; j <= m; ++j)
         for (int kl = 0; kl <= 1; ++kl)
           for (int kr = 0; kr <= 1; ++kr) {
             inc(tot, f[i][j][kl][kr]);
           }
     inc(ans, (long long)tot * (n + 1 - h) % mod);
     if (h == n) break;
     for (int j = 1; j <= m; ++j)
       for (int i = 1; i < j; ++i)
         for (int kr = 0; kr <= 1; ++kr) {
           inc(f[i + 1][j][0][kr], (f[i][j][0][kr] + f[i][j][1][kr]) % mod);
         }
     for (int i = 1; i <= m; ++i)
       for (int j = m; j > i; --j)
         for (int kl = 0; kl <= 1; ++kl) {
           inc(f[i][j - 1][kl][0], (f[i][j][kl][0] + f[i][j][kl][1]) % mod);
         }
     for (int j = 2; j <= m; ++j)
       for (int i = j; i > 1; --i)
         for (int kr = 0; kr <= 1; ++kr) {
           inc(f[i - 1][j][1][kr], f[i][j][1][kr]);
         }
     for (int i = 1; i <= m - 1; ++i)
       for (int j = i; j < m; ++j)
         for (int kl = 0; kl <= 1; ++kl) {
           inc(f[i][j + 1][kl][1], f[i][j][kl][1]);
         }
   }
   printf( %d
 , ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAX_N = 1e5 + 5;
 char a[MAX_N];
 int main() {
   int N;
   cin >> N;
   scanf( %s , a + 1);
   int cnt = 0;
   for (int i = 1; i <= N; i++) {
     if (a[i] == 'S' && a[i] >= 2 && a[i - 1] == 'G') {
       cnt++;
     }
   }
   if (a[N] == 'G') cnt++;
   int len = 0, prelen = (cnt >= 2), ans = 0;
   for (int i = 1; i <= N; i++) {
     if (a[i] == 'G') {
       len++;
       ans = max(ans, len + prelen);
     }
     if (a[i] == 'S') {
       if (2 <= i && i <= N - 1 && a[i - 1] == 'G' && a[i + 1] == 'G') {
         if (cnt == 2) {
           prelen = len;
         } else if (cnt >= 3) {
           prelen = len + 1;
         }
       } else {
         prelen = (cnt >= 2);
       }
       len = 0;
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long double EPS = 1e-10;
 const long double INF = (int)1e9 + 34;
 const int N = (int)1e6 + 34;
 void ret(int x) {
   cout << x;
   exit(0);
 }
 int n, k;
 int a[N];
 int b[N], len;
 bool u[N];
 int main() {
   scanf( %d%d , &n, &k);
   for (int i = 0; i < n; i++) scanf( %d , a + i);
   srand(3431);
   sort(a, a + n);
   a[n] = 100000000;
   int lst = a[0];
   b[len++] = a[0];
   for (int i = 1; i < n; i++)
     if (a[i + 1] - lst >= k) {
       b[len++] = lst = a[i];
     }
   int mn = a[0];
   for (int i = 0; i < n; i++) mn = min(mn, a[i]);
   vector<int> v;
   for (int i = 0; i < 30; i++) v.push_back(a[abs((rand() << 16) ^ rand()) % n]);
   for (int i = 1; i <= 1000000; i++)
     for (int j = 0; !u[i] && j < (int)v.size(); j++)
       if (v[j] % i > k) u[i] = true;
   int l1 = -1;
   int pr;
   bool fail;
   for (int d = mn; d >= 0; d--) {
     if (u[d]) continue;
     fail = false;
     for (int i = 0; !fail && i < len; i++)
       if (b[i] % d > k) fail = true;
     if (!fail) {
       pr = d;
       break;
     }
     l1 = d;
   }
   for (int d = pr; d >= 0; d--) {
     if (u[d]) continue;
     fail = false;
     for (int i = n - 1; !fail && i >= 0; i--)
       if (a[i] % d > k) fail = true;
     if (!fail) {
       pr = d;
       break;
     }
     l1 = d;
   }
   cout << pr;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(0);
   long long n, cnt1 = 0, cnt2 = 0, l, r;
   cin >> n;
   for (int i = 0; i < n; i++) {
     cin >> l >> r;
     cnt1 += l;
     cnt2 += r;
   }
   cout << min(cnt1, n - cnt1) + min(cnt2, n - cnt2);
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int Mmax(long long int a, long long int b) {
   if (a > b) return a;
   return b;
 }
 long long int Mmin(long long int a, long long int b) {
   if (a < b) return a;
   return b;
 }
 long long int nod(long long int a, long long int b) {
   while (a && b) {
     if (a > b)
       a %= b;
     else
       b %= a;
   }
   return Mmax(a, b);
 }
 long long int nok(long long int a, long long int b) {
   return a * b / nod(a, b);
 }
 bool IsPrime(long long int x) {
   if (x < 2) return false;
   long long int X = sqrt(x), i;
   for (i = 2; i <= X; i++)
     if (x % i == 0) return false;
   return true;
 }
 void hanoi(int n, int A, int C, int B) {
   if (n == 1) {
     cout << n <<     << A <<     << C << endl;
   } else {
     hanoi(n - 1, A, B, C);
     cout << n <<     << A <<     << C << endl;
     hanoi(n - 1, B, C, A);
   }
 }
 string pr2(string a, int d) {
   if (d == 0) return  0 ;
   string b;
   long long int sz = a.size(), i, prenos = 0;
   for (i = 0; i < sz; i++) {
     b[i] = a[i];
   }
   for (i = sz - 1; i > -1; i--) {
     a[i] = ((b[i] - '0') * d + prenos) % 10 + '0';
     prenos = ((b[i] - '0') * d + prenos) / 10;
   }
   if (prenos) a = char(prenos + '0') + a;
   return a;
 }
 string sum(string a, string b) {
   bool carry = false;
   long long int i, sz1, sz2, maxsz, minsz;
   string c, d;
   sz1 = a.size();
   sz2 = b.size();
   maxsz = max(sz1, sz2);
   minsz = min(sz1, sz2);
   while (sz1 < maxsz) {
     sz1++;
     a = '0' + a;
   }
   while (sz2 < maxsz) {
     sz2++;
     b = '0' + b;
   }
   for (i = maxsz - 1; i > -1; i--) {
     d = char((a[i] + b[i] - 96 + carry) % 10 + 48) + d;
     if (a[i] + b[i] - 96 + carry > 9)
       carry = true;
     else
       carry = false;
   }
   if (carry == true) d = char('1') + d;
   return d;
 }
 string pr(string a, string b) {
   string res =  0 , p, p2;
   int sz = a.size(), x = 0;
   for (sz = a.size(); sz > 0; sz--, x++) {
     int d = a[sz - 1] - '0';
     a = a.erase(sz - 1, 1);
     p2 = pr2(b, d);
     p2 += p;
     res = sum(res, p2);
     p +=  0 ;
   }
   return res;
 }
 bool vesokosna(long long int x) {
   return (x % 4 == 0 && x % 100 || x % 400 == 0);
 }
 long long int reverse(long long int x) {
   long long int mirror = 0;
   while (x) {
     mirror = mirror * 10 + x % 10;
     x /= 10;
   }
   return mirror;
 }
 long long int ost(string x, long long int k) {
   long long int num = 0, i, sz = x.size();
   for (i = 0; i < sz; i++) {
     num = num * 10;
     num += x[i] - '0';
     num %= k;
   }
   return num;
 }
 bool can[128][128];
 vector<string> ans;
 int main() {
   char last;
   int k, i, j, sz2 = 1;
   cin >> k;
   string q;
   cin >> q;
   int sz = q.size();
   if (sz < k) {
     cout <<  NO
 ;
     return 0;
   }
   string help;
   help += q[0];
   ans.push_back(help);
   last = q[0];
   for (i = 1; i < sz; i++) {
     if (sz2 == k) break;
     bool can = true;
     ;
     for (j = 0; j < sz2; j++)
       if (q[i] == ans[j][0]) can = false;
     if (can) {
       string help;
       help += q[i];
       last = q[i];
       ans.push_back(help);
       sz2++;
     } else
       ans.back() += q[i];
   }
   if (sz2 == k) {
     cout <<  YES
 ;
     for (; i < sz; i++) {
       ans.back() += q[i];
     }
     for (i = 0; i < sz2; i++) cout << ans[i] << endl;
     return 0;
   }
   cout <<  NO
 ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <ostream& out = cout, class... T>
 void prints(T&&...) {}
 template <ostream& out = cout, class... T>
 void printe(T&&...) {}
 template <ostream& out = cout, class... T>
 void printw(T&&...) {}
 template <ostream& out = cout, class... T>
 void printew(T&&...) {}
 template <ostream& out = cout, class... T>
 void printb(T&&...) {}
 template <ostream& out = cout, class... T>
 void printd(T&&...) {}
 template <ostream& out = cout, class... T>
 void printde(T&&...){};
 template <class T>
 istream& operator>>(istream& in, vector<T>& v) {
   for (auto& e : v) {
     in >> e;
   }
   return in;
 }
 int main() {
   int n;
   cin >> n;
   vector<char> s(n);
   cin >> s;
   int k = 0;
   bool pr;
   for (int i = 0; i < n; i++) {
     if (s[i] == '1') {
       k++;
       pr = true;
     } else {
       break;
     }
   }
   if (k == n) {
     cout <<  YES ;
     return 0;
   }
   int l = 0;
   for (int i = 0; i < n; i++) {
     if (s[i] == '0') {
       l++;
       pr = false;
     } else {
       break;
     }
   }
   if (l == n) {
     cout <<  YES ;
     return 0;
   }
   int m;
   m = max(l, k);
   for (int i = 0; i < n; i = i + m) {
     if (s[i] == '1' && pr) {
       for (int j = 0; j < m; j++) {
         if (s[j + i] != '1') {
           cout <<  NO ;
           return 0;
         }
       }
       pr = false;
       continue;
     }
     if (s[i] == '0' && !pr) {
       for (int j = 0; j < m; j++) {
         if (s[j + i] != '0') {
           cout <<  NO ;
           return 0;
         }
       }
       pr = true;
       continue;
     }
     cout <<  NO ;
     return 0;
   }
   cout <<  YES ;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int nax = 505;
 int n;
 char grid[nax][nax];
 int cc[nax][nax];
 int cc_size[nax * nax];
 int when_added[nax * nax];
 const int dx[4] = {-1, 1, 0, 0};
 const int dy[4] = {0, 0, -1, 1};
 const char EMPTY = '.';
 bool inside(int x, int y) { return 0 <= min(x, y) && max(x, y) < n; }
 void dfs(int x, int y, int which_cc) {
   cc[x][y] = which_cc;
   ++cc_size[which_cc];
   for (int i = 0; i < 4; ++i) {
     int x2 = x + dx[i];
     int y2 = y + dy[i];
     if (inside(x2, y2) && grid[x2][y2] == EMPTY && cc[x2][y2] == 0)
       dfs(x2, y2, which_cc);
   }
 }
 void add(int x, int y, int& answer, int current_time) {
   if (inside(x, y) && grid[x][y] == EMPTY) {
     int id = cc[x][y];
     if (when_added[id] != current_time) {
       when_added[id] = current_time;
       answer += cc_size[id];
     }
   }
 }
 int main() {
   int k;
   scanf( %d%d , &n, &k);
   for (int i = 0; i < n; ++i) scanf( %s , grid[i]);
   int how_many_cc = 0;
   for (int x = 0; x < n; ++x)
     for (int y = 0; y < n; ++y)
       if (grid[x][y] == EMPTY && cc[x][y] == 0) dfs(x, y, ++how_many_cc);
   int cur_time = 1;
   int best_answer = 0;
   for (int y_low = 0; y_low + k <= n; ++y_low) {
     for (int x = 0; x < k; ++x)
       for (int y = y_low; y < y_low + k; ++y) --cc_size[cc[x][y]];
     for (int x_low = 0; x_low + k <= n; ++x_low) {
       int answer = k * k;
       for (int x = x_low; x < x_low + k; ++x) {
         add(x, y_low - 1, answer, cur_time);
         add(x, y_low + k, answer, cur_time);
       }
       for (int y = y_low; y < y_low + k; ++y) {
         add(x_low - 1, y, answer, cur_time);
         add(x_low + k, y, answer, cur_time);
       }
       ++cur_time;
       best_answer = max(best_answer, answer);
       if (x_low + k != n) {
         for (int y = y_low; y < y_low + k; ++y) {
           ++cc_size[cc[x_low][y]];
           --cc_size[cc[x_low + k][y]];
         }
       }
     }
     for (int x = n - k; x < n; ++x)
       for (int y = y_low; y < y_low + k; ++y) ++cc_size[cc[x][y]];
   }
   printf( %d , best_answer);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = 1000000000;
 const int maxn = 100010;
 int a[maxn];
 int max(int a, int b) { return a > b ? a : b; }
 int min(int a, int b) { return a > b ? b : a; }
 int main() {
   int n, l, r, i, j;
   string s;
   while (cin >> n) {
     for (i = 0; i < n; i++) cin >> a[i];
     cin >> s;
     l = -inf, r = inf;
     for (i = 4; i < n; i++) {
       if (s[i] == '1') {
         int maxi = -inf;
         for (j = i; j >= i - 4; j--) {
           maxi = max(maxi, a[j]);
         }
         int cnt = 0;
         for (j = i; j >= i - 4; j--) {
           if (s[j] == '0') cnt++;
         }
         if (cnt == 4) {
           l = max(l, maxi + 1);
         }
       } else if (s[i] == '0') {
         int mina = inf;
         for (j = i; j >= i - 4; j--) {
           mina = min(mina, a[j]);
         }
         int cnt = 0;
         for (j = i; j >= i - 4; j--) {
           if (s[j] == '1') cnt++;
         }
         if (cnt == 4) {
           r = min(r, mina - 1);
         }
       }
     }
     cout << l <<     << r << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long i, j, k, n, v[200010], m, a, b, x, y, h, w, l, p, ans, numitor, aux,
     mini, sum, poz;
 string s1, s2, second;
 char c;
 map<long long, long long> mip;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cin >> n >> x;
   for (long long i = 1; i <= n; i++) {
     cin >> v[i];
     if (v[i] < x)
       v[i] = -1;
     else if (v[i] > x)
       v[i] = 1;
     else {
       v[i] = 0;
       poz = i;
     }
   }
   mip[0] = 1;
   sum = 0;
   for (long long i = 1; i <= n; i++) {
     sum += v[i];
     if (i >= poz) {
       if (mip.find(sum) != mip.end()) ans += mip[sum];
       if (mip.find(sum - 1) != mip.end()) ans += mip[sum - 1];
     } else {
       mip[sum] += 1;
       v[i] = sum;
     }
   }
   cout << ans;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e5 + 5;
 int n, ex, ey, sx[N], sy[N];
 char s[N];
 inline bool check(int m) {
   for (int i = 1; i + m - 1 <= n; i++) {
     int decx = sx[n] - sx[i + m - 1] + sx[i - 1];
     int decy = sy[n] - sy[i + m - 1] + sy[i - 1];
     int nedx = ex - decx;
     int nedy = ey - decy;
     if (abs(nedx) + abs(nedy) <= m && !(m - abs(nedx) - abs(nedy) & 1))
       return 1;
   }
   return 0;
 }
 int main() {
   scanf( %d%s%d%d , &n, s + 1, &ex, &ey);
   for (int i = 1; i <= n; i++) {
     sx[i] = sx[i - 1] + (s[i] == 'L' ? -1 : (s[i] == 'R' ? 1 : 0));
     sy[i] = sy[i - 1] + (s[i] == 'D' ? -1 : (s[i] == 'U' ? 1 : 0));
   }
   int l = 0, r = n, mid, ans = -1;
   while (l <= r) {
     mid = l + r >> 1;
     if (check(mid)) {
       ans = mid;
       r = mid - 1;
     } else {
       l = mid + 1;
     }
   }
   printf( %d
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int a, b, c, d, i, j, x, y = 0, w, z = 0, e;
   cin >> a >> b >> c >> d;
   if (d % c != 0) {
     cout << 0;
     return 0;
   }
   w = d / c;
   if (c == d) {
     if (c >= a && c <= b)
       cout << 1;
     else
       cout << 0;
     return 0;
   }
   for (i = 1; i <= sqrt(w); i++) {
     if (w % i == 0) {
       x = w / i;
       e = 0;
       for (j = 2; j <= i; j++) {
         if (i % j == 0 && x % j == 0) {
           e = 1;
           break;
         }
       }
       if (e == 0) {
         if (i * c >= a && i * c <= b && x * c >= a && x * c <= b) y++;
       }
     }
   }
   cout << y * 2;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 1e6 + 5;
 int tree[4 * MAXN], n, m, a[MAXN], size = 0, less1[MAXN];
 vector<int> del, nums;
 void initialize(int l, int r, int node) {
   tree[node] = r - l + 1;
   if (r == l) return;
   int mid = (l + r) / 2;
   initialize(l, mid, 2 * node);
   initialize(mid + 1, r, 2 * node + 1);
 }
 int elim(int node) {
   while (node >= 1) {
     tree[node]--;
     node /= 2;
   }
 }
 void find(int l, int r, int sum, int node) {
   if (l == r) {
     nums[l] = -2;
     elim(node);
     return;
   }
   int mid = (l + r) / 2;
   if (tree[2 * node] >= sum)
     find(l, mid, sum, 2 * node);
   else
     find(mid + 1, r, sum - tree[2 * node], 2 * node + 1);
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin >> n >> m;
   for (int i = 0; i < m; ++i) {
     cin >> a[i];
   }
   for (int i = 0; i < MAXN; ++i) less1[i] = 0;
   for (int i = 0; i < m - 1; ++i)
     for (int k = a[i]; k < a[i + 1]; ++k) less1[k] = i + 1;
   for (int i = a[m - 1]; i < MAXN; ++i) less1[i] = m;
   for (int i = 0; i < n; ++i) {
     int x;
     cin >> x;
     if (x == -1) {
       del.push_back(less1[size]);
       size -= less1[size];
     } else {
       nums.push_back(x);
       size++;
     }
   }
   if (nums.size() == 0) {
     cout <<  Poor stack!  << endl;
     return 0;
   }
   initialize(0, nums.size() - 1, 1);
   for (int i = 0; i < del.size(); ++i)
     for (int j = del[i] - 1; j >= 0; --j) find(0, nums.size() - 1, a[j], 1);
   int ct = 0;
   for (int i = 0; i < nums.size(); ++i) {
     if (nums[i] != -2) {
       cout << nums[i];
       ++ct;
     }
   }
   if (ct == 0) cout <<  Poor stack! ;
   cout << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = 0x3f3f3f3f;
 const long long INFF = 0x3f3f3f3f3f3f3f3f;
 const double pi = acos(-1.0);
 const double eps = 1e-9;
 const long long mod = 1e9 + 7;
 int read() {
   int x = 0, f = 1;
   char ch = getchar();
   while (ch<'0' | ch> '9') {
     if (ch == '-') f = -1;
     ch = getchar();
   }
   while (ch >= '0' && ch <= '9') {
     x = x * 10 + ch - '0';
     ch = getchar();
   }
   return x * f;
 }
 void Out(int aa) {
   if (aa > 9) Out(aa / 10);
   putchar(aa % 10 + '0');
 }
 int n, t;
 int f[55], s[55], p[55];
 double dp[55][5005];
 bool check(double mid) {
   for (int i = n; i >= 1; i--) {
     for (int j = t + 1; j <= 5000; j++) dp[i + 1][j] = mid;
     for (int j = 0; j <= t; j++) {
       double t1 = (dp[i + 1][j + f[i]] + f[i]) * p[i] / 100;
       double t2 = (dp[i + 1][j + s[i]] + s[i]) * (100 - p[i]) / 100;
       dp[i][j] = min(mid, t1 + t2);
     }
   }
   return dp[1][0] < mid;
 }
 int main() {
   n = read(), t = read();
   for (int i = 1; i <= n; i++) f[i] = read(), s[i] = read(), p[i] = read();
   double l = 0, r = 1e12;
   for (int i = 0; i < 100; i++) {
     double mid = (l + r) / 2;
     if (check(mid))
       r = mid;
     else
       l = mid;
   }
   printf( %.10f
 , l);
   return 0;
 }


#include <iostream>
 #include <vector>
 #include <algorithm>

 using namespace std;

 #define int long long

 void solve() {
     int n;
     vector<long long> vect;
     cin >> n;
     for (int i = 0; i < n; i++) {
         long long x;
         cin >> x;
         vect.push_back(x);
     }
     sort(vect.begin(), vect.end());
     long long res = 0;
     long long now = 0;
     for (int i = n - 2; i >= 0; i--) {
         res -= now - (n - i - 2) * vect[i];
         now += vect[i + 1];
     }
     cout << res <<
 ;
 }

 signed main()
 {
     ios_base::sync_with_stdio(false);
     cin.tie(nullptr);
     int t;
     cin >> t;
     for (int i = 0; i < t; i++)
         solve();
     return 0;
 }


 // Problem : B. Tree Array
 // Contest : Codeforces - Codeforces Round #728 (Div. 1)
 // URL : https://codeforces.com/contest/1540/problem/B
 // Memory Limit : 256 MB
 // Time Limit : 2000 ms
 // Powered by CP Editor (https://github.com/cpeditor/cpeditor)

 #include <bits/stdc++.h>

 using namespace std;

 int N;
 long long pas[205][205];
 const long long MOD = 1e9+7;
 long long cache[205][205];
 vector<int> graph[205];
 int par[205];
 bool vis[205];

 long long add(long long a, long long b){
 	return (a + b) % MOD;
 }

 long long mult(long long a, long long b){
 	return a * b % MOD;
 }

 long long qikpow(long long b, long long e){
 	if(!e){
 		return 1;
 	}
 	long long ret = qikpow(b, e / 2);
 	ret = mult(ret, ret);
 	if(e & 1){
 		ret = mult(ret, b);
 	}
 	return ret;
 }

 long long sub(long long a, long long b){
 	return ((a - b) % MOD + MOD ) % MOD;
 }

 long long divd(long long a, long long b){
 	return mult(a, qikpow(b, MOD - 2));
 }

 void dfs1(int n){
 	for(int e : graph[n]){
 		if(e != par[n]){
 			par[e] = n;
 			dfs1(e);
 		}
 	}
 }

 int dfs2(int n, int p){
 	int sz = 1;
 	for(int e : graph[n]){
 		if(!vis[e] && p != e){
 			sz += dfs2(e, n);
 		}
 	}
 	return sz;
 }

 int main(){
 	ios::sync_with_stdio(0);
 	cin.tie(0);
 	cout.tie(0);
 	cin >> N;
 	for(int i = 0; i <= N; i++){
 		pas[i][0] = 1;
 		for(int j = 1; j <= i; j++){
 			pas[i][j] = add(pas[i-1][j], pas[i-1][j-1]);
 		}
 	}
 	for(int i = 1; i < N; i++){
 		int a, b;
 		cin >> a >> b;
 		graph[a].emplace_back(b);
 		graph[b].emplace_back(a);
 	}
 	long long ans = 0;
 	for(int n = 1; n <= N; n++){
 		fill(par, par+1+N, 0);
 		dfs1(n);
 		vis[n] = 1;
 		for(int k = n+1; k <= N; k++){
 			int cur = k, len = 0;
 			while(cur != n){
 				vis[cur] = 1;
 				len++;
 				cur = par[cur];
 			}
 			cur = k;
 			for(int t = len; t; t--){
 				int s = dfs2(cur, 0);
 				//cout << s <<
 ;
 				long long tot = divd(s, N);
 				if(t == len){
 					ans = add(ans, tot);
 				}
 				else{
 					/*
 					long long p = 1;//qikpow(divd(1, 2), len);
 					cout << p <<     << pas[len - 1][len - t] <<
 ;
 					p = mult(p, divd(pas[len - 1][len - t], pas[len][len-t]));
 					cout << n <<     << k <<     << cur <<
 ;
 					cout << p <<     << tot <<
 ;
 					ans = add(ans, mult(p, tot));
 					*/
 					//cout << n <<     << k <<     << cur <<
 ;
 					//cout << tot <<
 ;
 					if(!cache[len][t]){
 						for(int c = 0; c < t; c++){
 							cache[len][t] = add(cache[len][t], mult(pas[c+len-t-1][len-t-1], qikpow(divd(1, 2), c+len-t)));
 							//cout << mult(mult(pas[c+len-t-1][len-t-1], qikpow(divd(1, 2), c+len-t)), tot) <<
 ;
 						}
 					}
 					ans = add(mult(tot, cache[len][t]), ans);

 				}
 				cur = par[cur];
 			}
 			cur = k;
 			while(cur != n){
 				vis[cur] = 0;
 				cur = par[cur];
 			}
 		}
 		vis[n] = 0;
 	}
 	cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 int read() {
   int a = 0;
   char c = getchar();
   bool f = 0;
   while (!isdigit(c)) {
     f = c == '-';
     c = getchar();
   }
   while (isdigit(c)) {
     a = a * 10 + c - 48;
     c = getchar();
   }
   return f ? -a : a;
 }
 const int _ = 1e5 + 11;
 vector<int> nxt[_][2];
 int N, C[_], ans;
 long long num;
 void addEd(int a, int b, int c) { nxt[a][c].push_back(b); }
 int deg[_], dst[_], seq[_], cseq;
 unsigned long long arr[_][50];
 void build(int fir) {
   memset(deg, 0, sizeof(deg));
   cseq = 0;
   for (int i = 1; i <= N + 8; ++i) {
     for (auto t : nxt[i][0]) deg[t] += dst[t] == dst[i];
     for (auto t : nxt[i][1]) deg[t] += dst[t] == dst[i] + 1;
   }
   queue<int> q;
   q.push(fir);
   while (!q.empty()) {
     int t = q.front();
     q.pop();
     seq[++cseq] = t;
     for (int l = 0; l < 2; ++l)
       for (auto p : nxt[t][l])
         if (dst[p] == dst[t] + l && !--deg[p]) q.push(p);
   }
 }
 void solve(int size, vector<int> node, int fir, int mx) {
   memset(arr, 0, sizeof(arr));
   for (int i = 0; i < node.size(); ++i)
     arr[node[i]][i >> 6] |= 1ull << (i & 63);
   for (int i = 1; i <= cseq; ++i) {
     int t = seq[i];
     for (int l = 0; l < 2; ++l)
       for (auto p : nxt[t][l])
         if (dst[p] == dst[t] + l)
           for (int r = 0; r < 50; ++r) arr[p][r] |= arr[t][r];
   }
   long long cnt = 0;
   int val = node.size();
   for (int i = 1; i <= N && val == node.size(); ++i)
     if (dst[i] == mx) {
       int num = 0;
       for (int j = 0; j < 50; ++j) {
         num += __builtin_popcountll(arr[i][j]);
       }
       val = min(val, num);
     }
   if (val == node.size()) --mx;
   for (int i = 1; i <= N; ++i)
     if (dst[i] > mx)
       cnt += node.size();
     else if (dst[i] == mx) {
       int num = 0;
       for (int j = 0; j < 50; ++j) {
         num += __builtin_popcountll(arr[i][j]);
       }
       cnt += node.size() - num;
     }
   if (ans < mx) {
     ans = mx;
     num = 0;
   }
   if (ans == mx) num += cnt;
 }
 int main() {
   ios::sync_with_stdio(0);
   cin >> N;
   for (int i = 1; i <= N; ++i) {
     char c;
     cin >> c;
     C[i] = c - 'a' + 1;
     addEd(i, N + C[i], 0);
     addEd(N + C[i], i, 1);
   }
   for (int i = 2; i <= N; ++i) {
     addEd(i - 1, i, 1);
     addEd(i, i - 1, 1);
   }
   for (int i = 1; i <= 8; ++i) {
     memset(dst, 0x3f, sizeof(dst));
     dst[N + i] = 0;
     deque<int> q;
     q.push_back(N + i);
     int mx = 0;
     while (!q.empty()) {
       int t = q.front();
       q.pop_front();
       if (t <= N) mx = max(mx, dst[t]);
       for (auto r : nxt[t][0])
         if (dst[r] > dst[t]) {
           dst[r] = dst[t];
           q.push_front(r);
         }
       for (auto r : nxt[t][1])
         if (dst[r] > dst[t] + 1) {
           dst[r] = dst[t] + 1;
           q.push_back(r);
         }
     }
     vector<int> node;
     build(N + i);
     for (int j = 1; j <= N; ++j)
       if (C[j] == i) {
         node.push_back(j);
         if (node.size() == 64 * 50) {
           solve(64 * 50, node, N + i, mx);
           node.clear();
         }
       }
     if (node.size()) solve(node.size(), node, N + i, mx);
   }
   cout << ans << ' ' << num / 2;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int64_t ans, n, k, m, r[100005];
 char s[100005], t[100005];
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   int64_t tt;
   cin >> tt;
   while (tt--) {
     cin >> n;
     cin >> s;
     cin >> t;
     vector<int> ans;
     m = n - 1;
     int i = 0, j = n - 1;
     while (i < j) {
       r[m--] = i;
       i++;
       r[m--] = j;
       j--;
     }
     if (i == j) r[m] = i;
     m = 0;
     for (int i = n - 1; i >= 0; i--) {
       if ((s[r[i]] - '0' + m) % 2 == t[i] - '0') ans.push_back(1);
       ans.push_back(i + 1);
       m++;
     }
     cout << (int)ans.size() <<    ;
     for (auto i : ans) cout << i <<    ;
     cout <<
 ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> a;
 int k, t, n, mx;
 string s;
 int main() {
   cin >> n >> k;
   cin >> s;
   for (int i = 0; i < s.size(); i++)
     if (s[i] == '.') a.push_back(i);
   for (int i = 0; i < a.size() - 1; i++) mx = max(mx, a[i + 1] - a[i]);
   if (mx > k)
     cout <<  NO ;
   else
     cout <<  YES ;
 }


#include <bits/stdc++.h>
 using namespace std;
 int w;
 long long n, k;
 pair<long long, long long> o = {0, 0};
 pair<long long, long long> csop(long long kezd, long long kim) {
   long long sum = 0, kov = kezd + k;
   if (kezd <= kim && kim <= kov) {
     kov++;
   }
   sum = (kov - kezd) * (kov - kezd - 1) / 2;
   if (log(k) + log(kezd) < 42.0) {
     sum += kezd * (kov - kezd);
   } else {
     sum = 2e18;
   }
   if (kezd <= kim && kim < kov) {
     sum -= kim;
   }
   return {sum, kov};
 }
 pair<long long, long long> calc(long long ert) {
   if (ert <= k * (k + 1) / 2) {
     return {1, k * (k + 1) / 2};
   }
   long long kis = ert / k - k / 2;
   auto s = calc(kis);
   long long fi = s.first, se = s.second;
   long long s2 = kis, kor = 0, ert2 = 0;
   s2 -= (fi - 1) * (k + 1), kor += fi - 1, ert2 = (fi - 1) * (k + 1);
   long long ss = (s2 + k - 2) / k;
   kor += ss, ert2 += ss * k;
   ert2++, kor++;
   if (ert2 >= se) {
     ert2++;
   }
   auto cs = csop(ert2, se);
   if (cs.first < ert) {
     ert2 = cs.second, kor++;
     cs = csop(ert2, se);
   }
   return {kor, cs.first};
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin >> w;
   while (w--) {
     cin >> n >> k;
     if (n <= k) {
       cout << n <<
 ;
       continue;
     }
     if (k == 2 && n <= 5) {
       cout << n <<
 ;
       continue;
     }
     auto s = calc(n);
     if (s.second == n) {
       cout << s.first * (k + 1) <<
 ;
     } else {
       long long fi = s.first, ans = fi * (k + 1);
       n -= fi * (k + 1) - 1;
       ans += (k + 1) * (n / k) + n % k;
       if (n % k == 0) {
         ans--;
       }
       cout << ans <<
 ;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int cl[5000010], cr[5000010], ld[5000010], sl = 1;
 bool yz[5000010];
 void pushd(int x) {
   if (cl[x] == 0) {
     cl[x] = ++sl;
     yz[cl[x]] = 1;
   }
   if (cr[x] == 0) {
     cr[x] = ++sl;
     yz[cr[x]] = 1;
   }
   yz[x] = 0;
   if (ld[x] == 0) return;
   ld[cl[x]] = ld[cr[x]] = ld[x];
   ld[x] = 0;
 }
 vector<int> cf[50010], ve[5000010];
 void addqj(int i, long long l, long long r, long long L, long long R, int x) {
   if (R <= l || r <= L) return;
   if (L <= l && r <= R) {
     ld[i] = x;
     cf[x].push_back(i);
     return;
   }
   long long m = (l + r) >> 1;
   pushd(i);
   addqj(cl[i], l, m, L, R, x);
   addqj(cr[i], m, r, L, R, x);
 }
 void link(int x, int y) {
   if (yz[x] && yz[y]) {
     if (ld[x] < ld[y])
       ve[x].push_back(y);
     else
       ve[y].push_back(x);
     return;
   }
   link(yz[x] ? x : cl[x], yz[y] ? y : cl[y]);
   link(yz[x] ? x : cr[x], yz[y] ? y : cr[y]);
 }
 int find(int i, long long l, long long r, long long x) {
   if (yz[i]) return i;
   long long m = (l + r) >> 1;
   return x < m ? find(cl[i], l, m, x) : find(cr[i], m, r, x);
 }
 int lx[50010], fa[5000010], ans[50010];
 long long A[50010], B[50010];
 int getv(int x) { return x == fa[x] ? x : fa[x] = getv(fa[x]); }
 void merge(int x, int y) {
   x = getv(x);
   y = getv(y);
   if (x != y) fa[x] = y;
 }
 int main() {
   int n, m;
   scanf( %d%d , &n, &m);
   ld[1] = m + 1;
   yz[1] = 1;
   for (int i = 1; i <= m; i++) {
     char zf[10];
     scanf( %s%lld%lld , zf, &A[i], &B[i]);
     lx[i] = zf[0] == 'b' ? 0 : 1;
     if (lx[i] == 0) addqj(1, 0, (1ll << n), A[i], B[i] + 1, i);
   }
   for (int i = 1; i <= sl; i++) {
     fa[i] = i;
     if (!yz[i]) link(cl[i], cr[i]);
   }
   for (int i = 1; i <= sl; i++) {
     if (ld[i] != m + 1) continue;
     for (int j = 0; j < ve[i].size(); j++) merge(i, ve[i][j]);
   }
   for (int i = m; i >= 1; i--) {
     if (lx[i])
       ans[i] = (getv(find(1, 0, (1ll << n), A[i])) ==
                 getv(find(1, 0, (1ll << n), B[i])));
     else {
       for (int x = 0; x < cf[i].size(); x++) {
         int u = cf[i][x];
         for (int y = 0; y < ve[u].size(); y++) merge(u, ve[u][y]);
       }
     }
   }
   for (int i = 1; i <= m; i++) {
     if (lx[i]) printf( %d
 , ans[i]);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 15;
 long long Get_Num(int num[], int b, int n) {
   long long number = 0;
   long long in = 1;
   for (int i = n - 1; i >= 0; i--) {
     number += num[i] * in;
     in = in * b;
   }
   return number;
 }
 int main() {
   int x[N], y[N];
   int b;
   int n;
   long long X, Y;
   cin >> n >> b;
   for (int i = 0; i < n; i++) {
     cin >> x[i];
   }
   X = Get_Num(x, b, n);
   cin >> n >> b;
   for (int i = 0; i < n; i++) {
     cin >> y[i];
   }
   Y = Get_Num(y, b, n);
   if (X == Y) {
     printf( =
 );
   } else if (X > Y) {
     printf( >
 );
   } else {
     printf( <
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     long n, m;
     cin >> n >> m;
     long sum = 0;
     if (n == 1 || n == 2) {
       if (n == 1)
         cout << 0 << endl;
       else
         cout << m << endl;
     } else {
       cout << 2 * m << endl;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int mod = 1e9 + 7;
 long long n, m;
 int k;
 int mpow(int a, long long b) {
   int c = 1;
   for (; b; b >>= 1, a = 1ll * a * a % mod)
     if (b & 1) c = 1ll * c * a % mod;
   return c;
 }
 int main() {
   scanf( %lld%lld%d , &n, &m, &k);
   if ((n + m) & 1)
     if (k == -1) {
       printf( 0
 );
       return 0;
     }
   int tmp = mpow(2, n - 1);
   printf( %d
 , mpow(tmp, m - 1));
 }


#include <bits/stdc++.h>
 using namespace std;
 int tt(char p) {
   int s;
   if (p == '>')
     s = 8;
   else if (p == '<')
     s = 9;
   else if (p == '+')
     s = 10;
   else if (p == '-')
     s = 11;
   else if (p == '.')
     s = 12;
   else if (p == ',')
     s = 13;
   else if (p == '[')
     s = 14;
   else
     s = 15;
   return s;
 }
 int main() {
   string x;
   int t = 0;
   int q = x.length() - 1;
   cin >> x;
   for (int i = 0; i < x.length(); ++i) {
     t = (t * 16 + tt(x[i])) % 1000003;
   }
   cout << t;
   return 0;
 }


#include <bits/stdc++.h>
 template <typename T>
 inline T MIN(T a, T b) {
   return a < b ? a : b;
 }
 template <typename T>
 inline T MAX(T a, T b) {
   return a > b ? a : b;
 }
 using namespace std;
 void reop() { freopen( input.txt ,  r , stdin); }
 int a[100001] = {0};
 int ans = 0, curans = 0;
 bool test(int div, int n, int start) {
   int prev = start;
   for (int i = 2; i < n; ++i) {
     if (a[i] == prev + div)
       prev = a[i];
     else if (a[i] + 1 == prev + div) {
       prev = a[i] + 1;
       curans++;
     } else if (a[i] - 1 == prev + div) {
       prev = a[i] - 1;
       curans++;
     } else
       return false;
   }
   ans = MIN(ans, curans);
   return true;
 }
 int main() {
   int n;
   cin >> n;
   for (int i = 0; i < n; i++) {
     cin >> a[i];
   }
   if (n == 1 || n == 2) {
     cout << 0;
     return 0;
   }
   ans = 1000000007;
   for (int i = -1; i <= 1; ++i) {
     for (int j = -1; j <= 1; ++j) {
       curans = 0;
       int div = a[1] + j - (a[0] + i);
       if (j) curans++;
       if (i) curans++;
       test(div, n, a[1] + j);
     }
   }
   cout << (ans == 1000000007 ? -1 : ans);
   return 0;
 }


#include<bits/stdc++.h>
 #define int long long
 #define pii pair<int,int>
 #define F first
 #define S second
 #define pi acos(-1.0)
 #define pb push_back
 #define sf(x) scanf( %lld ,&x)
 #define pf(x) printf( %lld ,&x)
 using namespace std;
 const int mod=1e9+7;
 int a1[2000],ar[2000],ff[2000] ;
 map<int,int>mp,mk ;
 int bigmod(int a , int b)
 {
     if(b==0) return 1 ;
     int x = bigmod(a,b/2) ;
     x=((x%mod)*(x%mod))%mod ;
     if(b%2) x=((x%mod)*(a%mod))%mod ;
     return x ;
 }
 int ncr(int a , int b)
 {
     int p=(ff[a-b]*ff[b])%mod ;
     p=(ff[a]*bigmod(p,mod-2)) ; p%=mod ;
     return p ;
 }
 int32_t main()
 {
     ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0) ;
     ff[0]=1 ; ff[1]=1 ;
     for(int i = 2 ; i <= 1000 ; i++) ff[i]=(ff[i-1]*i)%mod ;
     int t ; cin>>t ;
     while(t--)
     {
         int n,k,mx=0 ; cin>>n>>k ; mp.clear() ; mk.clear() ;
         set<int>st ;
         for(int i = 1 ; i <= n ; i++){cin>>ar[i] ; mp[ar[i]]++ ; a1[i]=ar[i] ; }
         sort(a1+1,a1+n+1) ; reverse(a1+1,a1+n+1) ;
        // for(int i = 1 ; i <= n ; i++) cout<<a1[i]<<    ; cout<<endl ;
         for(int i = 1 ; i <= k ; i++) {mx+=a1[i] ; st.insert(a1[i]) ; mk[a1[i]]++ ;}
         int ans=1 ;
         for(int x : st)
         {
             int p=mp[x],q=mk[x] ;
           //  cout<<x<<   <<p<<   <<q<<endl ;
             ans*=ncr(p,q); ans%=mod ;
         }
         cout<<ans<<endl ;
     }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e5 + 2, M = 1e9 + 7;
 int tt = 1, n, k;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cin >> tt;
   while (tt--) {
     cin >> n;
     if (n == 1) {
       cout <<  a
 ;
       continue;
     }
     string second =   , ans =   ;
     for (int i = 0; i < n / 2 - 1; i++) second += 'a';
     ans += 'a' + second + 'b' + second;
     if (n & 1) ans += 'c';
     cout << ans <<
 ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 2e5 + 7;
 const long long inf = 1e17 + 7;
 pair<long long, long long> a[maxn];
 pair<long long, long long> A[maxn];
 pair<pair<long long, long long>, pair<long long, long long> > S[maxn];
 int pos;
 inline long long elf(pair<long long, long long> z, long long x) {
   return z.first * x + z.second;
 }
 inline long long earl(pair<long long, long long> z, long long v) {
   long long a = z.first, b = z.second;
   long long x = (v - b) / a;
   if (a * x >= v - b) return x;
   return x + 1;
 }
 inline long double intx(pair<long long, long long> l1,
                         pair<long long, long long> l2) {
   long long a1 = l1.first, a2 = l2.first, b1 = l1.second, b2 = l2.second;
   return (long double)(b2 - b1) / (long double)(a1 - a2);
 }
 void ins(long long a, long long b) {
   while (pos >= 2 && intx(S[pos].first, S[pos - 1].first) >=
                          intx(make_pair(a, b), S[pos].first))
     pos--;
   S[pos].second.second = ceil(intx(S[pos].first, make_pair(a, b))) - 1;
   pos++;
   S[pos] =
       make_pair(make_pair(a, b),
                 make_pair(ceil(intx(S[pos - 1].first, make_pair(a, b))), inf));
 }
 int main() {
   int n;
   long long s;
   scanf( %d %lld , &n, &s);
   for (int i = 1; i <= n; i++) {
     scanf( %lld %lld , &a[i].first, &a[i].second);
   }
   sort(a + 1, a + n + 1);
   a[0].first = -1;
   long long cur = -1;
   long long best = inf;
   int idx = 0;
   for (int i = n; i >= 0; i--) {
     if (a[i].first != cur) {
       if (cur > 0) {
         idx++;
         A[idx] = make_pair(cur, best);
       }
       cur = a[i].first;
       best = inf;
     }
     best = min(best, a[i].second);
   }
   n = idx;
   for (int i = 1; i <= n; i++) a[i] = A[i];
   best = inf;
   idx = 0;
   for (int i = 1; i <= n; i++) {
     if (a[i].second >= best) continue;
     idx++;
     A[idx] = a[i];
     best = min(best, a[i].second);
   }
   n = idx;
   reverse(A + 1, A + n + 1);
   long long ans = inf;
   pos = 1;
   S[pos] = make_pair(make_pair(A[1].first, 0), make_pair(0, inf));
   int fp = 1;
   for (int i = 2; i <= n; i++) {
     while (fp < pos && elf(S[fp].first, S[fp].second.second) < A[i].second)
       fp++;
     long long cx = earl(S[fp].first, A[i].second);
     long long fcx = elf(S[fp].first, cx);
     ins(A[i].first, fcx - A[i].second - A[i].first * cx);
     fp = min(fp, pos);
   }
   for (int i = 1; i <= pos; i++) {
     ans = min(ans, earl(S[i].first, s));
   }
   printf( %lld
 , ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e6 + 3;
 const long long int M = 1000000007;
 void solve() {
   string s;
   cin >> s;
   int n = s.length();
   for (int i = 0; i < n - 1; ++i) {
     s += '0';
   }
   long long int ans = 0;
   for (int i = 0; i < 2 * (n - 1) + 1; ++i) {
     ans += ans + s[i] - '0';
     ans %= M;
   }
   cout << ans;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   solve(), cout <<
 ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct like {
   int id;
   int change = 0;
 };
 int main() {
   int n, s, max = 0, idmax = 0, count = 0, k = 0;
   cin >> n;
   like *A = new like[n];
   for (int i = 0; i < n; i++) {
     cin >> s;
     k = 1;
     for (int j = 0; j < count; j++) {
       if (A[j].id == s) {
         A[j].change++;
         k = 0;
         if (A[j].change > max) {
           max = A[j].change;
           idmax = A[j].id;
         }
       }
     }
     if (k == 1) {
       A[count].id = s;
       A[count].change++;
       if (A[count].change > max) {
         max = A[count].change;
         idmax = A[count].id;
       }
       count++;
     }
   }
   cout << idmax << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int H[100000][5], V[100000][5];
 long long dp[32];
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   int n, m;
   cin >> n >> m;
   for (int i = 1; i < m; ++i)
     for (int j = 0; j < n; ++j) cin >> H[i][j];
   for (int i = 0; i < m; ++i)
     for (int j = 0; j < n; ++j) cin >> V[i][j];
   for (int i = 1; i < (1 << n); ++i) dp[i] = 1e18;
   const int sz = 1 << n;
   for (int i = 1; i < m; ++i) {
     for (int j = 0; j < sz; ++j) {
       for (int k = 0; k < n; ++k) {
         if (!(j & (1 << k))) {
           auto &t = dp[j | (1 << k)];
           const auto &nt = dp[j] + H[i][k];
           if (nt < t) t = nt;
         }
       }
       for (int k = 0; k < n; ++k) {
         if ((((j >> k)) ^ ((j >> ((k + 1) % n)))) & 1) dp[j] += V[i][k];
       }
     }
   }
   cout << dp[sz - 1] << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int arr[100000], arr1[100000], arr2[100000];
 int main() {
   int n, flag = 0;
   cin >> n;
   multiset<long long int> m;
   multiset<long long int>::iterator it;
   for (int i = 0; i < n; i++) {
     cin >> arr[i];
     m.insert(arr[i]);
     if (i == 0)
       arr1[i] = arr[i];
     else
       arr1[i] = arr1[i - 1] + arr[i];
   }
   for (int i = n - 1; i >= 0; i--) {
     if (i == n - 1)
       arr2[n - 1 - i] = arr[i];
     else
       arr2[n - 1 - i] = arr2[n - 2 - i] + arr[i];
   }
   if (arr1[n - 1] % 2 != 0)
     cout <<  NO ;
   else {
     long long int sum = arr1[n - 1] / 2;
     it = m.find(arr[0]);
     m.erase(it);
     if (arr1[0] == sum || arr2[0] == sum) flag = 1;
     if (!flag) {
       for (int i = 1; i < n - 1; i++) {
         if (m.find(sum - arr1[i - 1]) != m.end()) {
           flag = 1;
           break;
         }
         if (!flag) {
           int low = i + 1, high = n - 1;
           while (low < high) {
             int mid = low + (high - low + 1) / 2;
             if (arr1[mid] - arr1[i] + arr1[i - 1] > sum)
               high = mid - 1;
             else
               low = mid;
           }
           if (arr1[low] - arr1[i] + arr1[i - 1] == sum) {
             flag = 1;
             break;
           }
         }
         it = m.find(arr[i]);
         m.erase(it);
       }
     }
     m.clear();
     if (!flag) {
       for (int i = 0; i < n; i++) m.insert(arr[i]);
       it = m.find(arr[n - 1]);
       m.erase(it);
       for (int i = 1; i < n - 1; i++) {
         if (m.find(sum - arr2[i - 1]) != m.end()) {
           flag = 1;
           break;
         }
         int low = i + 1, high = n - 1;
         while (low < high) {
           int mid = low + (high - low + 1) / 2;
           if (arr2[mid] - arr2[i] + arr2[i - 1] > sum)
             high = mid - 1;
           else
             low = mid;
         }
         if (arr2[low] - arr2[i] + arr2[i - 1] == sum) {
           flag = 1;
           cout << i << endl;
           break;
         }
         it = m.find(arr[n - 1 - i]);
         m.erase(it);
       }
     }
     if (flag)
       cout <<  YES ;
     else
       cout <<  NO ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 const ll MOD = 1e9 + 7;
 void dfs(int node, vector<vector<pair<int, int>>>& adj, vector<bool>& vu,
          ll& cnt) {
   for (auto& p : adj[node]) {
     if (!vu[p.first] and p.second == 0) {
       vu[p.first] = true;
       ++cnt;
       dfs(p.first, adj, vu, cnt);
     }
   }
 }
 ll getPow(ll a, ll p) {
   ll ret = 1, cp = a;
   while (p) {
     if (p & 1) ret = (ret * cp) % MOD;
     p >>= 1;
     cp = (cp * cp) % MOD;
   }
   return ret;
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   int n, k, a, b, c;
   cin >> n >> k;
   vector<vector<pair<int, int>>> adj(n + 1);
   for (int i = 1; i < n; ++i) {
     cin >> a >> b >> c;
     adj[a].emplace_back(b, c);
     adj[b].emplace_back(a, c);
   }
   vector<bool> vu(n + 1);
   ll ans = getPow(n, k);
   for (int i = 1; i <= n; ++i) {
     if (!vu[i]) {
       ll tmp = 1;
       vu[i] = true;
       dfs(i, adj, vu, tmp);
       ans = (ans - getPow(tmp, k)) % MOD;
     }
   }
   cout << (ans + MOD) % MOD;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, v[5000];
 char a[5000][5050];
 bool dfs(int x, int from) {
   v[x] = 1;
   for (int y = 0; y < n; y++)
     if (a[x][y] == '1') {
       if (!v[y]) {
         if (dfs(y, x)) return true;
       } else {
         if (a[y][from] == '1') {
           printf( %d %d %d
 , y + 1, from + 1, x + 1);
           return true;
         }
       }
     }
   return false;
 }
 int main() {
   scanf( %d , &n);
   gets(a[0]);
   for (int i = 0; i < n; i++) gets(a[i]);
   for (int i = 0; i < n; i++)
     if (!v[i])
       if (dfs(i, -1)) return 0;
   puts( -1 );
 }


#include <bits/stdc++.h>
 const double EPS = 0.0000000001;
 const long long mod1 = 998244353;
 const long long mod2 = 1000000007;
 const long long mod3 = 1000000009;
 const long long mod4 = 998244353;
 const long long inf = 10000000000000000;
 using namespace std;
 signed main() {
   cin.tie(0);
   cout.tie(0);
   std::ios_base::sync_with_stdio(0);
   long long n, a;
   cin >> n >> a;
   long long matrix[a][n];
   for (long long i = 0; i < a; i++) {
     for (long long z = 0; z < n; z++) {
       char c;
       cin >> c;
       matrix[i][z] = (c - '0');
     }
   }
   bool used[a];
   for (long long i = 0; i < a; i++) {
     bool p = 0;
     for (long long z = 0; z < n; z++) {
       if (!matrix[i][z]) p = 1;
     }
     used[i] = p;
   }
   long long c = 0;
   long long x = 1;
   long long maxi = -1;
   for (long long i = 0; i < a; i++) {
     if (used[i]) {
       c++;
       continue;
     }
     maxi = max(maxi, c);
     c = 0;
   }
   maxi = max(maxi, c);
   cout << maxi;
 }


#include <bits/stdc++.h>
 using namespace std;
 pair<int, int> x[1024], y[1024];
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     for (int i = 0; i < n; i++) {
       cin >> x[i].first >> x[i].second;
       y[i].first = x[i].first;
       y[i].second = x[i].second;
     }
     sort(x, x + n, [](const pair<int, int>& a, const pair<int, int>& b) {
       return a.first < b.first || (a.first == b.first && a.second < b.second);
     });
     sort(y, y + n, [](const pair<int, int>& a, const pair<int, int>& b) {
       return a.second < b.second || (a.second == b.second && a.first < b.first);
     });
     string ans =   ;
     int r = 0, c = 0;
     for (int i = 0; i < n; i++) {
       if (x[i] != y[i]) {
         cout <<  NO  << endl;
         ans =   ;
         break;
       } else {
         ans += string(x[i].first - r, 'R');
         ans += string(x[i].second - c, 'U');
         r = x[i].first;
         c = x[i].second;
       }
     }
     if (!ans.empty()) {
       cout <<  YES  << endl << ans << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s[123456];
 int main() {
   scanf( %s , s);
   stack<pair<int, int> > st;
   int best = 0, rr = -1;
   int sqb = 0;
   for (int i = 0; s[i]; ++i) {
     if (s[i] == '(' || s[i] == '[') {
       st.push(make_pair(i, sqb));
       sqb = 0;
     } else if (s[i] == ')') {
       if (st.size() > 0 && s[st.top().first] == '(') {
         sqb += st.top().second;
         if (best < sqb) {
           best = sqb;
           rr = i;
         }
         st.pop();
       } else {
         stack<pair<int, int> >().swap(st);
         sqb = 0;
       }
     } else if (s[i] == ']') {
       if (st.size() > 0 && s[st.top().first] == '[') {
         sqb += st.top().second + 1;
         if (best < sqb) {
           best = sqb;
           rr = i;
         }
         st.pop();
       } else {
         stack<pair<int, int> >().swap(st);
         sqb = 0;
       }
     }
   }
   s[rr + 1] = 0;
   int ll = rr;
   stack<int> out;
   for (int i = rr; i >= 0; --i) {
     if (s[i] == ')' || s[i] == ']') {
       out.push(i);
     } else if (s[i] == '(') {
       if (out.size() > 0 && s[out.top()] == ')') {
         ll = i;
         out.pop();
       } else
         break;
     } else if (s[i] == '[') {
       if (out.size() > 0 && s[out.top()] == ']') {
         ll = i;
         out.pop();
       } else
         break;
     }
   }
   while (out.size() > 0) {
     ll = out.top() + 1;
     out.pop();
   }
   printf( %d
 , best);
   puts(s + ll);
   return 0;
 }


#include <bits/stdc++.h>
 template <int MOD_>
 struct modnum {
   static constexpr int MOD = MOD_;
   static_assert(MOD_ > 0,  MOD must be positive );

  private:
   using ll = long long;
   int v;
   static int minv(int a, int m) {
     a %= m;
     assert(a);
     return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
   }

  public:
   modnum() : v(0) {}
   modnum(ll v_) : v(int(v_ % MOD)) {
     if (v < 0) v += MOD;
   }
   explicit operator int() const { return v; }
   friend std::ostream& operator<<(std::ostream& out, const modnum& n) {
     return out << int(n);
   }
   friend std::istream& operator>>(std::istream& in, modnum& n) {
     ll v_;
     in >> v_;
     n = modnum(v_);
     return in;
   }
   friend bool operator==(const modnum& a, const modnum& b) {
     return a.v == b.v;
   }
   friend bool operator!=(const modnum& a, const modnum& b) {
     return a.v != b.v;
   }
   modnum inv() const {
     modnum res;
     res.v = minv(v, MOD);
     return res;
   }
   friend modnum inv(const modnum& m) { return m.inv(); }
   modnum neg() const {
     modnum res;
     res.v = v ? MOD - v : 0;
     return res;
   }
   friend modnum neg(const modnum& m) { return m.neg(); }
   modnum operator-() const { return neg(); }
   modnum operator+() const { return modnum(*this); }
   modnum& operator++() {
     v++;
     if (v == MOD) v = 0;
     return *this;
   }
   modnum& operator--() {
     if (v == 0) v = MOD;
     v--;
     return *this;
   }
   modnum& operator+=(const modnum& o) {
     v += o.v;
     if (v >= MOD) v -= MOD;
     return *this;
   }
   modnum& operator-=(const modnum& o) {
     v -= o.v;
     if (v < 0) v += MOD;
     return *this;
   }
   modnum& operator*=(const modnum& o) {
     v = int(ll(v) * ll(o.v) % MOD);
     return *this;
   }
   modnum& operator/=(const modnum& o) { return *this *= o.inv(); }
   friend modnum operator++(modnum& a, int) {
     modnum r = a;
     ++a;
     return r;
   }
   friend modnum operator--(modnum& a, int) {
     modnum r = a;
     --a;
     return r;
   }
   friend modnum operator+(const modnum& a, const modnum& b) {
     return modnum(a) += b;
   }
   friend modnum operator-(const modnum& a, const modnum& b) {
     return modnum(a) -= b;
   }
   friend modnum operator*(const modnum& a, const modnum& b) {
     return modnum(a) *= b;
   }
   friend modnum operator/(const modnum& a, const modnum& b) {
     return modnum(a) /= b;
   }
 };
 int main() {
   using namespace std;
   ios_base::sync_with_stdio(false), cin.tie(nullptr);
   using num = modnum<int(1e9) + 7>;
   int Q;
   cin >> Q;
   string A;
   cin >> A;
   vector<num> dp(int(A.size()) + 1, num(1));
   Q--;
   while (Q--) {
     string B;
     cin >> B;
     vector<num> ndp(int(B.size()) + 1, num(0));
     int Asize = int(A.size());
     int Bsize = int(B.size());
     int L = max(Asize + 1, Bsize + 1);
     A.resize(L + 1, '$');
     B.resize(L + 1, '%');
     dp.resize(L + 1, num(0));
     ndp.resize(L, num(0));
     vector<array<array<num, 2>, 2>> pref_dp(L);
     pref_dp[0][0][0]++;
     for (int i = 0; i < L; i++) {
       for (int z : {0, 1}) {
         pref_dp[i][1][z] += dp[i] * pref_dp[i][0][z];
       }
       if (i + 1 == L) break;
       for (int z : {0, 1}) {
         pref_dp[i + 1][z][1] += pref_dp[i][z][1];
         if (A[i + z] == B[i]) {
           pref_dp[i + 1][z][0] += pref_dp[i][z][0];
         } else if (A[i + z] < B[i]) {
           pref_dp[i + 1][z][1] += pref_dp[i][z][0];
         }
       }
     }
     bool suff_less = true;
     num ways_suff = num(0);
     num ways_suff_less = num(0);
     for (int i = L - 1; i >= 0; i--) {
       ways_suff += dp[i + 1];
       if (suff_less) {
         ways_suff_less += dp[i + 1];
       }
       if (A[i] < B[i + 1]) {
         ways_suff_less = ways_suff;
       } else if (A[i] > B[i + 1]) {
         ways_suff_less = 0;
       }
       if (A[i + 1] != B[i + 1]) {
         suff_less = A[i + 1] < B[i + 1];
       }
       ndp[i] += ways_suff_less * pref_dp[i][0][0];
       ndp[i] += suff_less * pref_dp[i][1][0];
       ndp[i] += ways_suff * pref_dp[i][0][1];
       ndp[i] += pref_dp[i][1][1];
     }
     A.resize(Asize);
     B.resize(Bsize);
     dp.resize(Asize + 1);
     ndp.resize(Bsize + 1);
     A = std::move(B);
     dp = std::move(ndp);
   }
   num ans = accumulate(dp.begin(), dp.end(), num(0));
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int dx[] = {0, 0, 1, -1};
 int dy[] = {-1, 1, 0, 0};
 template <class T>
 T power(T N, T P) {
   return (P == 0) ? 1 : N * power(N, P - 1);
 }
 template <class T>
 T gcd(T a, T b) {
   if (b == 0) return a;
   return gcd(b, a % b);
 }
 struct debugger {
   template <typename T>
   debugger& operator,(const T& v) {
     cerr << v <<    ;
     return *this;
   }
 } dbg;
 void make_unique(vector<int>& a) {
   sort(a.begin(), a.end());
   a.erase(unique(a.begin(), a.end()), a.end());
 }
 int on(int n, int pos) { return n = n | (1 << pos); }
 void off(int& n, int pos) { n = n & ~(1 << pos); }
 bool check(int n, int pos) { return (bool)(n & (1 << pos)); }
 const int N = 1e5 + 10;
 const int M = 64;
 int id, cnt[M * N], a[N * M][5];
 int new_node() {
   for (int i = 0; i < 3; i++) {
     a[id][i] = -1;
   }
   cnt[id] = 0;
   return id++;
 }
 vector<int> bit(70);
 void make(long long int x) {
   bit.clear();
   int i = 0;
   while (x) {
     bit.push_back(x % 2);
     x /= 2LL;
     i++;
   }
   while (i < M - 1) {
     bit.push_back(0LL);
     i++;
   }
   reverse(bit.begin(), bit.end());
 }
 void add(long long int x) {
   make(x);
   int cur = 0, typ;
   for (int i = 0; i < M; i++) {
     typ = bit[i];
     if (a[cur][typ] == -1) {
       a[cur][typ] = new_node();
     }
     cur = a[cur][typ];
     ++cnt[cur];
   }
 }
 void del(long long int x) {
   make(x);
   int cur = 0, typ;
   for (int i = 0; i < M; i++) {
     typ = bit[i];
     cur = a[cur][typ];
     --cnt[cur];
   }
 }
 long long int query(long long int x) {
   make(x);
   int cur = 0, typ;
   long long int ans = 0LL;
   for (int i = 0; i < M; i++) {
     typ = bit[i];
     if (cnt[a[cur][!typ]] > 0) {
       ans += (1LL << (M - i - 2));
       cur = a[cur][!typ];
     } else {
       if (a[cur][typ] == -1) return ans;
       cur = a[cur][typ];
     }
   }
   return ans;
 }
 long long int arr[N];
 int main() {
   int n;
   while (scanf( %d , &n) != EOF) {
     id = 0;
     new_node();
     for (__typeof(n) i = 1; i <= n; i++) {
       scanf( %lld , &arr[i]);
     }
     long long int xr = 0LL;
     for (int i = n; i >= 1; i--) {
       xr ^= arr[i];
       add(xr);
     }
     long long int mx = 0LL;
     long long int nw = 0LL;
     mx = max(mx, query(nw));
     for (int i = 1; i <= n; i++) {
       nw ^= arr[i];
       mx = max(mx, query(nw));
       del(xr);
       xr ^= arr[i];
     }
     printf( %lld
 , mx);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t > 0) {
     t--;
     string s, x;
     cin >> s;
     x.push_back(s[0]);
     for (int i = 1; i < s.size(); i++) {
       if (i % 2 != 0) x.push_back(s[i]);
     }
     cout << x << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(false);
   int n, m, a;
   cin >> n >> m;
   int l = 1, mx = 0;
   for (int i = 0; i < n; i++) {
     cin >> a;
     if ((a + m - 1) / m >= mx) {
       mx = (a + m - 1) / m;
       l = i + 1;
     }
   }
   cout << l << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long const M = 2000 + 10, M2 = 1e7 + 10, mod = 1e9 + 7, inf = 1e9 + 10,
                 sq = 450;
 long long a[M][M], ro[M], col[M], all[M];
 int32_t main() {
   long long n, ans = 0;
   cin >> n;
   for (long long i = 1; i <= n; i++) {
     string x;
     cin >> x;
     for (long long j = 0; j < x.size(); j++) {
       a[i][j + 1] = x[j] - '0';
     }
   }
   for (long long i = 1; i <= n; i++) {
     for (long long j = n; j > i; j--) {
       long long good = col[j] xor ro[i];
       long long need = good xor a[i][j];
       ro[i] = ro[i] xor need;
       all[i] = all[i] xor need;
       col[j] = col[j] xor need;
       all[j] = all[j] xor need;
       ans += need;
     }
   }
   for (long long i = 1; i <= n; i++) col[i] = 0, ro[i] = 0;
   for (long long i = n; i >= 1; i--) {
     for (long long j = 1; j < i; j++) {
       long long good = col[j] xor ro[i];
       long long need = good xor a[i][j];
       ro[i] = ro[i] xor need;
       all[i] = all[i] = all[i] xor need;
       col[j] = col[j] xor need;
       all[j] = all[j] xor need;
       ans += need;
     }
   }
   for (long long i = 1; i <= n; i++) {
     ans += all[i] xor a[i][i];
   }
   cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long INF = 1e18;
 int32_t main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   long long n, m;
   cin >> n >> m;
   long long a[n], b[m];
   for (long long i = 0; i < n; ++i) {
     cin >> a[i];
   }
   for (long long i = 0; i < m; ++i) {
     cin >> b[i];
   }
   long long ans = INF;
   for (long long i = 0; i < n; ++i) {
     long long res = -INF;
     for (long long j = 0; j < n; ++j) {
       if (i != j) {
         for (long long k = 0; k < m; ++k) {
           res = max(res, a[j] * b[k]);
         }
       }
     }
     ans = min(ans, res);
   }
   cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 + 5;
 int n, x[maxn], y[maxn];
 int solve(set<pair<int, int> >& a, set<pair<int, int> >& b) {
   set<pair<int, int> >::iterator itl = a.begin(), itd = b.begin(), it1;
   set<pair<int, int> >::reverse_iterator itr = a.rbegin(), itu = b.rbegin(),
                                          it2;
   set<pair<int, int> > f, s;
   bool chk = false;
   for (int i = 0; i < a.size() - 1; ++i) {
     if (next(itl)->first > itl->first + 1) {
       chk = true;
       for (it1 = a.begin(); it1 != next(itl); ++it1) {
         f.insert({x[it1->second], it1->second});
       }
     } else if (next(itd)->first > itd->first + 1) {
       chk = true;
       for (it1 = b.begin(); it1 != next(itd); ++it1) {
         f.insert({x[it1->second], it1->second});
       }
     } else if (next(itr)->first + 1 < itr->first) {
       chk = true;
       for (it2 = a.rbegin(); it2 != next(itr); ++it2) {
         f.insert({x[it2->second], it2->second});
       }
     } else if (next(itu)->first + 1 < itu->first) {
       chk = true;
       for (it2 = b.rbegin(); it2 != next(itu); it2++) {
         f.insert({x[it2->second], it2->second});
       }
     }
     if (chk) {
       for (auto u : f) {
         s.insert({y[u.second], u.second});
         b.erase({y[u.second], u.second});
         a.erase(u);
       }
       return solve(a, b) + solve(f, s);
     }
     itl++;
     itr++;
     itd++;
     itu++;
   }
   return 1;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cin >> n;
   set<pair<int, int> > xs, ys;
   for (int i = 0; i < n; ++i) {
     cin >> x[i] >> y[i];
     xs.insert({x[i], i});
     ys.insert({y[i], i});
   }
   cout << solve(xs, ys);
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int t = 1, i, j, n;
   long long int p, q, temp, arr[90];
   for (j = 0; j < t; j++) {
     scanf( %lld %lld , &p, &q);
     scanf( %d , &n);
     for (i = 0; i < n; i++) scanf( %lld , &arr[i]);
     for (i = 0; i < n; i++) {
       if (p / q < arr[i]) {
         printf( NO
 );
         break;
       }
       p = p - (q * arr[i]);
       if (p == 0 && i != n - 1) {
         printf( NO
 );
         break;
       }
       if (i != n - 1) {
         temp = p;
         p = q;
         q = temp;
       }
     }
     if (p == 0 && i == n)
       printf( YES
 );
     else if (i == n)
       printf( NO
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int bits = 1000;
 bitset<bits> node[4 * 100060];
 long long int leftOver[4 * 100060];
 int n;
 int treeSize[100060];
 vector<int> adj[100060];
 int dfsOrder[100060];
 int o = 1;
 void dfs(int curr = 1, int par = -1) {
   treeSize[curr] = 1;
   dfsOrder[curr] = o++;
   for (int nxt : adj[curr]) {
     if (nxt != par) {
       dfs(nxt, curr);
       treeSize[curr] += treeSize[nxt];
     }
   }
 }
 int m;
 bitset<bits> rotate(std::bitset<bits> b, unsigned k) {
   b = b << (k) | b >> (m - (k));
   b = b | (b >> m);
   return b;
 }
 void build(int pos, int bit, int id = 1, int l = 1, int r = n) {
   if (l == r) {
     node[id][bit] = 1;
     return;
   }
   int mid = (l + r) >> 1;
   if (pos <= mid)
     build(pos, bit, id << 1, l, mid);
   else
     build(pos, bit, (id << 1) + 1, mid + 1, r);
   node[id] = node[id << 1] | node[(id << 1) + 1];
 }
 void push(int id) {
   if (leftOver[id] > 0) {
     leftOver[id << 1] = (leftOver[id] + leftOver[id << 1]) % m;
     leftOver[(id << 1) + 1] = (leftOver[id] + leftOver[(id << 1) + 1]) % m;
     leftOver[id] = 0;
   }
 }
 void add(int val, int L, int R, int id = 1, int l = 1, int r = n) {
   if (L <= l && r <= R) {
     leftOver[id] = (leftOver[id] + val) % m;
     return;
   }
   push(id);
   int mid = (l + r) >> 1;
   if (L <= mid) add(val, L, R, id << 1, l, mid);
   if (R > mid) add(val, L, R, (id << 1) + 1, mid + 1, r);
   node[id] = rotate(node[id << 1], leftOver[id << 1]) |
              rotate(node[(id << 1) + 1], leftOver[(id << 1) + 1]);
 }
 bitset<bits> ans;
 void query(int qL, int qR, int id = 1, int l = 1, int r = n) {
   if (qL <= l && qR >= r) {
     ans = ans | rotate(node[id], leftOver[id]);
     return;
   }
   push(id);
   int mid = (l + r) >> 1;
   if (qL <= mid) query(qL, qR, id << 1, l, mid);
   if (qR > mid) query(qL, qR, (id << 1) + 1, mid + 1, r);
   node[id] = rotate(node[id << 1], leftOver[id << 1]) |
              rotate(node[(id << 1) + 1], leftOver[(id << 1) + 1]);
 }
 vector<int> primes;
 vector<int> sieve(int limit) {
   vector<int> primes;
   bool in[limit + 1];
   memset(in, true, sizeof(in));
   for (int i = 2; i <= limit; i++) {
     if (in[i]) {
       primes.push_back(i);
       for (int j = i; j <= limit; j += i) in[j] = false;
     }
   }
   return primes;
 }
 int a[100060];
 int aTemp[100060];
 int addBrute(int l, int r, int x) {
   for (int i = l; i < r + 1; i++) a[i] = (a[i] + x) % m;
 }
 bitset<bits> findBrute(int l, int r) {
   bitset<bits> temp = 0;
   for (int i = l; i < r + 1; i++) temp[a[i] % m] = 1;
   return temp;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   int q;
   cin >> n >> m;
   primes = sieve(m);
   for (int i = 0; i < n; i++) cin >> a[i + 1];
   for (int i = 0; i < n - 1; i++) {
     int x, y;
     cin >> x >> y;
     adj[x].push_back(y);
     adj[y].push_back(x);
   }
   dfs();
   for (int i = 0; i < n; i++) {
     build(dfsOrder[i + 1], a[i + 1] % m);
   }
   for (int i = 1; i < n + 1; i++) {
     aTemp[dfsOrder[i]] = a[i];
   }
   for (int i = 1; i < n + 1; i++) {
     a[i] = aTemp[i];
   }
   cin >> q;
   for (int i = 0; i < q; i++) {
     int t, v;
     cin >> t >> v;
     if (t == 1) {
       int x;
       cin >> x;
       add(x % m, dfsOrder[v], dfsOrder[v] + treeSize[v] - 1);
     } else {
       ans = 0;
       query(dfsOrder[v], dfsOrder[v] + treeSize[v] - 1);
       int k = 0;
       for (int j = 0; j < primes.size(); j++) {
         if (primes[j] < m) {
           if (ans[primes[j]]) {
             k++;
           }
         }
       }
       cout << k <<
 ;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 100010;
 struct seg {
   vector<int> t;
   int n;
   seg(int n = 0) : t(2 * n), n(n) {}
   int query(int l, int r) {
     int ans = 0;
     for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
       if (l & 1) ans += t[l++];
       if (r & 1) ans += t[--r];
     }
     return ans;
   }
   void update(int p, int v) {
     for (t[p += n] += v; p > 1; p >>= 1) {
       t[p >> 1] = t[p] + t[p ^ 1];
     }
   }
 };
 seg s[4][11][10];
 void modify(int p, int c, int m) {
   for (int i = 1; i <= 10; i++) {
     s[c][i][p % i].update(p, m);
   }
 }
 void convert(string& s) {
   for (char& c : s) {
     switch (c) {
       case 'A':
         c = 0;
         break;
       case 'T':
         c = 1;
         break;
       case 'C':
         c = 2;
         break;
       case 'G':
         c = 3;
         break;
     }
   }
 }
 int main() {
   string k;
   cin >> k;
   convert(k);
   for (int i = 0; i < 4; i++) {
     for (int j = 1; j <= 10; j++) {
       for (int l = 0; l < j; l++) {
         s[i][j][l] = seg(k.size());
       }
     }
   }
   for (int i = 0; i < k.size(); i++) {
     modify(i, k[i], 1);
   }
   int q;
   cin >> q;
   while (q--) {
     int op;
     cin >> op;
     if (op == 1) {
       int x;
       string c;
       cin >> x >> c;
       convert(c);
       x--;
       modify(x, k[x], -1);
       k[x] = c[0];
       modify(x, k[x], 1);
     } else {
       int l, r;
       cin >> l >> r;
       l--;
       string e;
       cin >> e;
       convert(e);
       int a = 0;
       for (int i = l; i < l + e.size(); i++) {
         a += s[e[i - l]][e.size()][i % e.size()].query(l, r);
       }
       cout << a <<
 ;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
 const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
           d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 void DBG() { cout <<  ]  << endl; }
 template <class H, class... T>
 void DBG(H h, T... t) {
   cout << to_string(h);
   if (sizeof...(t)) cout <<  ,  ;
   DBG(t...);
 }
 template <class T>
 bool umin(T& a, const T& b) {
   return b < a ? a = b, 1 : 0;
 }
 template <class T>
 bool umax(T& a, const T& b) {
   return a < b ? a = b, 1 : 0;
 }
 int n;
 vector<pair<long long, long long>> cards[(long long)(200002)],
     need[(long long)(200002)];
 vector<array<long long, 3>> valid[(long long)(200002)];
 void prepare() {
   for (int i = (1), _c = (n); i <= _c; ++i) {
     vector<int> cnt(4, 0);
     sort((cards[i]).begin(), (cards[i]).end(),
          [](pair<long long, long long> a, pair<long long, long long> b) {
            return a.first < b.first ||
                   (a.first == b.first && a.second > b.second);
          });
     for (auto j : cards[i]) {
       if (j.first == 1 && cnt[j.first] >= 3) continue;
       if (j.first == 2 && cnt[j.first] >= 1) continue;
       if (j.first == 3 && cnt[j.first] >= 1) continue;
       need[i].push_back(j);
       ++cnt[j.first];
     }
   }
   for (int i = (1), _c = (n); i <= _c; ++i) {
     for (int j = (0), _c = ((1ll << (((int)((need[i]).size())))) - 1); j <= _c;
          ++j) {
       long long cost = 0, ma = 0, dame = 0;
       for (int k = (0), _c = (((int)((need[i]).size())) - 1); k <= _c; ++k) {
         if ((((j) >> (k)) & 1ll)) {
           cost += need[i][k].first;
           dame += need[i][k].second;
           umax(ma, need[i][k].second);
         }
       }
       if (cost <= 3) {
         int bonus = __builtin_popcountll(j);
         valid[i].push_back({bonus, dame, ma});
       }
     }
   }
 }
 long long dp[(long long)(200002)][11];
 void test_case() {
   cin >> n;
   for (int i = (1), _c = (n); i <= _c; ++i) {
     int k;
     cin >> k;
     cards[i].assign(k, {0, 0});
     for (int j = (0), _c = (k - 1); j <= _c; ++j)
       cin >> cards[i][j].first >> cards[i][j].second;
   }
   prepare();
   for (auto i : valid[1]) umax(dp[1][i[0]], i[1]);
   for (int i = (1), _c = (n - 1); i <= _c; ++i)
     for (int j = (0), _c = (10); j <= _c; ++j)
       if (dp[i][j] || j == 0) {
         for (auto k : valid[i + 1]) {
           int nxtNum = j + k[0];
           if (nxtNum < 10)
             umax(dp[i + 1][nxtNum], dp[i][j] + k[1]);
           else
             umax(dp[i + 1][nxtNum % 10], dp[i][j] + k[1] + k[2]);
         }
       }
   long long ans = 0;
   for (int i = (0), _c = (10); i <= _c; ++i) umax(ans, dp[n][i]);
   cout << ans <<
 ;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   auto time = clock();
   int test = 1;
   while (test--) {
     test_case();
   }
   cerr <<  [It took   << fixed << setprecision(3)
        << double(clock() - time) / CLOCKS_PER_SEC <<  s]
        <<
 ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void Get(int &T) {
   char C;
   bool F = 0;
   for (; C = getchar(), C < '0' || C > '9';)
     if (C == '-') F = 1;
   for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
     ;
   F && (T = -T);
 }
 int N;
 void Init() { Get(N); }
 int T[55][55];
 void Mul(int A[][55], int B[][55], int N, int K, int M) {
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= M; j++) {
       T[i][j] = 0;
       for (int k = 1; k <= K; k++) (T[i][j] ^= (A[i][k] & B[k][j]));
     }
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= M; j++) A[i][j] = T[i][j];
 }
 int A[55][55];
 int B[55][55];
 int T2[55][55];
 void Pow(int X[][55], long long P) {
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= N; j++) T2[i][j] = (i == j);
   for (; P; P >>= 1) {
     if (P & 1) Mul(T2, X, N, N, N);
     Mul(X, X, N, N, N);
   }
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= N; j++) X[i][j] = T2[i][j];
 }
 long long Factor[100005];
 int FactorTop;
 bool I() {
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= N; j++)
       if (B[i][j] != (i == j)) return 0;
   return 1;
 }
 bool Check() {
   for (int i = 1; i <= N; i++)
     for (int j = 1; j <= N; j++) B[i][j] = A[i][j];
   Pow(B, (1ll << N) - 1);
   int Ans = I();
   if (Ans == 0) return 0;
   for (int k = 1; k <= FactorTop; k++) {
     for (int i = 1; i <= N; i++)
       for (int j = 1; j <= N; j++) B[i][j] = A[i][j];
     Pow(B, ((1ll << N) - 1) / Factor[k]);
     int Ans = I();
     if (Ans == 1) return 0;
   }
   return 1;
 }
 void Debug() {
   FactorTop = 0;
   long long X = 1ll << N;
   X--;
   Factor[0] = 1;
   for (long long i = 2; i * i <= X; i++) {
     for (;; X /= i) {
       if (X % i != 0) break;
       Factor[++FactorTop] = i;
     }
   }
   if (X > 1) Factor[++FactorTop] = X;
   for (int i = 1; i < N; i++) A[i + 1][i] = 1;
   for (;;) {
     for (int i = 1; i <= N; i++) A[i][N] = rand() & 1;
     int Ans = Check();
     if (Ans) break;
   }
 }
 void Work() { Debug(); }
 void Output() {
   for (int i = N; i >= 1; i--) printf( %d  , A[i][N]);
   puts(  );
   for (int i = N; i >= 1; i--) printf( %d  , 1);
   puts(  );
 }
 int main() {
   srand(time(0));
   Init();
   Work();
   Output();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long t;
 long long n;
 long long tmp;
 long long gcd(long long a, long long b) {
   long long temp;
   if (a < b) {
     temp = a;
     a = b;
     b = temp;
   }
   while (b != 0) {
     temp = a % b;
     a = b;
     b = temp;
   }
   return a;
 }
 long long lcm(long long a, long long b) {
   long long temp_lcm;
   temp_lcm = a * b / gcd(a, b);
   return temp_lcm;
 }
 int main() {
   cin >> t;
   while (t--) {
     cin >> n;
     if (n % 2 == 0) {
       long long a, b;
       tmp = n / 2;
       while (tmp--) {
         cin >> a >> b;
         cout << b << ' ' << -a << ' ';
       }
       cout << endl;
     } else {
       long long a, b, c;
       tmp = (n - 2) / 2;
       while (tmp--) {
         cin >> a >> b;
         cout << b << ' ' << -a << ' ';
       }
       cin >> a >> b >> c;
       tmp = lcm(a, b);
       tmp = lcm(tmp, c);
       cout << tmp / a << ' ' << tmp / b << ' ' << -2 * tmp / c;
       cout << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   int max = 0, min = 0;
   int a[m], b[m];
   for (int i = 0; i < m; i++) {
     cin >> a[i];
     b[i] = a[i];
   }
   sort(a, a + m, greater<int>());
   for (int i = 0; i < n; i++) {
     if (a[0] != 0) {
       max = max + a[0];
       a[0] = a[0] - 1;
       sort(a, a + m, greater<int>());
     } else {
       break;
     }
   }
   sort(b, b + m);
   int i = 0, k = 0;
   while (i != n) {
     if (b[k] != 0) {
       min = min + b[k];
       b[k] = b[k] - 1;
     } else {
       k++;
       min = min + b[k];
       b[k] = b[k] - 1;
     }
     i++;
   }
   cout << max <<     << min;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool isPrime[1000005] = {false};
 vector<long long int> SPF(1000005);
 long long int fastPower(long long int a, long long int b) {
   if (b == 0) return 1;
   long long int prod = 1;
   while (b > 0) {
     if (b & 1) prod = prod * a;
     b = b >> 1;
     a = a * a;
   }
   return prod;
 }
 void sieve() {
   int i;
   for (i = 2; i < 1000005; ++i) isPrime[i] = 1;
   for (i = 2; i * i < 1000005; ++i) {
     if (isPrime[i]) {
       for (int j = i * i; j < 1000005; j += i) isPrime[j] = 0;
     }
   }
 }
 void printa(bool f) {
   if (f)
     cout <<  YES ;
   else
     cout <<  NO ;
   cout <<
 ;
 }
 bool flip(bool f) {
   if (f) return 0;
   return 1;
 }
 void solve() {
   long long int i, n;
   cin >> n;
   vector<long long int> a(n);
   for (i = 0; i < n; ++i) cin >> a[i];
   sort(a.begin(), a.end());
   long long int s = 0, m = a[n - 1];
   for (i = 0; i < n - 1; ++i) s += a[i];
   cout << abs(m - s) + 1 <<
 ;
 }
 int main() {
   int tc = 1;
   while (tc--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long int m, n, p, count = 0;
   cin >> m >> n;
   if (m % n == 0)
     cout << m / n;
   else {
     count = count + (m / n);
     m = m % n;
     for (;;) {
       if (m > n) {
         if (m % n == 0) {
           count += m / n;
           break;
         }
         m = m - n;
         count++;
       } else if (n > m) {
         if (n % m == 0) {
           count += n / m;
           break;
         }
         n = n - m;
         count++;
       }
     }
     cout << count;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 5;
 const int MAXM = 20;
 int n, m;
 int a[MAXN], b[MAXM];
 int num[MAXM];
 char ans[MAXN][MAXM];
 set<int> states[MAXM];
 int getState(int mask, int num[MAXM]) {
   int res = mask;
   for (int i = 0; i < n; ++i) {
     res = res * MAXN + num[i];
   }
   return res;
 }
 bool dfs(int j, int lastState = 0) {
   if (j == m) {
     for (int i = 0; i < n; ++i) {
       if (num[i] != a[i]) {
         return false;
       }
     }
     return true;
   }
   if (j) {
     if (states[j].find(lastState) != states[j].end()) {
       return false;
     }
     states[j].insert(lastState);
   }
   int origin[MAXM];
   memcpy(origin, num, sizeof(num));
   for (int k = 0; k < (1 << n); ++k) {
     int cnt = 0;
     for (int i = 0; i < n; ++i) {
       if (k & (1 << i)) {
         ans[i][j] = '*';
         if (i == 0 || ans[i - 1][j] == '.') {
           ++cnt;
         }
       } else {
         ans[i][j] = '.';
       }
     }
     if (cnt != b[j]) {
       continue;
     }
     for (int i = 0; i < n; ++i) {
       if (ans[i][j] == '*') {
         if (j == 0 || ans[i][j - 1] == '.') {
           ++num[i];
         }
       }
     }
     bool valid = true;
     for (int i = 0; i < n; ++i) {
       if (num[i] > a[i]) {
         valid = false;
         break;
       }
       if (ans[i][j] == '*') {
         if (a[i] - num[i] > (m - j - 1) / 2) {
           valid = false;
           break;
         }
       } else {
         if (a[i] - num[i] > (m - j) / 2) {
           valid = false;
           break;
         }
       }
     }
     if (valid) {
       if (dfs(j + 1, getState(k, num))) {
         return true;
       }
     }
     memcpy(num, origin, sizeof(origin));
   }
   return false;
 }
 int main() {
   while (cin >> n >> m) {
     for (int i = 0; i < n; ++i) {
       cin >> a[i];
     }
     for (int i = 0; i < m; ++i) {
       cin >> b[i];
     }
     for (int i = 0; i < n; ++i) {
       states[i].clear();
     }
     memset(num, 0, sizeof(num));
     dfs(0);
     for (int i = 0; i < n; ++i) {
       for (int j = 0; j < m; ++j) {
         cout << ans[i][j];
       }
       cout << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 100500;
 const int INF = 2 * (1e9);
 int n, m;
 vector<pair<int, int> > edge[N];
 vector<pair<int, int> > delay[N];
 int dist[N];
 int main() {
   cin.sync_with_stdio(false);
   cin >> n >> m;
   for (int i = 0; i < m; i++) {
     int from, to, len;
     cin >> from >> to >> len;
     from--;
     to--;
     edge[from].push_back(make_pair(to, len));
     edge[to].push_back(make_pair(from, len));
   }
   for (int i = 0; i < n; i++) {
     int num;
     cin >> num;
     int cur;
     for (int j = 0; j < num; j++) {
       cin >> cur;
       if (j == 0 or delay[i].back().second != cur)
         delay[i].push_back(make_pair(cur, cur + 1));
       else
         delay[i].back().second++;
     }
   }
   for (int i = 0; i < n; i++) dist[i] = INF;
   dist[0] = 0;
   if (delay[0].size() >= 1 and delay[0][0].first == 0)
     dist[0] = delay[0][0].second;
   set<pair<int, int> > dijkstra;
   dijkstra.insert(make_pair(dist[0], 0));
   for (int i = 0; i < n; i++) {
     if (dijkstra.empty()) break;
     pair<int, int> now = *dijkstra.begin();
     dijkstra.erase(now);
     for (const pair<int, int> &next : edge[now.second]) {
       int arrival = next.first;
       int tm = next.second + now.first;
       int idx = lower_bound(delay[arrival].begin(), delay[arrival].end(),
                             make_pair(tm, INF)) -
                 delay[arrival].begin();
       if (idx > 0 and arrival != n - 1) {
         idx--;
         if (delay[arrival][idx].first <= tm and tm < delay[arrival][idx].second)
           tm = delay[arrival][idx].second;
       }
       if (dist[arrival] > tm) {
         dijkstra.erase(make_pair(dist[arrival], arrival));
         dist[arrival] = tm;
         dijkstra.insert(make_pair(dist[arrival], arrival));
       }
     }
   }
   if (dist[n - 1] == INF) dist[n - 1] = -1;
   cout << dist[n - 1] << endl;
   return 0;
 }


#include <cstdio>
 #include <vector>
 #include <cstring>
 #include <algorithm>
 const int Maxn=200000;
 const int Inf=0x3f3f3f3f;
 int n;
 int a[Maxn+5];
 std::vector<int> v_lis[Maxn+5];
 struct Segment_Tree{
 	struct Segment_Node{
 		int minn,maxn;
 		int lazy;
 	}seg[Maxn<<2|5];
 	void update_tag(int root,int a){
 		seg[root].lazy+=a;
 		seg[root].minn+=a,seg[root].maxn+=a;
 	}
 	void push_up(int root){
 		seg[root].minn=std::min(seg[root<<1].minn,seg[root<<1|1].minn);
 		seg[root].maxn=std::max(seg[root<<1].maxn,seg[root<<1|1].maxn);
 	}
 	void push_down(int root){
 		if(seg[root].lazy==0){
 			return;
 		}
 		update_tag(root<<1,seg[root].lazy);
 		update_tag(root<<1|1,seg[root].lazy);
 		seg[root].lazy=0;
 	}
 	void update(int l,int r,int v,int root=1,int left=0,int right=n){
 		if(l>right||r<left){
 			return;
 		}
 		if(l<=left&&r>=right){
 			update_tag(root,v);
 			return;
 		}
 		push_down(root);
 		int mid=(left+right)>>1;
 		update(l,r,v,root<<1,left,mid);
 		update(l,r,v,root<<1|1,mid+1,right);
 		push_up(root);
 	}
 	int query_min(int l,int r,int root=1,int left=0,int right=n){
 		if(l>right||r<left){
 			return Inf;
 		}
 		if(l<=left&&r>=right){
 			return seg[root].minn;
 		}
 		push_down(root);
 		int mid=(left+right)>>1;
 		return std::min(query_min(l,r,root<<1,left,mid),query_min(l,r,root<<1|1,mid+1,right));
 	}
 	int query_max(int l,int r,int root=1,int left=0,int right=n){
 		if(l>right||r<left){
 			return -Inf;
 		}
 		if(l<=left&&r>=right){
 			return seg[root].maxn;
 		}
 		push_down(root);
 		int mid=(left+right)>>1;
 		return std::max(query_max(l,r,root<<1,left,mid),query_max(l,r,root<<1|1,mid+1,right));
 	}
 }seg_1,seg_2;
 int ans[Maxn+5];
 int main(){
 	scanf( %d ,&n);
 	for(int i=1;i<=n;i++){
 		scanf( %d ,&a[i]);
 		v_lis[a[i]].push_back(i);
 	}
 	for(int i=1;i<=n;i++){
 		seg_1.update(i>>1,(n-1)>>1,1);
 		seg_2.update((i+1)>>1,n>>1,1);
 	}
 	for(int i=1;i<=n;i++){
 		for(int j=0;j<(int)v_lis[i].size();j++){
 			int pos=v_lis[i][j];
 			int pos_1_max=seg_1.query_max(pos>>1,(n-1)>>1);
 			int pos_1_min=seg_1.query_min(0,(pos>>1)-1);
 			int pos_2_max=seg_2.query_max((pos+1)>>1,n>>1);
 			int pos_2_min=seg_2.query_min(0,(pos-1)>>1);
 			ans[pos]=std::max(std::max((pos_2_max-pos_2_min)>>1,(pos_1_max-pos_1_min)>>1),\n 			std::max((pos_2_max-pos_1_min-1)>>1,(pos_1_max-pos_2_min-1)>>1));
 		}
 		for(int j=0;j<(int)v_lis[i].size();j++){
 			seg_1.update(v_lis[i][j]>>1,(n-1)>>1,-2);
 			seg_2.update((v_lis[i][j]+1)>>1,n>>1,-2);
 		}
 		for(int j=0;j<(int)v_lis[i].size();j++){
 			int pos=v_lis[i][j];
 			int pos_1_min=seg_1.query_min(pos>>1,(n-1)>>1);
 			int pos_1_max=seg_1.query_max(0,(pos>>1)-1);
 			int pos_2_min=seg_2.query_min((pos+1)>>1,n>>1);
 			int pos_2_max=seg_2.query_max(0,(pos-1)>>1);
 			ans[pos]=std::max(ans[pos],std::max(
 			std::max((pos_2_max-pos_2_min)>>1,(pos_1_max-pos_1_min)>>1)-1,\n 			std::max((pos_2_max-pos_1_min-1)>>1,(pos_1_max-pos_2_min-1)>>1)));
 		}
 	}
 	for(int i=1;i<=n;i++){
 		printf( %d  ,ans[i]);
 	}
 	puts(  );;
 	return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> V[100005];
 int main() {
   int N, M, K, ans = 1;
   scanf( %d%d%d , &N, &M, &K);
   for (int i = 1; i <= N; ++i) {
     int c;
     scanf( %d , &c);
     V[c].push_back(i);
   }
   for (int c = 1; c <= M; ++c) {
     int L = V[c].size(), rem = 0, st = 0;
     for (int j = 1; j < L; ++j) {
       rem += V[c][j] - V[c][j - 1] - 1;
       while (rem > K) {
         rem -= V[c][st + 1] - V[c][st] - 1;
         ++st;
       }
       ans = max(ans, j - st + 1);
     }
   }
   printf( %d
 , ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 using namespace std;
 int main() {
   string s;
   cin >> s;
   string h = s.substr(0, 2);
   int hour = atoi(h.c_str());
   string m = s.substr(3, 2);
   int minute = atoi(m.c_str());
   long long time = 60 * hour + minute;
   long long add;
   cin >> add;
   long long sum = time + add;
   long long n = sum % (24 * 60);
   cout << setfill('0') << setw(2) << right << (n / 60);
   cout <<  : ;
   cout << setfill('0') << setw(2) << right << (n % 60) << endl;
   ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int a[7];
 int b[7];
 int main() {
   string s;
   int k;
   cin >> s >> k;
   int kol = 0;
   for (int i = 0; i < s.length(); i++) {
     if (s[i] == '0') {
       kol++;
     }
   }
   if (kol < k) {
     cout << s.length() - 1;
   } else {
     kol = 0;
     int res = 0;
     for (int i = s.length() - 1; i >= 0; i--) {
       if (s[i] == '0') {
         kol++;
       } else {
         res++;
       }
       if (kol == k) {
         cout << res;
         return 0;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s1[5010], s2[5010], str[5010];
 int num[30];
 int len1, len2;
 int dfs(int k, int m) {
   if (!s1[k]) return m;
   for (int i = 0; i < 26; i++)
     if (num[i] && (m || (i >= s2[k] - 'a'))) {
       str[k] = i + 'a';
       num[i]--;
       if (dfs(k + 1, m || (i > s2[k] - 'a'))) return 1;
       num[i]++;
     }
   return 0;
 }
 int main() {
   while (EOF != scanf( %s %s , s1, s2)) {
     memset(num, 0, sizeof(num));
     len1 = strlen(s1), len2 = strlen(s2);
     for (int i = 0; i < len1; i++) num[s1[i] - 'a']++;
     if (!dfs(0, 0))
       printf( -1
 );
     else {
       printf( %s
 , str);
     }
   }
   return 0;
 }