#include <bits/stdc++.h>
 const long long INF = 1000 * 1000 * 1000 + 47;
 const long long LINF = INF * (long long)INF;
 using namespace std;
 const int mod = 1000 * 1000 * 1000 + 9;
 const int MAX = 100005;
 pair<int, int> pts[MAX];
 int n, k;
 struct node {
   int x, y, l, r;
   int dp0, dp1;
   node() { l = r = -1; }
   node(pair<int, int> pt) {
     l = r = -1;
     x = pt.first, y = pt.second;
     dp1 = 1;
     dp0 = 0;
   }
 };
 node treap[MAX];
 int getDp0(int u) {
   if (u == -1) return 0;
   return treap[u].dp0;
 }
 int getDp1(int u) {
   if (u == -1) return 0;
   return treap[u].dp1;
 }
 void upd(int root) {
   if (root == -1) return;
   treap[root].dp1 = 1 + getDp0(treap[root].l) + getDp0(treap[root].r);
   treap[root].dp0 = max(treap[root].dp1 - 1,
                         max(getDp0(treap[root].l) + getDp1(treap[root].r),
                             getDp1(treap[root].l) + getDp0(treap[root].r)));
 }
 int merge(int l, int r) {
   if (l == -1) return r;
   if (r == -1) return l;
   int root;
   if (treap[l].y > treap[r].y) {
     root = l;
     treap[l].r = merge(treap[l].r, r);
   } else {
     root = r;
     treap[r].l = merge(l, treap[r].l);
   }
   upd(root);
   return root;
 }
 pair<int, int> split(int root, int x) {
   if (root == -1) return {-1, -1};
   pair<int, int> res;
   if (treap[root].x <= x) {
     res = split(treap[root].r, x);
     treap[root].r = res.first;
     res = {root, res.second};
   } else {
     res = split(treap[root].l, x);
     treap[root].l = res.second;
     res = {res.first, root};
   }
   upd(res.first);
   upd(res.second);
   return res;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   int root = -1;
   cin >> n >> k;
   for (int i = 0; i < k; i++) {
     cin >> pts[i].first >> pts[i].second;
   }
   int a, b, c, d;
   cin >> a >> b >> c >> d;
   for (int i = k; i < n; i++) {
     pts[i].first = ((a * (long long)pts[i - 1].first) + b) % mod;
     pts[i].second = ((c * (long long)pts[i - 1].second) + d) % mod;
   }
   sort(pts, pts + n);
   for (int i = 0; i < n; i++) {
     treap[i] = node(pts[i]);
     root = merge(root, i);
   }
   int q;
   cin >> q;
   while (q--) {
     int l, r;
     cin >> l >> r;
     auto roots = split(root, l - 1);
     auto roots2 = split(roots.second, r);
     cout << ((roots2.first == -1) ? 0 : treap[roots2.first].dp0) << endl;
     root = merge(roots.first, merge(roots2.first, roots2.second));
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 100000 + 10;
 int main() {
   long long n;
   cin >> n;
   if (n % 2 == 0) {
     cout << 1ll * n / 2;
     return 0;
   }
   bool flag = true;
   for (int i = 3; i <= sqrt(n); i += 2) {
     if (n % i == 0) {
       cout << 1ll * (n - i) / 2 + 1;
       return 0;
     }
   }
   cout << '1';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 2005, maxm = 4000005;
 const double pi = 3.141592653589793;
 const int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
 const int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
 int s[maxn][maxn], v[maxn][maxn], f[maxn][maxn];
 int qx[maxm], qy[maxm];
 int n, cir, rec;
 double dis(double x, double y) { return sqrt(x * x + y * y); }
 int main() {
   int i, j, k, a, b, x, y, h, t, tx, ty;
   double sumx, sumy, maxd, mind;
   scanf('%d', &n);
   for (i = 1; i <= n; ++i)
     for (j = 1; j <= n; ++j) {
       scanf('%d', &x);
       if (x == 0) continue;
       for (a = -2; a <= 2; ++a)
         for (b = -2; b <= 2; ++b)
           if (i + a > 0 && i + a <= n && j + b > 0 && j + b <= n)
             ++s[i + a][j + b];
     }
   for (i = 1; i <= n; ++i)
     for (j = 1; j <= n; ++j) {
       s[i][j] = s[i][j] > 15 ? 1 : 0;
       v[i][j] = s[i][j];
     }
   for (i = 1; i <= n; ++i)
     for (j = 1; j <= n; ++j) {
       if (s[i][j] == 0) continue;
       sumx = 0;
       sumy = 0;
       h = 1;
       t = 1;
       qx[h] = i;
       qy[h] = j;
       for (; h <= t; ++h) {
         a = qx[h];
         b = qy[h];
         sumx += a;
         sumy += b;
         for (k = 0; k < 8; ++k) {
           x = a + dx[k];
           y = b + dy[k];
           if (v[x][y] == 0) ++f[a][b];
           if (s[x][y] == 1) {
             ++t;
             qx[t] = x;
             qy[t] = y;
             s[x][y] = 0;
           }
         }
       }
       if (t < 125) continue;
       tx = round(sumx / t);
       ty = round(sumy / t);
       maxd = 0;
       for (k = 1; k <= t; ++k) maxd = max(maxd, dis(tx - qx[k], ty - qy[k]));
       if (t < 2.5 * maxd * maxd)
         ++rec;
       else
         ++cir;
     }
   printf('%d %d
', cir, rec);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1000000;
 int T, tot, prod, mn[1000010];
 bool solved[10000][11];
 bitset<1000> b[10000][11];
 vector<int> V, prime;
 unordered_map<int, int> id;
 int main() {
   scanf('%d', &T);
   for (int i = 2; i <= N; i++)
     if (!mn[i]) {
       if (prime.size() < 1000) prime.push_back(i);
       for (int j = i; j <= N; j += i) mn[j] = i;
     }
   auto get = [&](long long x) {
     V.clear();
     int lst = 50;
     while (x > 1) {
       int c = 0, t = x <= N ? mn[x] : 0;
       if (!t) {
         for (int i = lst; ~i; i--) {
           if (!(x % prime[i])) {
             t = prime[i], lst = i;
             break;
           }
         }
       }
       while (!(x % t)) x /= t, c++;
       V.push_back(c);
     }
     sort(V.begin(), V.end(), greater<int>());
     int z = prod = 1;
     for (int i = 0; i < V.size(); i++) {
       prod *= V[i] + 1;
       for (int j = 0; j < V[i]; j++) z *= prime[i];
     }
     return z;
   };
   for (int i = 1; i <= N; i++) {
     if (i == get(i)) id[i] = ++tot;
   }
   function<void(int, int)> solve = [&](int x, int step) {
     int y = get(x);
     if (prod >= 1000) return;
     if (!id.count(x)) id[x] = ++tot;
     int $ = id[x];
     if (solved[$][step]) return;
     solved[$][step] = 1;
     if (!step) {
       b[$][step][prod] = 1;
       return;
     }
     vector<int> V2 = V;
     for (int i = 0; i < V2.size(); i++) {
       int v = get(x / prime[i]);
       solve(v, step - 1), b[$][step] |= b[id[v]][step - 1];
       v = get(1LL * x * prime[i]);
       if (v <= N * 100) solve(v, step - 1), b[$][step] |= b[id[v]][step - 1];
     }
     int v = get(1LL * x * prime[V2.size()]);
     if (v <= N * 100) solve(v, step - 1), b[$][step] |= b[id[v]][step - 1];
   };
   for (int i = 1; i <= N; i++)
     if (id[i]) {
       for (int j = 0; j <= 10; j++) solve(i, j);
     }
   while (T--) {
     int x, y, ans = INT_MAX;
     scanf('%d %d', &x, &y);
     x = get(x), y = get(y);
     for (int i = 0; i <= 10; i++) {
       for (int j = 0; j <= 10; j++)
         if (i + j < ans) {
           if ((b[id[x]][i] & b[id[y]][j]).any()) ans = min(ans, i + j);
         }
     }
     assert(ans <= 10);
     printf('%d
', ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long mod = 1e9 + 7;
 const long long inf = 0x3f3f3f3f;
 const long long llinf = 1e18;
 const long long nax = 0;
 int n, m;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie();
   cout.tie();
   cin >> n >> m;
   vector<int> a(n + 5), b(m + 5);
   int maks1 = 0, mins1 = inf, mins2 = inf;
   for (int i = 0; i < n; ++i) {
     cin >> a[i];
     maks1 = max(maks1, a[i]);
     mins1 = min(mins1, a[i]);
   }
   for (int i = 0; i < m; ++i) {
     cin >> b[i];
     mins2 = min(mins2, b[i]);
   }
   if (max(mins1 * 2, maks1) < mins2) {
     cout << max(mins1 * 2, maks1) << '
';
   } else
     cout << '-1' << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int a[3];
 int main() {
   cin >> a[0] >> a[1] >> a[2];
   sort(a + 0, a + 3);
   int res = 0;
   while ((a[0] + a[1]) <= a[2]) {
     a[0]++;
     res++;
   }
   cout << res;
 }


#include<bits/stdc++.h>
 #include<algorithm>
 using namespace std;

 #define ll long long
 #define ld long double

 #define rep(i,a,b) for(ll i=a;i<b;i++)
 #define repb(i,a,b) for(ll i=a;i>=b;i--)

 #define err() cout<<'=================================='<<endl;
 #define errA(A) for(auto i:A)   cout<<i<<' ';cout<<endl;
 #define err1(a) cout<<#a<<' '<<a<<endl
 #define err2(a,b) cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<endl
 #define err3(a,b,c) cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<' '<<#c<<' '<<c<<endl
 #define err4(a,b,c,d) cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<' '<<#c<<' '<<c<<' '<<#d<<' '<<d<<endl

 #define pb push_back
 #define all(A)  A.begin(),A.end()
 #define allr(A)    A.rbegin(),A.rend()
 #define ft first
 #define sd second

 #define pll pair<ll,ll>
 #define V vector<ll>
 #define S set<ll>
 #define VV vector<V>
 #define Vpll vector<pll>
 #define VVpll vector<Vpll>

 #define endl '
'

 const ll logN = 20;
 const ll N=100005;
 const ll M = 1000000007;
 const ll M2=998244353 ;
 const ll INF = 1e12;
 #define PI 3.14159265
 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
 ll pow1(ll a,ll b){
     ll res=1;
     while(b>0){
         if(b&1){
         	res=(res*a)%M;
 		}
         a=(a*a)%M;
         b>>=1;
     }
     return res%M;
 }

 map<ll,ll>mp;
 V a;
 ll n;
 void recur(ll depth,ll ind1=0, ll ind2=n-1 )
 {
     if(ind1>ind2)return;
     ll mx_ind;
     ll mx=LLONG_MIN;
     rep(i,ind1,ind2+1)
     {
         if(a[i]>mx)
         {
             mx=a[i];
             mx_ind=i;
         }
     }
     mp[mx_ind]=depth;
     recur(depth+1,ind1,mx_ind-1);
     recur(depth+1,mx_ind+1,ind2);
     return;
 }

 int main()
 {
     fast;

     #ifndef ONLINE_JUDGE
     freopen('../input.txt', 'r', stdin);
     freopen('../output.txt', 'w', stdout);
     #endif

     ll T=1;
     cin>>T;
     while(T--)
     {
         cin>>n;
         a=V(n);
         rep(i,0,n)cin>>a[i];
         recur(0);
         for(auto it:mp)
         cout<<it.sd<<' ';
         cout<<endl;
         mp.clear();
     }

 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 void _R(T &x) {
   cin >> x;
 }
 void _R(int &x) { scanf('%d', &x); }
 void _R(long long &x) { scanf('%lld', &x); }
 void _R(double &x) { scanf('%lf', &x); }
 void _R(char &x) { scanf(' %c', &x); }
 void _R(char *x) { scanf('%s', x); }
 void R() {}
 template <class T, class... U>
 void R(T &head, U &...tail) {
   _R(head);
   R(tail...);
 }
 template <class T>
 void _W(const T &x) {
   cout << x;
 }
 void _W(const int &x) { printf('%d', x); }
 void _W(const long long &x) { printf('%lld', x); }
 void _W(const double &x) { printf('%.16f', x); }
 void _W(const char &x) { putchar(x); }
 void _W(const char *x) { printf('%s', x); }
 template <class T, class U>
 void _W(const pair<T, U> &x) {
   _W(x.first);
   putchar(' ');
   _W(x.second);
 }
 template <class T>
 void _W(const vector<T> &x) {
   for (auto i = x.begin(); i != x.end(); _W(*i++))
     if (i != x.cbegin()) putchar(' ');
 }
 void W() {}
 template <class T, class... U>
 void W(const T &head, const U &...tail) {
   _W(head);
   putchar(sizeof...(tail) ? ' ' : '
');
   W(tail...);
 }
 int MOD = 1e9 + 7;
 void ADD(long long &x, long long v) {
   x = (x + v) % MOD;
   if (x < 0) x += MOD;
 }
 const int SIZE = 1e6 + 10;
 int cnt[101][26];
 char an[101];
 pair<pair<int, int>, pair<char, char>> e[10000];
 int en;
 void query(int l, int r) {
   W('?', l, r);
   fflush(stdout);
   int num = (r - l + 1);
   memset((cnt), 0, sizeof((cnt)));
   for (int i = (1); i <= (num * (num + 1) / 2); ++i) {
     string s;
     R(s);
     if (((int)(s).size()) <= (num + 1) / 2) {
       for (char c : s) {
         cnt[((int)(s).size())][c - 'a']++;
       }
     }
   }
   int m = (num + 1) / 2;
   for (int i = m - 1; i >= 1; i--) {
     for (int j = 0; j < (26); ++j) cnt[i + 1][j] -= cnt[i][j];
   }
   for (int i = 1; i < m; i++) {
     for (int j = 0; j < (26); ++j) cnt[i][j] -= cnt[i + 1][j];
   }
   string ret;
   for (int i = (1); i <= (m); ++i) {
     for (int j = 0; j < (26); ++j) {
       if (cnt[i][j]) {
         cnt[i][j]--;
         ret += 'a' + j;
         break;
       }
     }
   }
   for (int i = m; i >= 1; i--) {
     for (int j = 0; j < (26); ++j) {
       if (cnt[i][j]) {
         cnt[i][j]--;
         ret += 'a' + j;
         break;
       }
     }
   }
   int ll = 0, rr = num - 1;
   while (ll <= rr) {
     if (ll == rr) {
       an[l] = ret[ll];
     } else {
       e[en++] = make_pair(make_pair(l, r), make_pair(ret[ll], ret[rr]));
     }
     ll++;
     rr--;
     l++;
     r--;
   }
 }
 int main() {
   int n;
   R(n);
   if (n == 1) {
     query(1, 1);
   } else if (n % 2 == 0) {
     query(1, n - 1);
     query(n / 2, n - 1);
     query(n / 2, n);
   } else {
     query(1, n);
     query(1, n / 2);
     query(1, n / 2 + 1);
   }
   for (int i = 0; i < (1000); ++i) {
     for (int j = 0; j < (en); ++j) {
       if (an[e[j].first.first]) {
         an[e[j].first.second] = (int)e[j].second.second +
                                 (int)e[j].second.first - an[e[j].first.first];
       }
       if (an[e[j].first.second]) {
         an[e[j].first.first] = (int)e[j].second.second +
                                (int)e[j].second.first - an[e[j].first.second];
       }
     }
   }
   W('!', string(an + 1));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 void MACRO_VAR_Scan(T& t) {
   cin >> t;
 }
 template <typename First, typename... Rest>
 void MACRO_VAR_Scan(First& first, Rest&... rest) {
   cin >> first;
   MACRO_VAR_Scan(rest...);
 }
 template <typename T>
 void MACRO_VEC_ROW_Init(int n, T& t) {
   t.resize(n);
 }
 template <typename First, typename... Rest>
 void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {
   first.resize(n);
   MACRO_VEC_ROW_Init(n, rest...);
 }
 template <typename T>
 void MACRO_VEC_ROW_Scan(int p, T& t) {
   cin >> t[p];
 }
 template <typename First, typename... Rest>
 void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {
   cin >> first[p];
   MACRO_VEC_ROW_Scan(p, rest...);
 }
 void _main();
 signed main() {
   cin.tie(0);
   ios::sync_with_stdio(false);
   _main();
 }
 inline long long toInt(std::string s) {
   long long v;
   std::istringstream sin(s);
   sin >> v;
   return v;
 }
 template <class T>
 inline string toString(T x) {
   ostringstream sout;
   sout << x;
   return sout.str();
 }
 template <class T>
 inline T sqr(T x) {
   return x * x;
 }
 template <typename A, typename B>
 inline void chmin(A& a, B b) {
   if (a > b) a = b;
 }
 template <typename A, typename B>
 inline void chmax(A& a, B b) {
   if (a < b) a = b;
 }
 long long qp(long long a, long long b) {
   long long ans = 1ll;
   do {
     if (b & 1) ans = 1ll * ans * a;
     a = 1ll * a * a;
   } while (b >>= 1);
   return ans;
 }
 long long qpmod(long long a, long long b, long long mo) {
   long long ans = 1ll;
   do {
     if (b & 1) ans = 1ll * ans * a % mo;
     a = 1ll * a * a % mo;
   } while (b >>= 1);
   return ans;
 }
 inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
 inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
 inline bool valid(int x, int h) { return 0 <= x && x < h; }
 const double EPS = 1e-8;
 const double PI = acos(-1.0);
 const int INF = (int)(1e9) + 7;
 const long long MOD = (long long)(1e9) + 7;
 const long long MOD2 = (long long)(1e18) + 9;
 const long long INF2 = (long long)(1e18);
 const long long INTMAX = (0x7FFFFFFFL);
 const long long LLMAX = (0x7FFFFFFFFFFFFFFFL);
 const int N4 = (int)1e4 + 10;
 const int N5 = (int)1e5 + 10;
 const int N9 = (int)1e9;
 int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
 int dy[8] = {0, 1, 0, -1, -1, -1, 1, 1};
 struct uf_tree {
   std::vector<int> parent;
   int __size;
   uf_tree(int size_) : parent(size_, -1), __size(size_) {}
   void unite(int x, int y) {
     if ((x = find(x)) != (y = find(y))) {
       if (parent[y] < parent[x]) std::swap(x, y);
       parent[x] += parent[y];
       parent[y] = x;
       __size--;
     }
   }
   bool is_same(int x, int y) { return find(x) == find(y); }
   int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
   int size(int x) { return -parent[find(x)]; }
   int size() { return __size; }
 };
 long long digitsum(long long x) {
   long long dig;
   long long summ = 0;
   while (x) {
     dig = x % 10;
     summ = summ + dig;
     x = x / 10;
   }
   return summ;
 }
 long long ret, cnt, cur, ans, f;
 void an(long long x, long long y) { cout << x << ' ' << y << endl; }
 void _main() {
   long long n, m, k;
   MACRO_VAR_Scan(n, m, k);
   ;
   if (k < n) {
     an(k + 1, 1);
     return;
   }
   long long ok = 0, ng = n / 2;
   while (ng - ok > 1) {
     long long mi = (ng + ok) / 2;
     if (n + mi * 2 * (m - 1) <= k)
       ok = mi;
     else
       ng = mi;
   }
   k -= n - 1 + ok * 2 * (m - 1);
   int x = n - ok * 2, y = 2;
   if (k <= m - 1) {
     an(x, k + 1);
     return;
   } else {
     an(x - 1, m - (k - (m - 1)) + 1);
     return;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MX = 300300;
 int t, n, m, i, k[MX], c[MX];
 long long r;
 int main() {
   cin >> t;
   while (t--) {
     int n, x, sum = 0, ctr = 0;
     cin >> n >> x;
     int arr[n];
     for (int i = 0; i < n; i++) {
       cin >> arr[i];
       sum += arr[i];
       ctr += (arr[i] == x);
     }
     if (ctr == n)
       cout << 0 << '
';
     else if (ctr >= 1 || sum == n * x)
       cout << 1 << '
';
     else
       cout << 2 << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int a, b;
   scanf('%d%d', &a, &b);
   int c = 0;
   while (b) {
     c *= 10;
     c += b % 10;
     b /= 10;
   }
   printf('%d
', c + a);
 }


#include <bits/stdc++.h>
 using namespace std;
 inline int read() {
   int x = 0, fu = 0;
   char ch = getchar();
   for (; ch < 48 || ch > 57; ch = getchar()) fu |= (ch == '-');
   for (; ch > 47 && ch < 58; ch = getchar()) x = x * 10 + ch - '0';
   return fu ? -x : x;
 }
 int n, mx = -1000000000, mn = 1000000000, ans = 1, tmp, no;
 set<int> s;
 char S[20];
 int main() {
   for (n = read(), s.insert(mx), s.insert(mn); n--;) {
     scanf('%s', S), tmp = read();
     if (S[1] == 'D')
       s.insert(tmp), no += (tmp > mx && tmp < mn);
     else {
       if (tmp < mx || tmp > mn) return puts('0'), 0;
       ans = (tmp > mx && tmp < mn) ? (ans << 1) % 1000000007 : ans;
       s.erase(tmp), no = 0;
       set<int>::iterator it = s.lower_bound(tmp);
       mn = *it, --it, mx = *it;
     }
   }
   printf('%d
', (int)(1LL * ans * (no + 1) % 1000000007));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   char a[111][111];
   for (long long i = 1; i <= 3; i++)
     for (long long j = 1; j <= 3; j++) cin >> a[i][j];
   if (a[1][1] == a[3][3] && a[3][2] == a[1][2] && a[3][1] == a[1][3] &&
       a[2][3] == a[2][1])
     cout << 'YES';
   else
     cout << 'NO';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int tree[500000];
 int a[100000], pos[100001];
 void update(int node, int l, int r, int pos, int u) {
   if (l == r) {
     tree[node] += u;
     return;
   }
   int mid = (l + r) / 2;
   if (pos <= mid)
     update(2 * node, l, mid, pos, u);
   else
     update(2 * node + 1, mid + 1, r, pos, u);
   tree[node] = tree[2 * node] + tree[2 * node + 1];
 }
 int query(int node, int l, int r, int ql, int qr) {
   if (ql > r || qr < l) return 0;
   if (ql <= l && qr >= r) return tree[node];
   int mid = (l + r) / 2;
   return query(2 * node, l, mid, ql, qr) +
          query(2 * node + 1, mid + 1, r, ql, qr);
 }
 int main() {
   int i, j, k;
   int n, res = 0;
   scanf('%d', &n);
   for (i = 0; i < n; i++) scanf('%d', &a[i]);
   for (i = 0; i < n; i++) {
     scanf('%d', &j);
     pos[j] = i;
   }
   for (i = 0; i < n; i++) {
     if (query(1, 0, n, pos[a[i]] + 1, n)) res++;
     update(1, 0, n, pos[a[i]], 1);
   }
   printf('%d', res);
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m;
 int ans;
 int a[400050];
 int back[400050];
 vector<int> edge[400050];
 int main() {
   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
   cin >> n >> m;
   for (int i = 1; i <= n; i++) cin >> a[i];
   for (int i = 1, u, v; i <= m; i++) {
     cin >> u >> v;
     edge[v].push_back(u);
   }
   for (auto iter : edge[a[n]]) back[iter]++;
   for (int i = n - 1; i >= 1; i--) {
     if (back[a[i]] == n - i - ans)
       ans++;
     else {
       for (auto iter : edge[a[i]]) back[iter]++;
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 pair<int, int> p;
 void sort() {
   int temp = 0;
   if (p.first < p.second) {
     temp = p.first;
     p.first = p.second;
     p.second = temp;
   }
 }
 int main() {
   int n;
   cin >> n;
   for (int i = 0; i < n; i++) {
     int op = 0;
     cin >> p.first >> p.second;
     if (p.first == p.second)
       op = 1;
     else {
       if (p.first % p.second == 0)
         op = p.first / p.second;
       else {
         while (p.first % p.second != 0) {
           sort();
           op += p.first / p.second;
           p.first = p.first % p.second;
         }
       }
     }
     cout << op << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, a, b, finish1 = INFINITY, finish2 = INFINITY, start1 = 0,
                      start2 = 0, mx = 0;
   scanf('%lld', &n);
   for (int i = 1; i <= n; i++) {
     scanf('%lld %lld', &a, &b);
     start2 = max(start2, a);
     finish1 = min(finish1, b);
   }
   scanf('%lld', &n);
   for (int i = 1; i <= n; i++) {
     scanf('%lld %lld', &a, &b);
     start1 = max(start1, a);
     finish2 = min(finish2, b);
   }
   mx = max(mx, start1 - finish1);
   mx = max(mx, start2 - finish2);
   if (mx <= 0)
     printf('0
');
   else
     printf('%lld
', mx);
   return 0;
 }


#include <bits/stdc++.h>
 int x[100001] = {0};
 int main() {
   int i, n;
   scanf('%d', &n);
   for (int i = 1; i <= n; ++i) {
     scanf('%d', &x[i]);
   }
   int sum = 0;
   int cn = 0;
   for (i = 0; i < n; i++) {
     sum = sum + x[i] - x[i + 1];
     if (sum < 0) {
       do {
         cn++;
         sum++;
       } while (sum < 0);
     }
   }
   printf('%d', cn);
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int n, m;
   scanf('%d%d', &n, &m);
   int a[n], b[m], i, j;
   for (i = 0; i < n; i++) scanf('%d', &a[i]);
   for (i = 0; i < m; i++) scanf('%d', &b[i]);
   for (i = 0; i < n; i++) {
     for (j = 0; j < m; j++) {
       if (b[j] == a[i]) printf('%d ', a[i]);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 const ll mod = 1e9 + 7;
 const int maxn = 1e5 + 5, maxa = 71, lim = 1 << 19;
 ll ev[maxn], od[maxn], dp[2][lim];
 int n, a, f[lim], comp[maxa];
 vector<int> p;
 int calc_msk(int x) {
   int msk = 0, b = 1;
   for (int y : p) {
     while (x % (y * y) == 0) x /= y * y;
     msk = (x % y == 0) * b + msk;
     b *= 2;
   }
   return msk;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   for (int i = (2); i < (maxa); i++)
     for (int j = i + i; j < maxa; j += i) comp[j] = 1;
   for (int i = (2); i < (maxa); i++)
     if (!comp[i]) p.push_back(i);
   ev[0] = ev[1] = 1;
   for (int i = (2); i < (maxn); i++) ev[i] = ev[i - 1] * 2 % mod;
   od[0] = 0;
   od[1] = 1;
   for (int i = (2); i < (maxn); i++) od[i] = od[i - 1] * 2 % mod;
   cin >> n;
   for (int i = (0); i < (n); i++) {
     cin >> a;
     f[calc_msk(a)]++;
   }
   dp[0][0] = 1;
   for (int i = (1); i < (maxa); i++) {
     int x = calc_msk(i);
     for (int msk = (0); msk < (lim); msk++)
       dp[i & 1][msk] =
           (dp[i & 1 ^ 1][msk] * ev[f[x]] + dp[i & 1 ^ 1][msk ^ x] * od[f[x]]) %
           mod;
     f[x] = 0;
   }
   cout << (dp[maxa & 1 ^ 1][0] + mod - 1) % mod << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 const int M = 100000 + 10;
 int leaf[M];
 vector<int> adj[M];
 int childnum[M];
 void dfs(int cur, int par) {
   for (int i = 0; i < adj[cur].size(); i++) {
     int nxt = adj[cur][i];
     if (nxt != par) {
       childnum[cur]++;
       dfs(nxt, cur);
     }
   }
 }
 bool ok = true;
 long long int coef[M];
 long long int thres = 1e16 + 10;
 void dfs1(int cur, int par) {
   coef[cur] = childnum[par] * coef[par];
   if (coef[cur] > thres) {
     ok = false;
     return;
   }
   for (int i = 0; i < adj[cur].size(); i++) {
     int nxt = adj[cur][i];
     if (nxt != par) {
       dfs1(nxt, cur);
     }
   }
 }
 vector<int> leafnode;
 long long int gcd(long long int x, long long int y) {
   return y == 0 ? x : gcd(y, x % y);
 }
 int main() {
   int n;
   cin >> n;
   long long int s = 0;
   for (int i = 1; i <= n; i++) {
     cin >> leaf[i];
     s = s + leaf[i];
   }
   for (int i = 0; i < n - 1; i++) {
     int u, v;
     cin >> u >> v;
     adj[u].push_back(v);
     adj[v].push_back(u);
   }
   for (int i = 2; i <= n; i++) {
     if (adj[i].size() == 1) {
       leafnode.push_back(i);
     }
   }
   dfs(1, 0);
   coef[0] = 1;
   childnum[0] = 1;
   dfs1(1, 0);
   long long int minv = -1;
   for (int i = 0; i < leafnode.size(); i++) {
     int curleaf = leafnode[i];
     long long int x = leaf[curleaf];
     long long int k = coef[curleaf];
     long long int v = k * x;
     if (minv == -1) {
       minv = v;
     } else {
       minv = min(minv, v);
     }
   }
   if (!ok) {
     cout << s << endl;
   } else {
     long long int lcm = 1;
     for (int i = 0; i < leafnode.size(); i++) {
       int curleaf = leafnode[i];
       long long int k = coef[curleaf];
       long long int d = gcd(lcm, k);
       lcm = lcm * k / d;
       if (lcm > minv) {
         ok = false;
         break;
       }
     }
     if (!ok) {
       cout << s << endl;
     } else {
       lcm = minv / lcm * lcm;
       long long int ans = 0;
       for (int i = 0; i < leafnode.size(); i++) {
         int curleaf = leafnode[i];
         long long int k = coef[curleaf];
         long long int x = lcm / k;
         long long int val = leaf[curleaf] - x;
         ans = ans + val;
       }
       cout << ans << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int m, n;
   cin >> m >> n;
   cout << ((m % 2) ? m * (n / 2) + (n % 2) * (m / 2)
                    : n * (m / 2) + (m % 2) * (n / 2));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n;
 long long a[300000];
 int main() {
   while (cin >> n) {
     for (int i = 0; i < n; i++) cin >> a[i];
     sort(a, a + n);
     long long ans = 0;
     for (int i = 0; i < n; i++) {
       int x = (i == n - 1 ? -1 : 0);
       ans += (x + i + 2) * a[i];
     }
     cout << ans << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool check(const string &str) {
   vector<string> vec{'ABSINTH', 'BEER',    'BRANDY', 'CHAMPAGNE', 'GIN', 'RUM',
                      'SAKE',    'TEQUILA', 'VODKA',  'WHISKEY',   'WINE'};
   for (int i = 0; i < (int)vec.size(); ++i) {
     if (str == vec[i]) return true;
   }
   return false;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   int n;
   cin >> n;
   int cnt = 0;
   for (int i = 0; i < n; ++i) {
     string str;
     cin >> str;
     if (isdigit(str[0])) {
       int num = stoi(str);
       if (num < 18) ++cnt;
     } else {
       if (check(str)) ++cnt;
     }
   }
   cout << cnt << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     int n, m;
     cin >> n >> m;
     vector<vector<int> > a(n), b(m);
     for (int i = 0; i < n; i++) {
       a[i].resize(m);
       for (int j = 0; j < m; j++) cin >> a[i][j];
     }
     for (int j = 0; j < m; j++) {
       b[j].resize(n);
       for (int i = 0; i < n; i++) cin >> b[j][i];
     }
     for (int i = 0; i < n; i++) {
       for (int j = 0; j < m; j++) {
         if (a[i][0] == b[j][0]) {
           swap(a[0], a[i]);
           swap(b[0], b[j]);
           break;
         }
       }
     }
     for (int j = 0; j < n; j++) {
       for (int i = 0; i < n; i++) {
         if (a[i][0] == b[0][j]) {
           for (auto x : a[i]) cout << x << ' ';
           cout << '
';
           break;
         }
       }
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int yy[100005], id;
 int n, m;
 int st[100005 * 4], lz[100005 * 4];
 int a[100005];
 int l[305], r[305];
 inline void pu(int rt) { st[rt] = max(st[2 * rt + 1], st[2 * rt + 2]); }
 void pd(int rt, int l, int r, int m) {
   if (lz[rt]) {
     int t = lz[rt];
     lz[2 * rt + 1] += t;
     lz[2 * rt + 2] += t;
     st[2 * rt + 1] += t;
     st[2 * rt + 2] += t;
     lz[rt] = 0;
   }
 }
 void build(int rt, int l, int r) {
   lz[rt] = 0;
   if (l == r) {
     st[rt] = a[l];
     return;
   }
   int mid = (l + r) / 2;
   build(2 * rt + 1, l, mid);
   build(2 * rt + 2, mid + 1, r);
   pu(rt);
 }
 void ud(int rt, int l, int r, int ul, int ur, int dt) {
   if (l > ur || r < ul) return;
   if (ul <= l && r <= ur) {
     lz[rt] += dt;
     st[rt] += dt;
     return;
   }
   int mid = (l + r) / 2;
   pd(rt, l, r, mid);
   ud(2 * rt + 1, l, mid, ul, ur, dt), ud(2 * rt + 2, mid + 1, r, ul, ur, dt);
   pu(rt);
 }
 int query(int rt, int l, int r, int ql, int qr) {
   if (l > qr || r < ql) return -(0x3f3f3f3f);
   if (ql <= l && r <= qr) return st[rt];
   int mid = (l + r) / 2;
   pd(rt, l, r, mid);
   return max(query(2 * rt + 1, l, mid, ql, qr),
              query(2 * rt + 2, mid + 1, r, ql, qr));
 }
 void init() {}
 int main() {
   cin >> n >> m;
   int ans = -(0x3f3f3f3f), p = -1;
   int mmx = -(0x3f3f3f3f), mmn = (0x3f3f3f3f);
   for (int i = (1); i <= (n); i++) {
     scanf('%d', a + i);
     mmx = max(a[i], mmx), mmn = min(a[i], mmn);
   }
   if (m == 0) {
     printf('%d
0', mmx - mmn);
     return 0;
   }
   for (int i = (0); i < (m); i++) scanf('%d%d', l + i, r + i);
   build(0, 1, n);
   for (int i = (1); i <= (n); i++) {
     for (int j = (0); j < (m); j++) {
       if (l[j] == i) {
         ud(0, 1, n, l[j], r[j], -1);
       } else if (i - 1 == r[j]) {
         ud(0, 1, n, l[j], r[j], 1);
       }
     }
     int mx = query(0, 1, n, 1, n);
     int nowmin = query(0, 1, n, i, i);
     if (mx - nowmin > ans) {
       ans = mx - nowmin;
       p = i;
     }
   }
   for (int i = (0); i < (m); i++) {
     if (p >= l[i] && p <= r[i]) {
       yy[id++] = i + 1;
     }
   }
   printf('%d
%d
', ans, id);
   for (int i = (0); i < (id); i++) printf('%d ', yy[i]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 10;
 const int INF = 2147000000;
 int n, k;
 int a[maxn][maxn];
 int b[maxn], vis[maxn];
 int ans = INF;
 int make(int num) {
   int res = 0;
   for (int i = 1; i <= k; i++) {
     int c = b[i];
     res = res * 10 + a[num][c];
   }
   return res;
 }
 void solve() {
   int Max = -INF, Min = INF;
   for (int i = 1; i <= n; i++) {
     int res = make(i);
     Max = max(Max, res);
     Min = min(Min, res);
   }
   ans = min(ans, Max - Min);
 }
 void dfs(int cur) {
   if (cur == k + 1) {
     solve();
     return;
   }
   for (int i = 1; i <= k; i++) {
     if (!vis[i]) {
       vis[i] = 1;
       b[cur] = i;
       dfs(cur + 1);
       vis[i] = 0;
     }
   }
 }
 int main() {
   scanf('%d%d', &n, &k);
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= k; j++) {
       char c;
       scanf(' %c', &c);
       a[i][j] = c - '0';
     }
   }
   memset(vis, 0, sizeof(vis));
   dfs(1);
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int m, n, arr[2000][2000], temp[2000][2000];
 char ctrl(int i) {
   for (int j = 1; j <= n; j++)
     if (arr[i][j] and arr[i][j] != temp[i][j]) return 0;
   return 1;
 }
 char solve(int a, int b, int c, int d, int ttt = 0) {
   if (ttt) ttt = 3;
   for (int i = 1; i <= m; i++) {
     if (i & 1) {
       for (int j = 1; j <= n; j++)
         if (j & 1)
           temp[i][j] = a;
         else
           temp[i][j] = b;
       if (ctrl(i)) continue;
       for (int j = 1; j <= n; j++)
         if (j & 1)
           temp[i][j] = b;
         else
           temp[i][j] = a;
       if (ctrl(i)) continue;
       return 0;
     } else {
       for (int j = 1; j <= n; j++)
         if (j & 1)
           temp[i][j] = c;
         else
           temp[i][j] = d;
       if (ctrl(i)) continue;
       for (int j = 1; j <= n; j++)
         if (j & 1)
           temp[i][j] = d;
         else
           temp[i][j] = c;
       if (ctrl(i)) continue;
       return 0;
     }
   }
   while (ttt--) {
     for (int i = 1; i <= m; i++)
       for (int j = 1; j <= n; j++) arr[j][m - i + 1] = temp[i][j];
     swap(m, n);
     for (int i = 1; i <= m; i++)
       for (int j = 1; j <= n; j++) temp[i][j] = arr[i][j];
   }
   for (int i = 1; i <= m; i++) {
     for (int j = 1; j <= n; j++) cout << temp[i][j];
     cout << endl;
   }
   return 1;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin >> m >> n;
   char c;
   for (int i = 1; i <= m; i++)
     for (int j = 1; j <= n; j++) {
       cin >> c;
       arr[i][j] = c - '0';
     }
   if (solve(1, 2, 3, 4)) return 0;
   if (solve(1, 3, 2, 4)) return 0;
   if (solve(1, 4, 2, 3)) return 0;
   if (solve(2, 3, 1, 4)) return 0;
   if (solve(2, 4, 1, 3)) return 0;
   if (solve(3, 4, 1, 2)) return 0;
   for (int i = 1; i <= m; i++)
     for (int j = 1; j <= n; j++) temp[j][m - i + 1] = arr[i][j];
   swap(m, n);
   for (int i = 1; i <= m; i++)
     for (int j = 1; j <= n; j++) arr[i][j] = temp[i][j];
   if (solve(1, 2, 3, 4, 1)) return 0;
   if (solve(1, 3, 2, 4, 1)) return 0;
   if (solve(1, 4, 2, 3, 1)) return 0;
   if (solve(2, 3, 1, 4, 1)) return 0;
   if (solve(2, 4, 1, 3, 1)) return 0;
   if (solve(3, 4, 1, 2, 1)) return 0;
   cout << 0 << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Data {
   int r, c, v;
   bool operator<(const Data &r) const { return v < r.v; }
 };
 vector<vector<int> > mat, ans;
 Data sorteds[1001000];
 int mR[1001000], mC[1001000];
 map<pair<int, int>, vector<int> > mr, mc;
 void recursiveFix(int rr, int cc) {
   vector<int> &vv = mr[pair<int, int>(rr, mat[rr][cc])];
   vector<int> t = vv;
   for (int j = int(0); j < int(vv.size()); j++) {
     ans[rr][vv[j]] = ans[rr][cc];
   }
   vv.clear();
   vector<int> &vv2 = mc[pair<int, int>(cc, mat[rr][cc])];
   vector<int> t2 = vv2;
   for (int j = int(0); j < int(vv2.size()); j++) {
     ans[vv2[j]][cc] = ans[rr][cc];
   }
   vv2.clear();
   for (int j = int(0); j < int(t2.size()); j++) recursiveFix(t2[j], cc);
   for (int j = int(0); j < int(t.size()); j++) recursiveFix(rr, t[j]);
 }
 int main() {
   int N, M;
   while (scanf('%d %d', &N, &M) != EOF) {
     int c = 0;
     vector<int> t(M + 1);
     mr.clear(), mc.clear();
     mat.clear();
     ans.clear();
     mat.push_back(t);
     ans.push_back(t);
     for (int i = int(1); i < int(N + 1); i++) {
       mat.push_back(t);
       ans.push_back(t);
       for (int j = int(1); j < int(M + 1); j++) {
         scanf('%d', &mat[i][j]);
         sorteds[c++] = Data{i, j, mat[i][j]};
         ans[i][j] = 1;
         mr[pair<int, int>(i, mat[i][j])].push_back(j);
         mc[pair<int, int>(j, mat[i][j])].push_back(i);
       }
     }
     sort(sorteds, sorteds + c);
     memset(mR, 0, sizeof(mR));
     memset(mC, 0, sizeof(mC));
     int prev = -1;
     priority_queue<Data> q;
     for (int i = int(0); i < int(c); i++) {
       if (prev != sorteds[i].v) {
         while (!q.empty()) {
           int rr = q.top().r, cc = q.top().c;
           recursiveFix(rr, cc);
           q.pop();
         }
       }
       int r = sorteds[i].r, c = sorteds[i].c;
       ans[r][c] = 0;
       if (mat[r][mR[r]] == mat[r][c])
         ans[r][c] = max(ans[r][c], ans[r][mR[r]]);
       else
         ans[r][c] = max(ans[r][c], ans[r][mR[r]] + 1);
       if (mat[mC[c]][c] == mat[r][c])
         ans[r][c] = max(ans[r][c], ans[mC[c]][c]);
       else
         ans[r][c] = max(ans[r][c], ans[mC[c]][c] + 1);
       q.push(Data{r, c, ans[r][c]});
       mR[r] = c;
       mC[c] = r;
       prev = sorteds[i].v;
     }
     while (!q.empty()) {
       int rr = q.top().r, cc = q.top().c;
       recursiveFix(rr, cc);
       q.pop();
     }
     for (int i = int(0); i < int(N); i++) {
       for (int j = int(0); j < int(M); j++)
         printf('%d%c', ans[i + 1][j + 1], '
'[j == M - 1]);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct EDGE {
   EDGE(int _v, int _cap, int _edgeidx) : v(_v), cap(_cap), edgeidx(_edgeidx) {}
   int v;
   int cap;
   int edgeidx;
 };
 int DEBUG = 0;
 const int MAXN = 200001;
 vector<EDGE> edge[MAXN];
 int in[MAXN], total[MAXN], dir[MAXN];
 int mark[MAXN];
 int main(int argc, char **argv) {
   DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;
   int n, m;
   cin >> n >> m;
   memset(total, 0, sizeof(total));
   memset(in, 0, sizeof(in));
   memset(mark, 0, sizeof(mark));
   for (int i = 1; i <= m; i++) {
     int a, b, c;
     cin >> a >> b >> c;
     edge[a].push_back(EDGE(b, c, i));
     edge[b].push_back(EDGE(a, c, -i));
     total[a] += c;
     total[b] += c;
   }
   if (DEBUG) {
     for (int i = 1; i <= m; i++) {
       printf('total[%d]= %d ', i, total[i]);
     }
     printf('
');
   }
   queue<int> que;
   mark[1] = 1;
   que.push(1);
   while (!que.empty()) {
     int u = que.front();
     que.pop();
     mark[u] = 1;
     if (DEBUG) {
       printf('
u= %d
', u);
     }
     for (int i = 0; i < edge[u].size(); i++) {
       int edgeidx = edge[u][i].edgeidx;
       int v = edge[u][i].v;
       int cap = edge[u][i].cap;
       if (DEBUG) {
         printf('u= %d, v= %d, edgeidx= %d, cap= %d, mark[%d]= %d
', u, v,
                edgeidx, cap, v, mark[v]);
       }
       if (mark[v] == 0) {
         in[v] += cap;
         if (total[v] == 2 * in[v] && v != n) {
           if (DEBUG) {
             printf('que.push(%d)
', v);
           }
           que.push(v);
         }
         dir[abs(edgeidx)] = ((edgeidx < 0) ? 1 : 0);
       }
     }
     if (DEBUG) {
       printf('que.size()= %d
', que.size());
     }
   }
   for (int i = 1; i <= m; i++) {
     cout << dir[i] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double eps = 1.0e-8;
 const int maxn = 1e6 + 10;
 const int maxm = 300;
 const int turn[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
 long long mod = 1e9 + 7;
 vector<int> tree[10005];
 int ans[10005];
 int anser = 0;
 void dfs(int x, int pre, int c) {
   int cur = c;
   if (c != ans[x]) {
     anser++, cur = ans[x];
   }
   int len = tree[x].size();
   for (int i = 0; i < len; i++) {
     int to = tree[x][i];
     if (to == pre) {
       continue;
     }
     dfs(to, x, cur);
   }
 }
 int main() {
   int n;
   int now;
   cin >> n;
   for (int i = 2; i <= n; i++) {
     cin >> now;
     tree[now].push_back(i);
     tree[i].push_back(now);
   }
   for (int i = 1; i <= n; i++) {
     cin >> ans[i];
   }
   dfs(1, 0, 0);
   cout << anser << endl;
 }


#include <bits/stdc++.h>
 clock_t t = clock();
 namespace my_std {
 using namespace std;
 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 template <typename T>
 inline T rnd(T l, T r) {
   return uniform_int_distribution<T>(l, r)(rng);
 }
 template <typename T>
 inline bool chkmax(T &x, T y) {
   return x < y ? x = y, 1 : 0;
 }
 template <typename T>
 inline bool chkmin(T &x, T y) {
   return x > y ? x = y, 1 : 0;
 }
 template <typename T>
 inline void read(T &t) {
   t = 0;
   char f = 0, ch = getchar();
   double d = 0.1;
   while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
   while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
   if (ch == '.') {
     ch = getchar();
     while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
   }
   t = (f ? -t : t);
 }
 template <typename T, typename... Args>
 inline void read(T &t, Args &...args) {
   read(t);
   read(args...);
 }
 char __sr[1 << 21], __z[20];
 int __C = -1, __zz = 0;
 inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
 inline void print(register int x) {
   if (__C > 1 << 20) Ot();
   if (x < 0) __sr[++__C] = '-', x = -x;
   while (__z[++__zz] = x % 10 + 48, x /= 10)
     ;
   while (__sr[++__C] = __z[__zz], --__zz)
     ;
   __sr[++__C] = '
';
 }
 void file() {}
 inline void chktime() {}
 long long ksm(long long x, int y) {
   long long ret = 1;
   for (; y; y >>= 1, x = x * x)
     if (y & 1) ret = ret * x;
   return ret;
 }
 }  // namespace my_std
 using namespace my_std;
 int n;
 int a[505050];
 namespace BIT {
 int tr[505050];
 void add(int x, int w) {
   ++x;
   x = n - x + 2;
   while (x <= n + 1) chkmax(tr[x], w), x += x & -x;
 }
 int query(int x) {
   ++x;
   x = n - x + 2;
   int res = -1e9;
   while (x) chkmax(res, tr[x]), x -= x & -x;
   return res;
 }
 void clear() {
   for (int i = (1); i <= (n + 1); i++) tr[i] = -1e9;
 }
 }  // namespace BIT
 struct hh {
   int k;
   long long b;
   int l, r;
   const int q(long long x) const {
     x = x * k + b;
     return max(1ll * l, min(x, 1ll * r));
   }
   const hh operator+(const hh &x) const {
     hh res;
     res.k = k * x.k, res.b = b * x.k + x.b;
     long long L = x.q(l), R = x.q(r);
     if (L > R) swap(L, R);
     res.l = L, res.r = R;
     return res;
   }
 };
 struct hhh {
   hh a, b;
   int tp;
   const hhh operator+(const hhh &x) {
     return tp ? (hhh){a + x.b, b + x.a, x.tp ^ 1}
               : (hhh){a + x.a, b + x.b, x.tp};
   }
   pair<long long, long long> q(long long l, long long r) {
     return tp ? make_pair(b.q(r), a.q(l)) : make_pair(a.q(l), b.q(r));
   }
 };
 hhh tr[505050 << 2];
 void pushup(int k) { tr[k] = tr[k << 1 | 1] + tr[k << 1]; }
 void build(int k, int l, int r) {
   if (l == r)
     return tr[k] =
                (hhh){(hh){-1, a[l], 0, a[l] - 1}, (hh){-1, a[l], 1, a[l]}, 1},
            void();
   int mid = (l + r) >> 1;
   build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r), pushup(k);
 }
 int query(int k, int l, int r, int x, long long &L, long long &R) {
   if (r <= x) {
     pair<long long, long long> xx = tr[k].q(L, R);
     if (xx.first <= xx.second) return tie(L, R) = xx, l;
     if (l == r) return L = R = -1, l + 1;
     int mid = (l + r) >> 1;
     int res = query(k << 1 | 1, mid + 1, r, x, L, R);
     return L == -1 ? res : query(k << 1, l, mid, x, L, R);
   }
   int mid = (l + r) >> 1;
   if (x <= mid) return query(k << 1, l, mid, x, L, R);
   int res = query(k << 1 | 1, mid + 1, r, x, L, R);
   return L == -1 ? res : query(k << 1, l, mid, x, L, R);
 }
 int qL(int x, long long l, long long r) { return query(1, 1, n, x, l, r); }
 int dp[505050];
 namespace Q2 {
 long long dB;
 int tp;
 map<long long, int> w;
 multiset<int> s;
 void clear() {
   dB = 0, tp = 1;
   w.clear(), s.clear();
 }
 void insert(long long B, int W) {
   B = (B - dB) * tp;
   if (w.find(B) == w.end())
     w[B] = W, s.insert(W);
   else if (w[B] < W)
     s.erase(s.find(w[B])), s.insert(W), w[B] = W;
 }
 void maintain(long long a) {
   tp *= -1, dB = a - dB;
   for (auto it = w.begin(); it != w.end();) {
     long long b = it->first * tp + dB;
     if (b < 1 || b >= a)
       s.erase(s.find(it->second)), w.erase(it++);
     else
       break;
   }
   int c = 0;
   for (auto it = w.rbegin(); it != w.rend();) {
     long long b = it->first * tp + dB;
     if (b < 1 || b >= a)
       s.erase(s.find(it->second)), it++, c++;
     else
       break;
   }
   while (c--) w.erase(prev(w.end()));
 }
 void add(long long B) {
   B = (B - dB) * tp;
   auto it = w.find(B);
   if (it == w.end()) return;
   s.erase(s.find(it->second)), s.insert(it->second + 1);
   ++w[B];
 }
 int mx() { return s.size() ? *(s.rbegin()) : -1e9; }
 }  // namespace Q2
 void work() {
   read(n);
   for (int i = (1); i <= (n); i++) read(a[i]);
   BIT::clear();
   Q2::clear();
   BIT::add(0, 0);
   build(1, 1, n);
   for (int i = (1); i <= (n); i++) {
     int p = qL(i, 1, a[i] - 1);
     dp[i] = BIT::query(p - 1) + i - 1;
     Q2::maintain(a[i]);
     if (a[i] % 2 == 0) Q2::add(a[i] / 2);
     chkmax(dp[i], Q2::mx() + i - 1);
     if (a[i] % 2 == 0) {
       p = qL(i, a[i] / 2, a[i] / 2);
       int w = BIT::query(p - 1) + 1;
       chkmax(dp[i], w + i - 1);
       Q2::insert(a[i] / 2, w);
     }
     BIT::add(i, dp[i] - i);
   }
   print(n + n - dp[n]);
 }
 int main() {
   file();
   int T;
   read(T);
   while (T--) work();
   Ot();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int hell = 1000000007;
 void solve() {
   long long int n, m;
   cin >> n >> m;
   vector<long long int> used(3 * n + 5, 0);
   vector<long long int> mx;
   long long int ctr = 1;
   for (long long int i = 1; i <= m; i++) {
     long long int u, v;
     cin >> u >> v;
     if (!used[u] and !used[v]) {
       mx.push_back(i);
       used[u] = used[v] = 1;
     }
   }
   if ((long long int)((mx).size()) >= n) {
     cout << 'Matching
';
     for (long long int i = 0; i < n; i++) cout << mx[i] << ' ';
     cout << '
';
   } else {
     cout << 'IndSet
';
     long long int ctr = n;
     for (long long int i = 1; i <= 3 * n; i++) {
       if (!used[i]) {
         cout << i << ' ';
         ctr--;
       }
       if (!ctr) break;
     }
     cout << '
';
   }
 }
 int32_t main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   long long int t;
   cin >> t;
   for (long long int i = 0; i < t; i++) solve();
   cerr << 'Time elapsed : ' << clock() * 1000.0 / CLOCKS_PER_SEC << 'ms'
        << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int n;
 vector<long long> S;
 vector<int> N, L, R;
 vector<bool> E;
 pair<long long, pair<int, int>> merge(int l, int r, int mid,
                                       pair<long long, pair<int, int>> L,
                                       pair<long long, pair<int, int>> R) {
   int ll = L.second.first, lr = L.second.second;
   int rl = R.second.first, rr = R.second.second;
   long long ls = L.first, rs = R.first;
   long long sum = ls + rs;
   int lw = ll, rw = rr;
   int lLength = mid - l + 1, rLength = r - (mid + 1) + 1;
   if (lr && rl && N[mid + 1] >= N[mid]) {
     if (ll == lLength) lw += rl;
     if (rr == rLength) rw += lr;
     if (ll != lLength && rl != rLength)
       sum += (long long)(lr + rl) * (lr + rl - 1) / 2;
   } else {
     if (ll != lLength) sum += (long long)(lr) * (lr - 1) / 2;
     if (rr != rLength) sum += (long long)(rl) * (rl - 1) / 2;
   }
   return {sum, {lw, rw}};
 }
 pair<long long, pair<int, int>> getVal(int lq, int rq, int l = 0, int r = n - 1,
                                        int index = 0) {
   if (l == r) return {0, {1, 1}};
   if (lq == l && rq == r && E[index]) return {S[index], {L[index], R[index]}};
   int mid = (l + r) >> 1;
   if (rq <= mid) {
     return getVal(lq, rq, l, mid, 2 * index + 1);
   } else if (lq > mid) {
     return getVal(lq, rq, mid + 1, r, 2 * index + 2);
   }
   auto lv = getVal(lq, mid, l, mid, 2 * index + 1);
   auto rv = getVal(mid + 1, rq, mid + 1, r, 2 * index + 2);
   auto ret = merge(lq, rq, mid, lv, rv);
   if (l == lq && r == rq) {
     E[index] = true;
     S[index] = ret.first, L[index] = ret.second.first,
     R[index] = ret.second.second;
   }
   return ret;
 }
 void update(int lq, int rq, int l = 0, int r = n - 1, int index = 0) {
   if (l == r) return;
   E[index] = false;
   int mid = (l + r) >> 1;
   if (rq <= mid) {
     return update(lq, rq, l, mid, 2 * index + 1);
   } else if (lq > mid) {
     return update(lq, rq, mid + 1, r, 2 * index + 2);
   }
   update(lq, mid, l, mid, 2 * index + 1);
   update(mid + 1, rq, mid + 1, r, 2 * index + 2);
   return;
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   int q;
   cin >> n >> q;
   N = vector<int>(n);
   S = vector<long long>(4 * n + 1);
   L = R = vector<int>(4 * n + 1);
   E = vector<bool>(4 * n + 1, false);
   for (auto &it : N) cin >> it;
   int cmd, x, y;
   for (int qq = 0; qq < q; qq++) {
     cin >> cmd >> x >> y;
     if (cmd == 1) {
       N[x - 1] = y;
       update(x - 1, x - 1);
     } else {
       x--, y--;
       auto res = getVal(x, y);
       auto sum = res.first;
       int l = res.second.first, r = res.second.second;
       if (l == (y - x + 1))
         sum += (long long)(l) * (l - 1) / 2;
       else
         sum += ((long long)(l) * (l - 1) + (long long)(r) * (r - 1)) / 2;
       sum += (y - x + 1);
       cout << sum << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 505;
 int n, in[N], out[N], w[N], s[N], v[N];
 vector<int> st[N << 1];
 int f1[N][N << 1], f2[N][N << 1];
 int dfs(int x, int S) {
   if (S < 0) return 0;
   if (~f1[x][S]) return f1[x][S];
   f2[x][out[x] + 1] = 0;
   for (int i = out[x]; i >= in[x]; i--) {
     f2[x][i] = f2[x][i + 1];
     for (int j = 0; j < st[i].size(); j++) {
       int p = st[i][j];
       if (out[p] > out[x] || p == x) continue;
       f2[x][i] = max(f2[x][i], dfs(p, min(S - w[p], s[p])) + f2[x][out[p]]);
     }
   }
   return f1[x][S] = f2[x][in[x]] + v[x];
 }
 int main() {
   int S;
   scanf('%d%d', &n, &S);
   for (int i = 1; i <= n; i++) {
     scanf('%d%d%d%d%d', &in[i], &out[i], &w[i], &s[i], &v[i]);
     st[in[i]].push_back(i);
   }
   memset(f1, -1, sizeof f1);
   in[0] = 0;
   out[0] = 2 * n + 1;
   printf('%d
', dfs(0, S));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void fr(int &a, bool f = 0, char ch = getchar()) {
   for (a = 0; ch < '0' || ch > '9'; ch = getchar()) ch == '-' ? f = 1 : 0;
   for (; ch >= '0' && ch <= '9'; ch = getchar()) a = a * 10 + ch - '0';
   a = f ? -a : a;
 }
 int fr() {
   int a;
   return fr(a), a;
 }
 const int N = 12;
 int arr[(1 << N) + 10], a[(1 << N) + 10], ans[(1 << N) + 10], p[(1 << N) + 10];
 int tmp[(1 << N) + 10];
 bool visval[(1 << N) + 10], vispos[(1 << N) + 10];
 struct OI {
   int RP, score;
 } CSPS2021, NOIP2021;
 int main() {
   CSPS2021.RP++, CSPS2021.score++, NOIP2021.RP++, NOIP2021.score++, 392699;
   srand(time(0));
   int n = fr(), xsum = 0, tot = 1 << n;
   for (int i = 0; i < (1 << n); i++) fr(a[i]), xsum ^= a[i], arr[i] = i;
   if (xsum) return puts('Fou'), 0;
   puts('Shi');
   for (int i = 0; i < (1 << n); i++) {
     int pos = rand() % tot, arrpos = arr[pos];
     if (pos != tot - 1) arr[pos] = arr[tot - 1];
     tot--, tmp[0] = 0;
     bool flag = 0;
     for (int j = 0; j < (1 << n); j++)
       if (vispos[j] == 0 && visval[arrpos ^ a[j]] == 0) {
         flag = visval[arrpos ^ a[j]] = vispos[j] = 1, ans[j] = arrpos,
         p[arrpos ^ a[j]] = j;
         break;
       } else if (vispos[j] == 0)
         tmp[++tmp[0]] = j;
     if (flag == 0) {
       int j = tmp[rand() % tmp[0] + 1];
       vispos[p[arrpos ^ a[j]]] = 0;
       arr[tot++] = ans[p[arrpos ^ a[j]]], ans[j] = arrpos;
       vispos[j] = 1;
       visval[ans[j] ^ a[j]] = 1;
       p[ans[j] ^ a[j]] = j;
       i--;
     }
   }
   for (int i = 0; i < (1 << n); i++) printf('%d ', ans[i] ^ a[i]);
   puts('');
   for (int i = 0; i < (1 << n); i++) printf('%d ', ans[i]);
   puts('');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int ans[2020], cnt = 0, k;
 int main() {
   cin >> k;
   k++;
   ans[++cnt] = -1;
   while (k >= 1e6 - 1) {
     k -= 1e6 - 1;
     ans[++cnt] = 1e6 - 1 + 1;
   }
   ans[++cnt] = k + 1;
   cout << cnt << endl;
   for (int i = 1; i <= cnt; i++) cout << ans[i] << ' ';
   cout << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   long long f = 0, j, q = 1, i, n;
   while (q--) {
     long long y, k = 1, x, M, s;
     cin >> n >> M >> s >> f;
     unordered_map<int, pair<int, int> > m;
     for (i = 0; i < M; i++) {
       long long X, Y, Z;
       cin >> X >> Y >> Z;
       m.insert(make_pair(X, make_pair(Y, Z)));
     }
     if (s < f) {
       while (s != f) {
         if (m.count(k)) {
           if ((s >= m[k].first && s <= m[k].second) ||
               (s + 1 >= m[k].first && s + 1 <= m[k].second))
             cout << 'X';
           else {
             cout << 'R';
             s++;
           }
         } else {
           cout << 'R';
           s++;
         }
         k++;
       }
     } else {
       while (s != f) {
         if (m.count(k)) {
           if ((s >= m[k].first && s <= m[k].second) ||
               (s - 1 >= m[k].first && s - 1 <= m[k].second))
             cout << 'X';
           else {
             cout << 'L';
             s--;
           }
         } else {
           cout << 'L';
           s--;
         }
         k++;
       }
     }
     cout << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string ch1, ch2, ch3;
   getline(cin, ch1);
   getline(cin, ch2);
   for (int s = 0; s < ch1.size(); s++) {
     if (ch1[s] != ch2[s]) {
       ch3 += '1';
     } else {
       ch3 += '0';
     }
   }
   cout << ch3 << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cout.tie(nullptr);
   long long n, k;
   cin >> n >> k;
   long long l = 0;
   long long r = n;
   while (l != r) {
     long long mid = (l + r) / 2;
     if (mid * (mid + 1) / 2 - (n - mid) < k)
       l = mid + 1;
     else
       r = mid;
   }
   cout << n - l;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Solve {
   bool mark[(int)(3e5 + 10)];
   vector<pair<int, int>> ed[(int)(3e5 + 10)];
   vector<int> mtch;
   int n, m, T;
   void go() {
     T = 0;
     input();
     find_mx_match();
     if (T >= 2 * n)
       prnt_match();
     else
       prnt_ind();
   }
   void input() {
     cin >> n >> m;
     for (int i = 0; i < m; i++) {
       int u, v;
       cin >> u >> v;
       u--, v--;
       ed[u].push_back({v, i + 1});
       ed[v].push_back({u, i + 1});
     }
   }
   void find_mx_match() {
     for (int i = 0; i < 3 * n; i++) {
       if (mark[i]) continue;
       for (auto x : ed[i]) {
         if (mark[x.first]) continue;
         mark[x.first] = mark[i] = true;
         mtch.push_back(x.second);
         T += 2;
         break;
       }
     }
   }
   void prnt_match() {
     cout << 'Matching
';
     int I = 0;
     for (auto x : mtch) {
       if (I == n) break;
       cout << x << ' ';
       I++;
     }
     cout << endl;
   }
   void prnt_ind() {
     cout << 'IndSet
';
     for (int I = 0, J = 0; I < 3 * n && J < n; I++) {
       if (!mark[I]) {
         cout << I + 1 << ' ';
         J++;
       }
     }
     cout << endl;
   }
   void clear() {
     T = 0;
     for (int i = 0; i < 3 * n; i++) mark[i] = false;
     for (int i = 0; i < 3 * n; i++) ed[i].clear();
     n = -1, m = -1;
     mtch.clear();
   }
 } S;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(NULL), cout.tie(NULL);
   int T;
   cin >> T;
   while (T--) {
     S.go();
     S.clear();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ;
   long long t;
   cin >> t;
   while (t--) {
     long long n, x, m, i, flag = 0, a, b, l, r, sum = 2, ones = 0, zero = 0,
                           output = 0, j;
     vector<pair<long long, long long> > v;
     cin >> n >> m;
     long long arr[n + 1][m + 1];
     for (i = 1; i <= n; i++) {
       for (j = 1; j <= m; j++) {
         cin >> arr[i][j];
       }
     }
     a = 0;
     sum = 2;
     flag = 0;
     while (flag == 0) {
       for (i = 1; i <= n; i++) {
         for (j = 1; j <= m; j++) {
           if ((i + j == sum) || (i + j == n + m - a))
             v.push_back(make_pair(i, j));
         }
       }
       while (v.size() > 0) {
         i = v[v.size() - 1].first;
         j = v[v.size() - 1].second;
         if (arr[i][j] == 0)
           zero++;
         else
           ones++;
         v.pop_back();
       }
       output = output + min(zero, ones);
       zero = 0;
       ones = 0;
       a++;
       sum++;
       if (sum == n + m - a)
         break;
       else if (sum > (n + m - a))
         break;
     }
     cout << output << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long int t;
   cin >> t;
   while (t--) {
     long long int c, m, x;
     cin >> c >> m >> x;
     long long int y = c + m + x;
     y = y / 3;
     long long int ans = 0;
     if (c >= 1 && m >= 1) {
       long long int v = min(c, m);
       ans = min(v, y);
     }
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Edge {
   int t, next;
   Edge() {}
   Edge(int a, int b) : t(a), next(b) {}
 };
 Edge e[200005];
 int head[100005];
 vector<int> g[100005];
 queue<int> bel[100005];
 int d[100005], scc_cnt;
 namespace Graph {
 int dfn[100005], low[100005], dfs_cnt;
 int st[100005], top;
 bool in[100005];
 int id[100005];
 void dfs(int x) {
   dfn[x] = low[x] = ++dfs_cnt;
   st[++top] = x;
   in[x] = 1;
   for (int i = head[x]; i; i = e[i].next) {
     int u = e[i].t;
     if (!dfn[u]) {
       dfs(u);
       low[x] = min(low[x], low[u]);
     } else if (in[u])
       low[x] = min(low[x], dfn[u]);
   }
   if (low[x] >= dfn[x]) {
     scc_cnt++;
     int t;
     do {
       t = st[top--];
       in[t] = 0;
       id[t] = scc_cnt;
       bel[scc_cnt].push(t);
     } while (t != x);
   }
 }
 void build(int n) {
   for (int i = 1; i <= n; i++)
     if (!dfn[i]) dfs(i);
   for (int i = 1; i <= n; i++)
     for (int j = head[i]; j; j = e[j].next)
       if (id[i] != id[e[j].t]) {
         int u = id[i], v = id[e[j].t];
         g[u].push_back(v);
         d[v]++;
       }
 }
 }  // namespace Graph
 bool ask(int x, int y) {
   printf('? %d %d
', x, y);
   fflush(stdout);
   scanf('%d', &x);
   return x;
 }
 queue<int> q;
 void erase(int x) {
   for (int i = 0; i < g[x].size(); i++) {
     int u = g[x][i];
     d[u]--;
     if (!d[u]) q.push(u);
   }
 }
 int solve(int n) {
   int ans = 0;
   for (int i = 1; i <= scc_cnt; i++)
     if (!d[i]) {
       if (!ans)
         ans = bel[i].front();
       else
         q.push(i);
     }
   while (!q.empty()) {
     int t = q.front(), x = bel[t].front();
     if (ask(ans, x)) {
       bel[t].pop();
       if (!bel[t].size()) {
         erase(t);
         q.pop();
       }
     } else {
       int p = Graph::id[ans];
       bel[p].pop();
       if (!bel[p].size())
         erase(p);
       else
         q.push(p);
       ans = x;
       q.pop();
     }
   }
   return ans;
 }
 int main() {
   int n, m;
   scanf('%d%d', &n, &m);
   for (int i = 1; i <= m; i++) {
     int x, y;
     scanf('%d%d', &x, &y);
     e[i] = Edge(y, head[x]);
     head[x] = i;
   }
   Graph::build(n);
   printf('! %d
', solve(n));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int d[66000], s[66000];
 int que[66000];
 bool f[66000];
 int n, m;
 vector<pair<int, int> > res;
 int biton(int x, int k) { return (x | (1 << k)); }
 int Minus(int x, int y) {
   int z = 0;
   for (int i = 0; i < 17; i++) {
     int u = ((x >> i) & 1);
     int v = ((y >> i) & 1);
     if (u != v) z = biton(z, i);
   }
   return z;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin >> n;
   for (int i = 0; i < n; i++) cin >> d[i] >> s[i];
   for (int i = 0; i < n; i++)
     if (d[i] == 1) {
       f[i] = true;
       m++;
       que[m] = i;
     }
   while (m > 0) {
     int u = que[m];
     if (d[u] == 0) {
       m--;
       continue;
     }
     int v = s[u];
     res.push_back(make_pair(u, v));
     s[v] = Minus(s[v], u);
     d[v]--;
     m--;
     if (d[v] == 1 && f[v] == false) {
       f[v] = true;
       m++;
       que[m] = v;
     }
   }
   cout << res.size() << '
';
   for (int i = 0; i < res.size(); i++)
     cout << res[i].first << ' ' << res[i].second << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long a[200005];
 int main() {
   int t;
   cin >> t;
   while (t--) {
     memset(a, 0, sizeof(a));
     int n;
     cin >> n;
     for (int i = 0; i < n; i++) {
       cin >> a[i];
     }
     sort(a, a + n);
     if (a[0] != a[n - 1]) {
       printf('1
');
     } else {
       printf('%d
', n);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void _fill_int(int* p, int val, int rep) {
   int i;
   for (i = 0; i < rep; i++) p[i] = val;
 }
 signed long long GETi() {
   signed long long i;
   scanf('%lld', &i);
   return i;
 }
 int N, M;
 int A[10000], B[10000];
 int T[4][10000];
 void solve() {
   int f, i, j, k, l, x, y;
   cin >> N >> M;
   for (i = 0; i < N; i++) A[i] = 1 << 29;
   for (i = 0; i < M; i++) {
     cin >> T[0][i] >> T[1][i] >> T[2][i] >> T[3][i];
     T[1][i]--;
     T[2][i]--;
   }
   for (i = M - 1; i >= 0; i--) {
     if (T[0][i] == 1) {
       for (j = T[1][i]; j <= T[2][i]; j++) A[j] -= T[3][i];
     } else {
       for (j = T[1][i]; j <= T[2][i]; j++) A[j] = min(A[j], T[3][i]);
     }
   }
   int ng = 0;
   memmove(B, A, sizeof(A));
   for (i = 0; i < M; i++) {
     int y = -1 << 29;
     if (T[0][i] == 1)
       for (j = T[1][i]; j <= T[2][i]; j++) B[j] += T[3][i];
     else {
       for (j = T[1][i]; j <= T[2][i]; j++) y = max(y, B[j]);
       if (y != T[3][i]) ng = 1;
     }
   }
   if (ng)
     (void)printf('NO
');
   else {
     (void)printf('YES
');
     for (i = 0; i < N; i++) (void)printf('%d ', A[i]);
     (void)printf('
');
   }
 }
 int main(int argc, char** argv) {
   if (argc > 1) freopen(argv[1], 'r', stdin);
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, t;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cin >> t;
   while (t--) {
     cin >> n;
     if (n == 1) {
       cout << '-1' << endl;
       continue;
     }
     string ans = '2';
     for (int i = 1; i < n; i++) ans += '3';
     cout << ans << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int MinDist(int n, int k, const string& s) {
   int first_cow_pos = 0;
   while (first_cow_pos < n && s[first_cow_pos] == '1') {
     ++first_cow_pos;
   }
   int last_cow_pos = first_cow_pos;
   int rooms = 1;
   while (rooms < k + 1) {
     ++last_cow_pos;
     if (s[last_cow_pos] == '0') {
       ++rooms;
     }
   }
   int farmer_pos = first_cow_pos;
   int delta = last_cow_pos - first_cow_pos;
   for (int i = first_cow_pos + 1; i <= last_cow_pos; ++i) {
     if (s[i] == '0' && i - first_cow_pos < delta && last_cow_pos - i < delta) {
       delta = max(i - first_cow_pos, last_cow_pos - i);
       farmer_pos = i;
     }
   }
   --rooms;
   for (++first_cow_pos; first_cow_pos < n; ++first_cow_pos) {
     if (s[first_cow_pos] == '1') {
       continue;
     }
     while (rooms < k + 1 && last_cow_pos < n) {
       ++last_cow_pos;
       if (last_cow_pos == n) {
         break;
       }
       if (s[last_cow_pos] == '0') {
         ++rooms;
       }
     }
     if (last_cow_pos == n) {
       break;
     }
     int npos = max(farmer_pos, first_cow_pos);
     int cur_delta = max(farmer_pos - first_cow_pos, last_cow_pos - farmer_pos);
     while (npos < last_cow_pos) {
       ++npos;
       if (s[npos] == '1') {
         continue;
       }
       if (npos - first_cow_pos < cur_delta && last_cow_pos - npos < cur_delta) {
         cur_delta = max(npos - first_cow_pos, last_cow_pos - npos);
         farmer_pos = npos;
       } else if (npos - first_cow_pos >= cur_delta) {
         break;
       }
     }
     delta = min(delta, cur_delta);
     --rooms;
   }
   return delta;
 }
 int MinDistSlow(int n, int k, const string& s) {
   int result = n;
   for (int fcp = 0; fcp < n; ++fcp) {
     if (s[fcp] == '1') {
       continue;
     }
     int lcp = fcp;
     int rooms = 1;
     while (rooms < k + 1 && lcp < n) {
       ++lcp;
       if (lcp == n) {
         break;
       }
       if (s[lcp] == '0') {
         ++rooms;
       }
     }
     if (lcp == n) {
       break;
     }
     for (int fp = fcp; fp <= lcp; ++fp) {
       if (s[fp] == '1') {
         continue;
       }
       int delta = max(fp - fcp, lcp - fp);
       if (delta < result) {
         result = delta;
       }
     }
   }
   return result;
 }
 int main() {
   int n, k;
   cin >> n >> k;
   string s;
   char buffer[1000000];
   scanf('%s', buffer);
   s = buffer;
   int delta = MinDist(n, k, s);
   cout << delta << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = 1e9;
 const long long INF = 1e18;
 long long n, sum, ans;
 long long a[200010];
 struct BIT {
   long long tree[600010];
   BIT() { memset(tree, 0, sizeof(tree)); }
   void update(long long i, long long x) {
     while (i <= 600000) {
       tree[i] += x;
       i += i & -i;
     }
   }
   long long sum(long long i) {
     long long ret = 0;
     while (i) {
       ret += tree[i];
       i -= i & -i;
     }
     return ret;
   }
 } t1, t2;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   ;
   cin >> n;
   for (long long i = 1; i <= n; i++) {
     cin >> a[i];
     ans += (i - 1) * a[i] - t1.sum(a[i]);
     long long x = sum;
     for (long long j = a[i]; j <= 300000; j += a[i]) {
       t1.update(j, a[i]);
       x -= (j / a[i]) * a[i] * (t2.sum(j + a[i] - 1) - t2.sum(j - 1));
     }
     ans += x;
     sum += a[i];
     t2.update(a[i], 1);
     cout << ans << ' ';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 struct city_t : array<bitset<99>, 4> {
   int n, k, m, p;
   city_t() { cin >> n >> k, m = n / 2, p = n - 2; }
   void hotel(int i, int j) {
     if (!(*this)[i][j] && k > 0) (*this)[i][j] = true, k--;
   }
   void solve() {
     if (k % 2) hotel(1, m);
     for (int i = 1; i < 3 && k > 0; i++)
       for (int u = 1, v = p; v > u && k > 0; u++, v--) hotel(i, u), hotel(i, v);
     hotel(1, m), hotel(2, m);
   }
   void write() {
     cout << 'YES';
     for (int i = 0; i < 4; i++) {
       cout << '
';
       for (int j = 0; j < n; j++)
         if ((*this)[i][j])
           cout << '#';
         else
           cout << '.';
     }
   }
 };
 int main() {
   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
   city_t city;
   city.solve(), city.write();
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline void smin(T &a, T b) {
   if (a > b) a = b;
 }
 template <class T>
 inline void smax(T &a, T b) {
   if (a < b) a = b;
 }
 int V, E, src, tar;
 int head[60000], work[60000], dis[60000];
 int to[2000000], cap[2000000], nxt[2000000];
 int q[60000], qf, qb;
 void init(int n) {
   memset(head, -1, sizeof(int) * n);
   V = n;
   E = 0;
 }
 void add_edge(int u, int v, int c) {
   to[E] = v;
   cap[E] = c;
   nxt[E] = head[u];
   head[u] = E++;
   to[E] = u;
   cap[E] = 0;
   nxt[E] = head[v];
   head[v] = E++;
 }
 bool bfs() {
   memset(dis, -1, sizeof(dis));
   qf = qb = 0;
   q[qb++] = src;
   dis[src] = 0;
   while (qf < qb && dis[tar] == -1) {
     int u = q[qf++];
     for (int i = head[u]; i >= 0; i = nxt[i]) {
       int v = to[i];
       if (dis[v] == -1 && cap[i] > 0) {
         dis[v] = dis[u] + 1;
         q[qb++] = v;
       }
     }
   }
   return dis[tar] >= 0;
 }
 int dfs(int u, int &bot) {
   int v, bot1, delta;
   if (u == tar) return bot;
   for (int &i = work[u]; i >= 0; i = nxt[i]) {
     v = to[i];
     if (dis[v] != dis[u] + 1 || cap[i] == 0) continue;
     bot1 = min(bot, cap[i]);
     if (delta = dfs(v, bot1)) {
       cap[i] -= delta;
       cap[i ^ 1] += delta;
       bot = bot1;
       return delta;
     }
   }
   return 0;
 }
 int dinic() {
   int ans = 0, delta, bot;
   while (bfs()) {
     memcpy(work, head, sizeof(int) * V);
     delta = 0x3f3f3f3f;
     while (delta = dfs(src, bot = 0x3f3f3f3f)) ans += delta;
   }
   return ans;
 }
 int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
 int n, m, dst[33][33][33][33];
 char grid[33][33];
 int c[2], x[2][33 * 33], y[2][33 * 33], t[2][33 * 33];
 bool can(long long mid) {
   int A = c[0], nm = n * m;
   src = A * 2 + 2 * nm, tar = src + 1;
   init(tar + 10);
   for (int i = (0); i < (A); i++) add_edge(src, i, 1);
   for (int i = (0); i < (A); i++) add_edge(A + 2 * nm + i, tar, 1);
   for (int i = (0); i < (nm); i++) add_edge(A + i, A + nm + i, 1);
   for (int r = (0); r < (2); r++)
     for (int k = (0); k < (A); k++)
       for (int i = (0); i < (n); i++)
         for (int j = (0); j < (m); j++) {
           if (grid[i][j] == '#') continue;
           int xx = x[r][k], yy = y[r][k], tt = t[r][k];
           if (dst[xx][yy][i][j] < 1000 &&
               (long long)dst[xx][yy][i][j] * tt <= mid) {
             if (!r)
               add_edge(k, A + i * m + j, 1);
             else
               add_edge(A + nm + i * m + j, k + A + 2 * nm, 1);
           }
         }
   return dinic() == A;
 }
 int main() {
   ;
   scanf('%d%d%d%d', &n, &m, &c[0], &c[1]);
   for (int i = (0); i < (n); i++) scanf('%s', grid[i]);
   int u, v, w;
   scanf('%d%d%d', &u, &v, &w);
   u--;
   v--;
   for (int i = (0); i < (2); i++)
     for (int j = (0); j < (c[i]); j++) {
       scanf('%d%d%d', &x[i][j], &y[i][j], &t[i][j]);
       x[i][j]--;
       y[i][j]--;
     }
   if (c[0] < c[1]) {
     x[0][c[0]] = u;
     y[0][c[0]] = v;
     t[0][c[0]++] = w;
   } else {
     x[1][c[1]] = u;
     y[1][c[1]] = v;
     t[1][c[1]++] = w;
   }
   if (c[0] != c[1]) {
     puts('-1');
     return 0;
   }
   memset(dst, 0x3f, sizeof(dst));
   for (int i = (0); i < (n); i++)
     for (int j = (0); j < (m); j++) {
       if (grid[i][j] == '#') continue;
       qb = qf = 0;
       q[qb++] = i;
       q[qb++] = j;
       dst[i][j][i][j] = 0;
       while (qb > qf) {
         u = q[qf++];
         v = q[qf++];
         for (int k = (0); k < (4); k++) {
           int uu = u + dx[k], vv = v + dy[k];
           if ((uu < 0 || vv < 0 || uu >= n || vv >= m)) continue;
           if (grid[uu][vv] == '#') continue;
           if (dst[i][j][uu][vv] > dst[i][j][u][v] + 1) {
             dst[i][j][uu][vv] = dst[i][j][u][v] + 1;
             q[qb++] = uu;
             q[qb++] = vv;
           }
         }
       }
     }
   long long st = -1, ed = (1LL << 40), mid;
   while (st + 1 < ed) {
     mid = (st + ed) >> 1;
     if (can(mid))
       ed = mid;
     else
       st = mid;
   }
   if (ed == (1LL << 40))
     puts('-1');
   else
     printf('%I64d
', ed);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, m, k, x, y, z;
 int main() {
   cin >> n >> k >> m;
   if (m <= k)
     cout << m << endl;
   else if (m <= n)
     cout << k << endl;
   else {
     long long tmp = n + k;
     cout << tmp - m << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> E[3010];
 int mark[3010];
 int c, f;
 bool find(int i, int p) {
   if (mark[i] == 1) {
     c = i;
     f = 1;
     return true;
   }
   mark[i] = 1;
   for (vector<int>::iterator to = E[i].begin(); to < E[i].end(); to++) {
     if (*to != p && find(*to, i)) {
       if (f) mark[i] = 2;
       if (i == c) f = 0;
       return true;
     }
   }
   return false;
 }
 int ans[3010];
 void bfs(int i) {
   queue<int> v;
   v.push(i);
   while (!v.empty()) {
     int to = v.front();
     v.pop();
     for (vector<int>::iterator at = E[to].begin(); at < E[to].end(); at++) {
       if (ans[*at] > ans[to] + 1) {
         ans[*at] = ans[to] + 1;
         v.push(*at);
       }
     }
   }
 }
 int main() {
   int n;
   scanf('%d', &n);
   for (int i = 0; i < n; i++) {
     int a, b;
     scanf('%d%d', &a, &b);
     a--;
     b--;
     E[a].push_back(b);
     E[b].push_back(a);
   }
   find(0, -1);
   for (int i = 0; i < 3010; i++) ans[i] = 10000000;
   for (int i = 0; i < n; i++)
     if (mark[i] == 2) {
       ans[i] = 0;
       bfs(i);
     }
   for (int i = 0; i < n; i++) printf('%d ', ans[i]);
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('Ofast')
 #pragma GCC optimize('unroll-loops')
 int main() {
   std::ios::sync_with_stdio(false);
   std::cin.tie(nullptr);
   int n, k;
   std::cin >> n >> k;
   int64_t s1 = 0;
   int s2 = 0;
   for (int i = 0; i < k; ++i) {
     int a, b;
     std::cin >> a >> b;
     s1 += b;
     s2 = (s2 + 1ll * a * b) % n;
   }
   if (s1 > n) {
     std::cout << -1 << '
';
   } else if (s1 < n) {
     std::cout << 1 << '
';
   } else if (1ll * n * (n - 1) / 2 % n == s2) {
     std::cout << 1 << '
';
   } else {
     std::cout << -1 << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t, n, x, y, good, bad;
   scanf('%d', &t);
   while (t--) {
     scanf('%d%d%d', &n, &x, &y);
     bad = min(n, x + y - 1);
     if (x + y <= n)
       good = 1;
     else
       good = min(n, x + y - n + 1);
     printf('%d %d
', good, bad);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int gcd(long long int a, long long int b) {
   return b ? gcd(b, a % b) : a;
 }
 long long int lcm(long long int a, long long int b) {
   return a * b / gcd(a, b);
 }
 int n, m, cnt[11][11], a[22], b[22], tr[11], mx;
 void bktk(int i) {
   if (i == n + 1) {
     int ans = 0;
     memset(cnt, 0, sizeof(cnt));
     for (int j = 1; j <= m; j++)
       cnt[min(tr[a[j]], tr[b[j]])][max(tr[b[j]], tr[a[j]])]++;
     for (int j = 1; j <= 6; j++)
       for (int k = 1; k <= 6; k++)
         if (cnt[j][k]) ans++;
     mx = max(ans, mx);
     return;
   }
   for (int j = 1; j <= 6; j++) {
     tr[i] = j;
     bktk(i + 1);
   }
 }
 int main() {
   cin >> n >> m;
   if (n < 7) {
     cout << m << endl;
     return 0;
   }
   for (int i = 1; i <= m; i++) cin >> a[i] >> b[i];
   bktk(1);
   cout << mx << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long q;
   cin >> q;
   if (q == 1) {
     cout << '1
0
';
     return 0;
   }
   long long a = -1, b = -1;
   for (long long d = 2; d * d <= q; d++) {
     if (q % d) continue;
     a = d;
     b = q / d;
     break;
   }
   if (a == -1) {
     cout << '1
0
';
     return 0;
   }
   for (long long d = 2; d * d <= b; d++) {
     if (b % d) continue;
     cout << '1
' << a * d << '
';
     return 0;
   }
   cout << '2
';
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 using namespace std;
 const int MAX = 5e5 + 10;
 const long long MAX2 = 11;
 const long long MOD = 1000000007;
 const long long MOD2 = 1000000006;
 const long long INF = 2e18;
 const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
 const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
 const double pi = acos(-1);
 const double EPS = 1e-9;
 const int block = 2000;
 long long n, k, ans;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> k;
   ans = 1;
   for (int i = 2; i <= k; ++i) ans = (ans * k) % MOD;
   n -= k;
   for (int i = 1; i <= n; ++i) ans = (ans * n) % MOD;
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void main0();
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   main0();
   return 0;
 }
 const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
 const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
 const int N = 2e3 + 1;
 const int INF = 0x3f3f3f3f;
 const long long INFF = 0x3f3f3f3f3f3f3f3f;
 const int mod = 998244353;
 const double eps = 1e-6;
 const double Pi = acos(-1.0);
 mt19937 rnd(
     (unsigned int)chrono::steady_clock::now().time_since_epoch().count());
 int n, m;
 char a[N][N];
 int ans[N][N];
 int vis[N][N], step;
 int incyc, cyclen;
 pair<int, int> cycp;
 void dfs(int x, int y) {
   vis[x][y] = ++step;
   int xt = x;
   int yt = y;
   if (a[x][y] == 'U')
     xt--;
   else if (a[x][y] == 'D')
     xt++;
   else if (a[x][y] == 'L')
     yt--;
   else
     yt++;
   if ((1 > xt) || (xt > n) || (1 > yt) || (yt > m)) {
     vis[x][y] = 0;
     ans[x][y] = 1;
     return;
   }
   if (vis[xt][yt]) {
     incyc = 1;
     cyclen = vis[x][y] - vis[xt][yt] + 1;
     vis[xt][yt] = 0;
     vis[x][y] = 0;
     ans[x][y] = cyclen;
     return;
   }
   if (ans[xt][yt] == 0) {
     dfs(xt, yt);
   }
   if (incyc)
     ans[x][y] = cyclen;
   else
     ans[x][y] = ans[xt][yt] + 1;
   if (vis[x][y] == 0) incyc = 0;
   vis[x][y] = 0;
 }
 void main0() {
   int T;
   cin >> T;
   while (T--) {
     cin >> n >> m;
     for (int i = 1; i <= n; ++i) {
       for (int j = 1; j <= m; ++j) {
         ans[i][j] = vis[i][j] = 0;
       }
     }
     for (int i = 1; i <= n; ++i) {
       cin >> a[i] + 1;
     }
     for (int i = 1; i <= n; ++i) {
       for (int j = 1; j <= m; ++j) {
         if (ans[i][j]) continue;
         step = 0;
         dfs(i, j);
       }
     }
     pair<int, int> pos = {1, 1};
     for (int i = 1; i <= n; ++i) {
       for (int j = 1; j <= m; ++j) {
         if (ans[i][j] > ans[pos.first][pos.second]) pos = {i, j};
       }
     }
     cout << pos.first << ' ' << pos.second << ' ' << ans[pos.first][pos.second]
          << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = int(1e5 + 5);
 vector<int> g[N];
 int cnt[N];
 void dfs(int v, int depth = 0) {
   cnt[depth]++;
   for (int to : g[v]) {
     dfs(to, depth + 1);
   }
 }
 int main() {
   ios::sync_with_stdio(0);
   int n;
   cin >> n;
   for (int i = 2; i <= n; i++) {
     int p;
     cin >> p;
     g[p].push_back(i);
   }
   dfs(1);
   int res = 0;
   for (int i = 0; i <= n; i++) {
     res += cnt[i] % 2;
   }
   cout << res << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int ans[110000], add[110000];
 int n, m, a, cnt, me;
 char temp[3];
 int main() {
   while (~scanf('%d%d', &n, &m)) {
     for (int i = 1; i <= n; i++) ans[i] = 1, add[i] = 0;
     ;
     cnt = 0;
     me = 0;
     for (int i = 1; i <= m; i++) {
       scanf('%s%d', temp, &a);
       if (*temp == '+') {
         if (!me || (me == a))
           me = a;
         else
           ans[a] = 0;
         add[a] = 1;
         if (a != me && !add[me]) ans[me] = 0;
         cnt++;
       } else {
         if (!me) me = a;
         if (a != me && add[a] == 0) {
           ans[me] = 0;
           me = a;
         }
         if (add[a]) {
           add[a] = 0;
           cnt--;
         }
         if (cnt) ans[a] = 0;
       }
     }
     int tt = 0;
     bool fi = true;
     for (int i = 1; i <= n; i++)
       if (ans[i]) tt++;
     printf('%d
', tt);
     for (int i = 1; i <= n; i++) {
       if (ans[i]) {
         if (!fi) printf(' ');
         printf('%d', i);
         if (fi) fi = false;
       }
     }
     printf('
');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string str[1009];
   int n, m;
   while (cin >> n >> m) {
     for (int i = 0; i < n; i++) cin >> str[i];
     int ret = 0, cury = 0;
     for (int i = 0; i < n; i++) {
       int right = cury;
       if (i % 2 == 0) {
         for (int j = 0; j < m; j++)
           if (str[i][j] == 'W') right = j;
         int right1 = cury;
         if (i < n - 1)
           for (int j = 0; j < m; j++)
             if (str[i + 1][j] == 'W') right1 = j;
         right = max(right, right1);
         ret += (right - cury);
         cury = right;
       } else {
         int left = cury, left1 = cury;
         for (int j = m - 1; j >= 0; j--)
           if (str[i][j] == 'W') left = j;
         if (i < n - 1)
           for (int j = m - 1; j >= 0; j--)
             if (str[i + 1][j] == 'W') left1 = j;
         left = min(left, left1);
         ret += (cury - left);
         cury = left;
       }
     }
     int row = 0;
     for (int i = 0; i < n; i++)
       for (int j = 0; j < m; j++)
         if (str[i][j] == 'W') row = i;
     ret += row;
     cout << ret << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int dis[3];
 int n;
 int main() {
   scanf('%d', &n);
   scanf('%d %d %d', &dis[0], &dis[1], &dis[2]);
   int ans = 0;
   n--;
   int idx = 0;
   while (n--) {
     if (dis[idx] < dis[(idx + 1) % 3]) {
       ans += dis[idx];
       idx = (idx + 2) % 3;
     } else {
       ans += dis[(idx + 1) % 3];
       idx = (idx + 1) % 3;
     }
   }
   printf('%d
', ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxl = 1000 * 100 + 5;
 int n, k;
 string s;
 int dis[30][maxl];
 int main() {
   cin >> n >> k >> s;
   memset(dis, -1, sizeof dis);
   for (int i = 0; i < 27; i++) {
     queue<int> q;
     for (int j = 0; j < k; j++)
       if (s[j] == (char)(i + 'a')) {
         q.push(j);
         dis[i][j] = 0;
       }
     while (!q.empty()) {
       int index = q.front();
       q.pop();
       if (index != 0)
         if (dis[i][index - 1] == -1) {
           dis[i][index - 1] = dis[i][index] + 1;
           q.push(index - 1);
         }
       if (index != maxl - 1)
         if (dis[i][index + 1] == -1) {
           dis[i][index + 1] = dis[i][index] + 1;
           q.push(index + 1);
         }
     }
   }
   for (int j = 0; j < n; j++) {
     string tmp;
     cin >> tmp;
     long long int ans = 0;
     for (int i = 0; i < tmp.size(); i++)
       ans += (dis[tmp[i] - 'a'][i] == -1 ? tmp.size() : dis[tmp[i] - 'a'][i]);
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     string s(200, 'a');
     cout << s << endl;
     for (int i = 0; i < n; i++) {
       int u;
       cin >> u;
       s[u] = s[u] == 'a' ? 'b' : 'a';
       cout << s << endl;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<vector<long long>> gr;
 vector<bool> used;
 vector<long long> tin, up;
 const long long Nmax = 400000;
 long long p[Nmax];
 long long sz[Nmax];
 long long timer = 0;
 long long diametr(long long u, vector<vector<long long>> gr) {
   queue<long long> q;
   long long n = (long long)(gr.size());
   vector<long long> h(n + 1, -1);
   q.push(u);
   h[u] = 0;
   long long ml = u;
   while (!q.empty()) {
     long long v = q.front();
     q.pop();
     for (long long x : gr[v]) {
       if (h[x] == -1) {
         h[x] = h[v] + 1;
         if (h[x] > h[ml]) ml = x;
         q.push(x);
       }
     }
   }
   vector<long long> prev(n + 1, -1);
   h.assign(n + 1, -1);
   q.push(ml);
   h[ml] = 0;
   long long ms = ml;
   prev[ml] = ml;
   while (!q.empty()) {
     long long v = q.front();
     q.pop();
     for (long long x : gr[v]) {
       if (prev[x] == -1) {
         h[x] = h[v] + 1;
         prev[x] = v;
         if (h[x] > h[ms]) ms = x;
         q.push(x);
       }
     }
   }
   vector<long long> res;
   while (prev[ms] != ms) {
     res.push_back(ms);
     ms = prev[ms];
   }
   res.push_back(ms);
   reverse((res).begin(), (res).end());
   return (long long)(res.size()) - 1;
 }
 long long find(long long x) {
   if (p[x] != x) {
     p[x] = find(p[x]);
   }
   return p[x];
 }
 void merge(long long x, long long y) {
   x = find(x);
   y = find(y);
   p[y] = x;
 }
 void restart(long long N) {
   for (long long i = 0; i < N; ++i) {
     sz[i] = 1;
     p[i] = i;
   }
 }
 vector<pair<long long, long long>> most;
 void dfs(long long u, long long p) {
   used[u] = true;
   tin[u] = timer++;
   up[u] = tin[u];
   for (long long v : gr[u]) {
     if (used[v] && v != p) up[u] = min(up[u], tin[v]);
     if (!used[v]) {
       dfs(v, u);
       up[u] = min(up[u], up[v]);
       if (tin[u] < up[v]) {
         most.emplace_back(u, v);
       } else {
         merge(u, v);
       }
     }
   }
 }
 void solve() {
   long long n, m;
   cin >> n >> m;
   gr.resize(n);
   tin.assign(n, 0);
   up.assign(n, 0);
   used.assign(n, 0);
   restart(n);
   for (long long i = (0); i < (m); ++i) {
     long long u, v;
     cin >> u >> v;
     u--;
     --v;
     gr[u].push_back(v);
     gr[v].push_back(u);
   }
   dfs(0, -1);
   vector<vector<long long>> gr2(n);
   if (most.empty()) {
     cout << 0;
     return;
   }
   for (long long i = (0); i < ((long long)(most.size())); ++i) {
     long long u = most[i].first, v = most[i].second;
     u = find(u);
     v = find(v);
     gr2[u].push_back(v);
     gr2[v].push_back(u);
   }
   long long v = most[0].first;
   v = find(v);
   cout << diametr(v, gr2);
 }
 signed main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   long long t;
   t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 #pragma GCC target('sse4')
 using namespace std;
 using ll = long long;
 using vi = vector<int>;
 using pii = pair<int, int>;
 const int MX = 1e5 + 10;
 template <typename T>
 istream &operator>>(istream &is, vector<T> &v) {
   for (auto i = begin(v); i != end(v); i++) is >> *i;
   return is;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const vector<T> &v) {
   for (auto i = begin(v); i != end(v); i++)
     os << *i << (i == end(v) - 1 ? '
' : ' ');
   return os;
 }
 template <typename T>
 inline void rd(T &s) {
   s = 0;
   T t = 1, k = getchar();
   for (; k < '0' || k > '9'; k = getchar())
     if (k == '-') t = -1;
   for (; k >= '0' && k <= '9'; k = getchar())
     s = (s << 1) + (s << 3) + (k ^ 48);
   s *= t;
 }
 ll n, m, t;
 char a[1111][1111];
 ll b[1111][1111];
 ll dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
 void solve() {
   cin >> n >> m >> t;
   for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i) cin >> (a[i] + 1);
   queue<pii> q;
   for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i)
     for (register ll j = 1, jlen = (ll)(m); j <= jlen; ++j) {
       if (a[i][j] == a[i + 1][j] || a[i][j] == a[i - 1][j] ||
           a[i][j] == a[i][j + 1] || a[i][j] == a[i][j - 1])
         b[i][j] = 1, q.push(make_pair(i, j));
       else
         b[i][j] = 2e18;
     }
   while (q.size()) {
     ll x = q.front().first, y = q.front().second;
     q.pop();
     for (register ll i = 0, ilen = (ll)(4); i < ilen; ++i) {
       ll nx = x + dx[i], ny = y + dy[i];
       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
         if (b[x][y] + 1 < b[nx][ny]) q.push(make_pair(nx, ny));
         b[nx][ny] = min(b[nx][ny], b[x][y] + 1);
       }
     }
   }
   while (t--) {
     ll x, y, p;
     cin >> x >> y >> p;
     if (p >= b[x][y] && (p - b[x][y]) % 2 == 0) {
       if (a[x][y] == '1')
         cout << '0' << '
';
       else
         cout << '1' << '
';
     } else
       cout << a[x][y] << '
';
   }
 }
 signed main() {
   ll T = 1;
   while (T--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = (int)1e6 + 5;
 void compute() {
   int n, m;
   scanf('%d %d', &n, &m);
   vector<int> v(n);
   for (int i = 0; i < n; ++i) scanf('%d', &v[i]);
   vector<pair<int, int> > seg(m);
   vector<vector<int> > lf(n), rg(n);
   for (int i = 0; i < m; ++i) {
     scanf('%d%d', &seg[i].first, &seg[i].second);
     --seg[i].first;
     --seg[i].second;
     lf[seg[i].second].push_back(seg[i].first);
     rg[seg[i].first].push_back(seg[i].second);
   }
   vector<int> add(n, 0);
   vector<int> ansv(n, -inf);
   int mn = v[0];
   for (int i = 0; i < n; ++i) {
     ansv[i] = max(ansv[i], v[i] - mn);
     for (auto l : lf[i]) {
       for (int j = l; j <= i; ++j) {
         --add[j];
         mn = min(mn, v[j] + add[j]);
       }
     }
     mn = min(mn, v[i] + add[i]);
   }
   add = vector<int>(n, 0);
   mn = v[n - 1];
   for (int i = n - 1; i >= 0; --i) {
     ansv[i] = max(ansv[i], v[i] - mn);
     for (auto r : rg[i]) {
       for (int j = i; j <= r; ++j) {
         --add[j];
         mn = min(mn, v[j] + add[j]);
       }
     }
     mn = min(mn, v[i] + add[i]);
   }
   int res = *max_element(ansv.begin(), ansv.end());
   vector<int> res2;
   for (int i = 0; i < n; ++i) {
     if (ansv[i] == res) {
       for (int j = 0; j < m; ++j) {
         if (!(seg[j].first <= i && seg[j].second >= i)) res2.push_back(j + 1);
       }
       break;
     }
   }
   printf('%d
%d
', res, (int)res2.size());
   for (auto i : res2) printf('%d ', i);
   printf('
');
 }
 int main() {
   compute();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, k;
   bool odd = false, even = false;
   cin >> n >> k;
   if (n & 1) {
     if (k > (n / 2 + 1)) {
       even = true;
       k -= (n / 2 + 1);
     } else
       odd = true;
   } else {
     if (k > n / 2) {
       even = true;
       k -= (n / 2);
     } else
       odd = true;
   }
   if (odd)
     cout << 2 * k - 1 << endl;
   else
     cout << 2 * k << endl;
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize(3)
 using namespace std;
 bool Finish_read;
 template <class T>
 inline void read(T &x) {
   Finish_read = 0;
   x = 0;
   int f = 1;
   char ch = getchar();
   while (!isdigit(ch)) {
     if (ch == '-') f = -1;
     if (ch == EOF) return;
     ch = getchar();
   }
   while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
   x *= f;
   Finish_read = 1;
 }
 template <class T>
 inline void print(T x) {
   if (x / 10 != 0) print(x / 10);
   putchar(x % 10 + '0');
 }
 template <class T>
 inline void writeln(T x) {
   if (x < 0) putchar('-');
   x = abs(x);
   print(x);
   putchar('
');
 }
 template <class T>
 inline void write(T x) {
   if (x < 0) putchar('-');
   x = abs(x);
   print(x);
 }
 const int maxn = 100005, maxnode = 16500005;
 int mx, my, mz, n, m, k, lx, rx, ly, ry, lz, rz, cnt, lc[maxnode], rc[maxnode],
     lv[maxnode], rv[maxnode], rt[maxn << 2];
 inline bool Open(int x, int y, int z) {
   return lx <= x && x <= rx && ly <= y && y <= ry && lz <= z && z <= rz;
 }
 inline void update(int &o, int l, int r, int y, int z) {
   if (!o) o = ++cnt, rv[o] = mz + 1;
   if (lz <= z) rv[o] = min(rv[o], z);
   if (z <= rz) lv[o] = max(lv[o], z);
   if (l == r) return;
   int mid = (l + r) >> 1;
   if (y <= mid)
     update(lc[o], l, mid, y, z);
   else
     update(rc[o], mid + 1, r, y, z);
 }
 inline void modify(int o, int l, int r, int x, int y, int z) {
   update(rt[o], 1, my, y, z);
   if (l == r) return;
   int mid = (l + r) >> 1;
   if (x <= mid)
     modify(o << 1, l, mid, x, y, z);
   else
     modify(o << 1 | 1, mid + 1, r, x, y, z);
 }
 inline bool getans(int o, int l, int r, int yl, int yr, int zl, int zr) {
   if (!o) return 1;
   if (yl <= l && r <= yr) return lv[o] < zl && zr < rv[o];
   bool res = 1;
   int mid = (l + r) >> 1;
   if (yl <= mid) res &= getans(lc[o], l, mid, yl, yr, zl, zr);
   if (yr > mid) res &= getans(rc[o], mid + 1, r, yl, yr, zl, zr);
   return res;
 }
 inline bool query(int o, int l, int r, int xl, int xr, int yl, int yr, int zl,
                   int zr) {
   if (xl <= l && r <= xr) return getans(rt[o], 1, my, yl, yr, zl, zr);
   bool res = 1;
   int mid = (l + r) >> 1;
   if (xl <= mid) res &= query(o << 1, l, mid, xl, xr, yl, yr, zl, zr);
   if (xr > mid) res &= query(o << 1 | 1, mid + 1, r, xl, xr, yl, yr, zl, zr);
   return res;
 }
 inline void solve(int x, int y, int z) {
   if (Open(x, y, z)) return (void)puts('OPEN');
   if (query(1, 1, mx, min(lx, x), max(rx, x), min(ly, y), max(ry, y),
             min(lz, z), max(rz, z)))
     return (void)puts('UNKNOWN');
   return (void)puts('CLOSED');
 }
 int main() {
   read(mx), read(my), read(mz), read(n), read(m), read(k);
   lx = mx, ly = my, lz = mz;
   for (int i = 1, x, y, z; i <= n; ++i) {
     read(x), read(y), read(z);
     lx = min(lx, x), rx = max(rx, x);
     ly = min(ly, y), ry = max(ry, y);
     lz = min(lz, z), rz = max(rz, z);
   }
   for (int i = 1, x, y, z; i <= m; ++i) {
     read(x), read(y), read(z);
     if (Open(x, y, z)) return 0 * puts('INCORRECT');
     modify(1, 1, mx, x, y, z);
   }
   puts('CORRECT');
   for (int i = 1, x, y, z; i <= k; ++i)
     read(x), read(y), read(z), solve(x, y, z);
 }


#include <bits/stdc++.h>
 using namespace std;
 long long a[(200020)], b[(200020)], close[100][2];
 set<pair<long long, long long> > vis;
 signed main() {
   long long n;
   cin >> n;
   for (long long i = 1; i <= n; i++) scanf('%lld', &a[i]);
   for (long long i = 1; i <= n; i++) b[i] = b[i - 1] + a[i];
   long long ans = 0;
   for (long long i = 1; i <= n; i++) {
     if (i >= 3)
       for (long long k = 0; k < 30; k++)
         if (!(a[i] & (1 << k))) {
           if (close[k][0] && i - close[k][0] > 1) {
             if ((a[i] ^ a[close[k][0]]) == b[i - 1] - b[close[k][0]]) {
               if (vis.find(make_pair(close[k][0], i)) != vis.end()) continue;
               ans++;
               vis.insert(make_pair(close[k][0], i));
             }
           }
           if (close[k][1] && i - close[k][1] > 1) {
             if ((a[i] ^ a[close[k][1]]) == b[i - 1] - b[close[k][1]]) {
               if (vis.find(make_pair(close[k][1], i)) != vis.end()) continue;
               ans++;
               vis.insert(make_pair(close[k][1], i));
             }
           }
         }
     for (long long k = 0; k < 30; k++) {
       if (a[i] & (1 << k)) {
         close[k][1] = close[k][0];
         close[k][0] = i;
       }
     }
   }
   memset(close, 0, sizeof(close));
   for (long long i = n; i >= 1; i--) {
     if (n - i + 1 >= 3)
       for (long long k = 0; k < 30; k++)
         if (!(a[i] & (1 << k))) {
           if (close[k][0] && close[k][0] - i > 1) {
             if ((a[i] ^ a[close[k][0]]) == b[close[k][0] - 1] - b[i]) {
               if (vis.find(make_pair(i, close[k][0])) != vis.end()) continue;
               ans++;
               vis.insert(make_pair(i, close[k][0]));
             }
           }
           if (close[k][1] && close[k][1] - i > 1) {
             if ((a[i] ^ a[close[k][1]]) == b[close[k][1] - 1] - b[i]) {
               if (vis.find(make_pair(i, close[k][1])) != vis.end()) continue;
               ans++;
               vis.insert(make_pair(i, close[k][1]));
             }
           }
         }
     for (long long k = 0; k < 30; k++) {
       if (a[i] & (1 << k)) {
         close[k][1] = close[k][0];
         close[k][0] = i;
       }
     }
   }
   cout << ans << endl;
   return 0;
 }


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


#include <bits/stdc++.h>
 using namespace std;
 const int N = 105;
 int freq[N];
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   int n, m;
   cin >> n >> m;
   for (int i = 0; i < m; i++) {
     int type;
     cin >> type;
     freq[type]++;
   }
   int start = 1, end = 100;
   while (start <= end) {
     int mid = (start + end) >> 1;
     int persons = 0;
     for (int types = 1; types <= 100; types++) {
       persons += (freq[types] / mid);
     }
     if (persons >= n)
       start = mid + 1;
     else
       end = mid - 1;
   }
   cout << end << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int32_t main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   long long TC = 1;
   while (TC--) {
     long long n;
     cin >> n;
     vector<long long> a(n);
     for (long long i = 0; i < n; i++) cin >> a[i];
     long long lcnt = 1, rcnt = 1;
     for (long long i = 1; i < n; i++) {
       if (a[i] > a[i - 1])
         lcnt++;
       else
         break;
     }
     for (long long i = n - 1 - 1; i >= 0; i--) {
       if (a[i] > a[i + 1])
         rcnt++;
       else
         break;
     }
     if ((a[0] >= a[n - 1] && lcnt % 2) || (a[0] <= a[n - 1] && rcnt % 2)) {
       cout << 'Alice
';
       continue;
     } else if (lcnt % 2 == 0 && rcnt % 2 == 0) {
       cout << 'Bob
';
     } else {
       cout << 'Alice
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 100005;
 const double eps = 1.0e-8;
 const double pi = acos(-1.0);
 complex<double> p[MAXN], P;
 double cross(complex<double> a, complex<double> b) {
   return (conj(a) * b).imag();
 }
 double check(complex<double> a, complex<double> b) {
   double l = 0, r = 1;
   complex<double> v = b - a;
   for (int i = 0; i < 50; i++) {
     double mid = (l + r) * 0.5;
     double midd = (mid + r) * 0.5;
     complex<double> x = a + v * mid, y = a + v * midd;
     if (abs(P - x) + eps < abs(P - y)) {
       r = midd;
     } else {
       l = mid;
     }
   }
   return abs(P - (a + v * l));
 }
 int main() {
   int n;
   cin >> n;
   int x, y;
   cin >> x >> y;
   P = complex<double>(x, y);
   for (int i = 0; i < n; i++) {
     scanf('%d %d', &x, &y);
     p[i] = complex<double>(x, y);
   }
   p[n] = p[0];
   double r1 = 1.0e20, r2 = 0;
   for (int i = 0; i < n; i++) {
     r1 = min(r1, check(p[i], p[i + 1]));
     r2 = max(r2, abs(P - p[i]));
   }
   printf('%.10f
', pi * (r2 * r2 - r1 * r1));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 100000;
 int f[MAXN + 5];
 int main() {
   memset(f, 0, sizeof(f));
   int m = 0, a = 0, r0 = 0, b = 0, ans = 1;
   cin >> a >> b >> m >> r0;
   f[r0] = 1;
   for (int i = 2;; ++i) {
     r0 = (a * r0 + b) % m;
     ++ans;
     if (f[r0] != 0) {
       ans = ans - f[r0];
       break;
     }
     f[r0] = ans;
   }
   cout << ans << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   int n, m, count = 0;
   cin >> n >> m;
   int x = min(n, m);
   n = n - x;
   m = m - x;
   int rest = (n / 2) + (m / 2);
   cout << x << ' ' << rest << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   int t;
   cin >> t;
   while (t--) {
     unordered_map<char, int> mp;
     int n;
     cin >> n;
     string arr[n];
     for (int i = 0; i < n; i++) {
       cin >> arr[i];
       for (auto c : arr[i]) {
         mp[c]++;
       }
     }
     bool ok = true;
     for (auto p : mp) {
       if (p.second % n != 0) {
         ok = false;
         break;
       }
     }
     cout << ((ok) ? 'YES
' : 'NO
');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long int a, b, c, l;
   cin >> a >> b >> c >> l;
   long long int Total = (l + 1) * (l + 2) * (l + 3) / 6;
   for (long long int la = 0; la <= l; la++) {
     long long int mini = min(a - b - c + la, l - la);
     if (mini < 0) continue;
     long long int wrong = (mini + 1) * (mini + 2) / 2;
     Total -= wrong;
   }
   for (long long int lb = 0; lb <= l; lb++) {
     long long int mini = min(b - a - c + lb, l - lb);
     if (mini < 0) continue;
     long long int wrong = (mini + 1) * (mini + 2) / 2;
     Total -= wrong;
   }
   for (long long int lc = 0; lc <= l; lc++) {
     long long int mini = min(c - b - a + lc, l - lc);
     if (mini < 0) continue;
     long long int wrong = (mini + 1) * (mini + 2) / 2;
     Total -= wrong;
   }
   cout << Total << endl;
   return 0;
 }