#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>

#include <fstream>

using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

struct edge{ll to,cost; };
vector<edge> G[500010];
ll d[500010];

void dijkstra(ll s){
    fill(d,d+500010,inf);
    d[s]=0;
    priority_queue<l_l,vector<l_l>,greater<l_l>> que;
    que.push(l_l(0,s));
    while(!que.empty()){
        l_l p=que.top();que.pop();
        ll v=p.second;
        if(d[v]<p.first)continue;
        if(G[v].size()>0){
            rep(i,0,G[v].size()-1){
                edge e=G[v][i];
                if(d[e.to]>d[v]+e.cost){
                    d[e.to]=d[v]+e.cost;
                    que.push(l_l(d[e.to],e.to));
                }
            }
        }
    }
}

int main(){fastio
    ll n,m;cin>>n>>m;
    ll a[m],b[m];
    rep(i,0,m-1){
        cin>>a[i]>>b[i];
        edge e;
        e.to=b[i];e.cost=1;
        G[a[i]].pb(e);
        e.to=a[i];e.cost=1;
        G[b[i]].pb(e);
    }
    ll k;cin>>k;
    ll c[k];rep(i,0,k-1)cin>>c[i];
    ll l[k][k]; //c[i]からc[j]への最短距離
    rep(i,0,k-1){
        dijkstra(c[i]);
        rep(j,0,k-1){
            l[i][j] = d[c[j]];
        }
    }
    /*
    rep(i,0,k-1){
        rep(j,0,k-1){
            cout<<l[i][j]<<' ';
        }
        cout<<endl;
    }
    */
    ll L=(1<<k);
    ll dp[L][k];rep(i,0,L-1)rep(j,0,k-1)dp[i][j]=inf;
    rep(i,0,k-1){
        dp[(1<<i)][i]=0;
    }
    rep(i,0,L-1){
        rep(j,0,k-1){
            if(!((i>>j)&1)){
                continue;
            }
            ll M=i-(1<<j);
            rep(ii,0,k-1){
                if((M>>ii)&1){
                    dp[i][j] = min(dp[i][j],dp[M][ii]+l[ii][j]);
                }
            }
        }
    }
    ll ans=inf;
    rep(i,0,k-1){
        ans=min(ans,dp[L-1][i]);
    }
    if(ans>=inf){
        cout<<-1<<endl;
    }else{
        cout<<ans+1<<endl;
    }

    return 0;

}

/*************************************************************************
  > File Name: solve.cpp
  > Author: liupo
  > Mail: lanzongwei@gmail.com
  > Created Time: 2021-02-28 21:37:24
 ************************************************************************/

#define GOODOJ
#define SYNC 0

#ifdef GOODOJ
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <chrono>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
#include <limits>
#include <cassert>
#include <sstream>
#include <iterator>
#include <functional>
#endif
using namespace std;

#define endl '\n'
#define fep(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,x) for(int i=0;i<(x);++i)
#define rap(i,x) for(auto& i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define qxx(i,x) for(int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x))&(x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << ' ';
	err(++it, args...);
}
#define debug(args...) {string _s=#args;replace(seg(_s),',',' ');\
	cerr<<"DEBUG:";istringstream it(_s);\
	err(istream_iterator<string>(it), args);cerr<<endl;}
#else
#define debug(...)
#endif

template<typename T> inline bool cmax(T& a,const T& b) {return a<b?a=b,1:0;}
template<typename T> inline bool cmin(T& a,const T& b) {return a>b?a=b,1:0;}

#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;

const double P = acos(-1.0), eps = 1e-9;
struct point { db x ,y;};
inline int sign(db a) {return a < -eps ? -1 : a > eps;}
#define dot(p1,p2,p3) ((p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y))
#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))

constexpr int Ma = 1e6, inf = 0x3f3f3f3f, mod = 1e9 + 7;

int who[200];

void solve() {
	str s[3]; rep (i, 3) cin >> s[i];
	set<int> cnt;
	rep (i, 3) rap (j, s[i]) cnt.emplace(j);
	if (cnt.size() > 10) {
		cout << "UNSOLVABLE" << endl;
		return ;
	}
	vector<int> id(10);
	iota(seg(id), 0);
	int ct = 0;
	rap (i, cnt) who[i] = ct++;
	do {
		bool show = true;
		rep (i, 3)
			if (id[who[s[i][0]]] == 0) {
				show = false;
				break;
			}
		if (not show) continue;
		ll num[3] = {0, 0, 0};
		rep (i, 3) {
			rap (j, s[i]) num[i] *= 10, num[i] += id[who[j]];
		}
		if (num[0] + num[1] == num[2]) {
			cout << num[0] << endl << num[1] << endl << num[2] << endl;
			return ;
		}
	} while (next_permutation(seg(id)));
	cout << "UNSOLVABLE" << endl;
}

signed main() {
#if SYNC==0
	ios::sync_with_stdio(false);
	cin.tie(0);
#endif
	int T = 1;
	while (T--) solve();

	return 0;
}


#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,cc,n) for(int i=cc;i<(n);++i)
#define dreps(i,cc,n) for(int i=cc;i>(n);--i)
#define llreps(i,cc,n) for(long long i=cc;i<(n);++i)
#define ALL(v) begin(v),end(v)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

int main()
{
	int a,b,c;
	cin >> a >> b >> c;
	if (a*a + b * b < c*c)
	{
		cout << "Yes" << endl;
		return 0;
	}
	cout << "No" << endl;
	return 0;
}

#include <iostream>
using namespace std;
int main(){
	int a,b,c;
	cin>>a>>b>>c;
	if (a==b){
		cout<<c;
		return 0;
	}

	if (a==c){
		cout<<b;
		return 0;
	}
	if (c==b){
		cout<<a;
		return 0;
	}
	cout<<0;
}


// ABC183C
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <string>
#include <regex>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <vector>
#include <set>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, m) for(int i = (int)(n); i < (int)(m); i++)
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))

using ll = long long;
using namespace std;

int main()
{
    int V, T, S, D;
    cin >> V >> T >> S >> D;

    if ((D) == (T * V)) {
        cout << "No" << endl;
    } else if (((D) > (T * V)) && ((D) < (S * V))) {
        cout << "No" << endl;
    } else if ((D) == (S * V)) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }

    return 0;
}

#include <iostream>
#include<bits/stdc++.h>
#include <string>
using namespace std;
int main(void){
    int h,w,x,y;
    std::cin >> h >> w >> x >> y;
    x -= 1;
    y -= 1;
    string s[h];

    for (int i= 0;i<h;i++){
        std::cin >> s[i];
    }

    int cnt = -3;
    // down
    for (int i = x; i < h && s[i][y] != '#'; i++){
        cnt++;
    }
    // up
    for (int i = x; i >= 0 && s[i][y] != '#'; i--){
        cnt++;
    }

    // right
    for (int i = y; i < w && s[x][i] != '#'; i++){
        cnt++;
    }
    // left
    for (int i = y; i >= 0 && s[x][i] != '#'; i--){
        cnt++;
    }

    std::cout << cnt << std::endl;
}


#include <bits/stdc++.h>
 using namespace std;
 typedef long long ll;
 typedef vector<long long> vll;
 //A
 /*int main(){
     int n; cin >> n;
     int x=n%100;
     if(n==0) { cout << 100 << endl; return 0; }
     cout << 100-x << endl;
     return 0;
 } */
 //B
 /*int main(){
     string s; cin >> s;
     bool c=1;
     for(int i=0; 2*i<s.size(); i++){
         if(s[2*i] >='a' && s[2*i]<='z' ) c=1;
         else { cout << "No"<< endl; return 0; }
    }
     for(int i=0; 2*i+1<s.size(); i++){
         if(s[2*i+1] >='A' && s[2*i+1]<='Z' ) c=1;
         else { cout << "No" << endl; return 0;}
    }
    cout << "Yes" << endl;
    return 0;
 } */
 //C
 string convert (ll n){
    ostringstream buff;
    buff<<n;
    return buff.str();
}
 string f(string x){
     vector <char> v;
     int a=x.size();
     for(int i=0; i<a; i++) v.push_back(x[i]);
     ll g1=0,g2=0;
     sort(v.begin(),v.end());
     for(int i=0; i<a; i++) g2+=(v[i]*pow(10,a-i-1));
     sort(v.rbegin(),v.rend());
     for(int i=0; i<a; i++) {
         if(v[i] != 0 ) g1+=( v[i]*pow(10,a-i-1));
         else { g1+=pow(10,a-i); break; }
     }
     return convert(g1-g2);
 }
 int main(){
     string N; cin >> N;
     int K; cin >> K;
     string res=N;
     for(int i=1; i<=K; i++) res=f(res);
     cout << res << endl;
     return 0;
 }

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

int main() {
  int n,k;
  cin>>n>>k;

  for(int i=0;i<k;i++){
    string s1=to_string(n);
    string s2=to_string(n);
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end(),greater<char>());
    int tmp1=stoi(s1);
    int tmp2=stoi(s2);
    n=tmp2-tmp1;
  }
  cout<<n<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 200005;
ll sum[maxn << 2];
ll lzd[maxn << 2];
ll lzc[maxn << 2];
vector<pair<ll,ll>>v;
void pushup(int x) {
    sum[x] = sum[x<<1] + sum[x<<1|1];
}
void pushdown(int x, int l, int r) {
    int mid = (l + r) >> 1;
    if (lzc[x]) {
        lzc[x<<1] = lzc[x<<1|1] = lzc[x];
        lzc[x] = 0;
        lzd[x<<1] = lzd[x<<1|1] = 0;
        sum[x<<1] = (mid - l + 1) * lzc[x<<1];
        sum[x<<1|1] = (r - mid) * lzc[x<<1|1];
    }
    if (lzd[x]) {
        lzd[x<<1] += lzd[x];
        lzd[x<<1|1] += lzd[x];
        sum[x<<1] += lzd[x] * (mid - l + 1);
        sum[x<<1|1] += lzd[x] * (r - mid);
        lzd[x] = 0;
    }
}
void build(int x, int l, int r) {
    if (l == r) {
        sum[x] = v[l].F;
        return;
    }
    int mid = (l + r) >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    pushup(x);
}
void update(int x, int l, int r, int ql, int qr, ll v) {
    if (ql <= l && qr >= r) {
        lzd[x] += v;
        sum[x] += v * (r - l + 1);
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(x,l,r);
    if (ql <= mid) {
        update(x<<1,l,mid,ql,qr,v);
    }
    if (qr > mid) {
        update(x<<1|1,mid+1,r,ql,qr,v);
    }
    pushup(x);
}
void update2(int x, int l, int r, int ql, int qr, ll v) {
    if (ql <= l && qr >= r) {
        lzc[x] = v;
        lzd[x] = 0;
        sum[x] = v * (r - l + 1);
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(x,l,r);
    if (ql <= mid) {
        update2(x<<1,l,mid,ql,qr,v);
    }
    if (qr > mid) {
        update2(x<<1|1,mid+1,r,ql,qr,v);
    }
    pushup(x);
}
ll query(int x, int l, int r, int ql, int qr) {
    if (ql <= l && qr >= r) {
        return sum[x];
    }
    int mid = (l + r) >> 1;
    ll ret = 0;
    pushdown(x,l,r);
    if (ql <= mid) {
        ret += query(x<<1,l,mid,ql,qr);
    }
    if (qr > mid) {
        ret += query(x<<1|1,mid+1,r,ql,qr);
    }
    pushup(x);
    return ret;
}
void solve() {
    int m;
    cin >> m;
    vector<pair<ll,ll>>op;
    f(m) {
        ll a, b;
        cin >> a >> b;
        op.pb({a, b});
    }
    int n;
    cin >> n;
    f1 (n) {
        int a;
        cin >> a;
        v.pb({a, i});
    }
    v.pb({-1e10, -1});
    sort(all(v));
    build(1,1,n);
    for (auto &i : op) {
        if (i.S == 1) {
            update(1,1,n,1, n,i.F);
        }
        else if (i.S == 2) {
            if (query(1,1,n,1,1) >= i.F) {
                continue;
            }
            int l = 1, r = n + 1;
            while (r - l > 1) {
                int mid = (l + r) >> 1;
                if (query(1,1,n,mid,mid) < i.F) {
                     l = mid;
                }
                else {
                    r = mid;
                }
            }
            update2(1,1,n,1,l,i.F);
        }
        else {
            if (query(1,1,n,n,n) <= i.F) {
                continue;
            }
            int l = 0, r = n ;
            while (r - l > 1) {
                int mid = (l + r) >> 1;
                if (query(1,1,n,mid,mid) > i.F) {
                    r = mid;
                }
                else {
                    l = mid;
                }
            }
            update2(1,1,n,r,n,i.F);
        }
    }
    ll ans[n + 5] = {};
    f1(n) {
        ans[v[i].S] = query(1,1,n,i,i);
    }
    f1(n) {
        cout << ans[i] << '\n';
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n , a [200005] , t [200005] , q , x [200005] , ans [200005] , ad [200005] , anss [200005];
deque < pair < long long , long long > > dq;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) cin >> a [i] >> t [i];
    cin >> q;
    for ( int i = 0 ; i < q ; i ++ )
    {
        cin >> x [i];
        dq . push_back ( { x [i] , i } );
    }
    sort ( dq . begin () , dq . end () );
    long long sum = 0;
    multiset < pair < long long , long long > > ms;

    for ( int i = 0 ; i < n ; i ++ )
    {
        if ( t [i] == 1 ) sum += a [i];
        if ( t [i] == 2 )
        {
            long long l = a [i];
            while ( ms . size () )
            {
                pair < long long , long long > p = * ms . begin ();
                if ( p . first + sum <= l )
                {
                    ad [ p . second ] = i + 1;
                    ms . erase ( ms . begin () );
                }
                else break;
            }
            ms . insert ( { a [i] - sum , i } );
        }
        if ( t [i] == 3 )
        {
            long long r = a [i];
            while ( ms . size () )
            {
                pair < long long , long long > p = * -- ms . end ();
                if ( r <= p . first + sum )
                {
                    ad [ p . second ] = i + 1;
                    ms . erase ( -- ms . end () );
                }
                else break;
            }
            ms . insert ( { a [i] - sum , i } );
        }
    }

    sum = 0;
    for ( int i = 0 ; i < n ; i ++ )
    {
        if ( dq . size () == 0 ) break;
        if ( t [i] == 1 ) sum += a [i];
        if ( t [i] == 2 )
        {
            long long l = a [i];
            while ( dq [0] . first + sum <= l )
            {
                ans [ dq [0] . second ] = i + 1;
                dq . pop_front ();
                if ( dq . size () == 0 ) break;
            }
        }
        if ( t [i] == 3 )
        {
            long long r = a [i];
            while ( r <= dq [ dq . size () - 1 ] . first + sum )
            {
                ans [ dq [ dq . size () - 1 ] . second ] = i + 1;
                dq . pop_back ();
                if ( dq . size () == 0 ) break;
            }
        }
    }
    sum = 0;
    for ( int i = n - 1 ; i >= 0 ; i -- )
    {
        if ( t [i] == 1 ) sum += a [i];
        if ( ad [i] == 0 ) anss [i] = a [i] + sum;
        else anss [i] = anss [ ad [i] - 1 ];
    }
    for ( int i = 0 ; i < q ; i ++ )
    {
        if ( ans [i] ) cout << anss [ ans [i] - 1 ] << endl;
        else cout << x [i] + sum << endl;
    }
}


#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch: s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    }
    else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) {
  return tolower(c) - 'a';
}

int compare_array(vector<int> a1, vector<int>a2) {
  int n1 = a1.size();
  int n2 = a2.size();
  if (n1 != n2) {
    return n1 - n2;
  }
  for (int i=0; i<n1; i++) {
    if (a1.at(i) != a2.at(i)) {
      return a1.at(i) - a2.at(i);
    }
  }
  return 0;
}

int gcd(int a, int b) {
  if(a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

char int_to_char(int a) {
  if (a == -1) {
    return 'z';
  }
  else {
    return 'a' + a;
  }
}

long nCr(int n, int r) {
  long ans = 1;
  for (int i = n; i > n - r; --i) {
    ans = ans*i;
  }
  for (int i = 1 ; i < r + 1; ++i) {
    ans = ans / i;
  }
  return ans;
}

long modinv(long a, long m) {
  long b = m, u = 1, v = 0;
  while (b) {
    long t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

int divide_count(int a, int divider) {
  int r = 0;
  while(a % divider == 0) {
    a /= divider;
    r++;
  }
  return r;
}

bool is_prime(int a) {
  int i = 2;
  while(i * i <= a) {
    if(a % i == 0) {
      return false;
    }
    i++;
  }
  return true;
}

vector<vector<int>> all_comb(int n, int k) {
  vector<vector<int>> combs(nCr(n, k), vector<int>(k));
  for(int i=0; i<k; i++) {
    combs[0][i] = i;
    combs[1][i] = i;
  }

  for(long i=1; i<nCr(n, k); i++) {
    int p = 1;
    while(combs[i][k - p] == n - p) {
      p++;
      if(p > k) {
        break;
      }
    }
    combs[i][k - p]++;
    int q = combs[i][k - p];
    for(int j=1; j<p; j++) {
      combs[i][k - p + j] = q + j;
    }

    if(i < nCr(n, k) - 1) {
      for(int j=0; j<k; j++) {
        combs[i + 1][j] = combs[i][j];
      }
    }
  }

  return combs;
}

struct UnionFind {
  vector<int> parent;

  UnionFind(int n) : parent(n) {
    for(int i=0; i<n; i++) {
      parent[i] = i;
    }
  }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    }
    return parent[x] = root(parent[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if(rx == ry) return;
    parent[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

template <typename TYPE> void co(TYPE a) {
  cout << a << endl;
}

template <typename TYPE> void co_2(TYPE a, TYPE b) {
  cout << a << ' ' << b << endl;
}

template <typename TYPE> void co_l(vector<TYPE> as) {
  int n = as.size();
  for(int i=0; i<n; i++) {
    cout << as[i] << endl;
  }
}

template <typename TYPE> void co_s(vector<TYPE> as) {
  int n = as.size();
  for(int i=0; i<n; i++) {
    if(i > 0) {
      cout << ' ';
    }
    cout << as[i];
  }
  cout << endl;
}

int main() {
  std::cout << std::setprecision(9);
  long mod = 1000000007;
  int x;
  cin >> x;
  if(x >= 0) {
    co(x);
  }
  else {
    co(0);
  }
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
const long long MOD2 = 998244353LL;
const long long MOD = 1000000007LL;
const long long INF = 1LL<<60;

int main(){
   int x;
   cin >> x;
   if(x >= 0)cout << x << endl;
   else cout << 0 << endl;
   return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using Tiib = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[0].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){
    return (a <= x && x < b);
}

void yes(){
    cout << "Yes" << endl;
}

void no (){
    cout << "No" << endl;
}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

ll len(vector<PL> &ar, ll kg){
    int l = ar.size();
    if (kg < ar[0].first) return 0LL;
    if (kg >= ar[l-1].first) return ar[l-1].second;
    int le = 0;
    int ri = l-1;
    int mid = (le + ri) / 2;
    while (true){
        if (ar[mid].first <= kg && kg < ar[mid+1].first) return ar[mid].second;
        if (kg < ar[mid].first) ri = mid;
        else le = mid;
        mid = (le + ri) / 2;
    }
}

int main(){
    int t; cin >> t;
    vector<string> ans(t);
    rep(i,t){
        int n; cin >> n;
        map<int,int> m;
        map<int,int> ::iterator ite;;
        rep(i,n){
            int a; cin >> a;
            if (m.find(a) == m.end()) m[a] = 1;
            else m[a]++;
        }
        if (n % 2 == 1) ans[i] = "Second";
        else {
            bool t = true;
            for (ite = m.begin(); ite != m.end(); ite++){
                if (ite->second % 2 == 1) t = false;
            }
            if (t) ans[i] = "Second";
            else ans[i] = "First";
        }
    }
    rep(i,t) cout << ans[i] <<endl;
}

#include<cstdio>
#include<cctype>
#include<set>
#include<map>
using namespace std;
typedef map<int,int>::iterator Mit;
template<class T>
void read(T& x)
{
    x=0;
    int f=1,ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=x*10+ch-'0';
        ch=getchar();
    }
    x*=f;
}
int t,n;
set<int> a;
map<int,int> b;
int main()
{
    read(t);
    while(t--)
    {
        read(n);
        int x=0,y;
        b.clear();
        for(int i=0;i<n;i++)
        {
            read(y);
            b[y]++;
        }
        Mit it=b.begin();
        for(it;it!=b.end();it++)
            if(it->second&1) {
                x=1;
                break;
            }
        if(x^(n&1)) puts("First");
        else puts("Second");
    }
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 1500000
using namespace std;
vector<int>pos[MAXN + 1];
int main() {
    int m, n;
    cin>>n>>m;
    for(int i=0; i<=n; i++)
        pos[i].push_back(-1);
    for(int i=0; i<n; i++) {
        int x;
        cin>>x;
        pos[x].push_back(i);
    }
    for(int i=0; i<=n; i++)
        pos[i].push_back(n);
    for(int ans=0; ans<=n; ans++) {
        for(int i=1; i<pos[ans].size(); i++) {
            if (pos[ans][i] - pos[ans][i-1] > m) {
                cout<<ans<<endl;
                return 0;
            }
        }
    }
    cout<<n+1<<endl;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1500010

using namespace std;

int n, m;
int tr[4*N], a[N], ans;

void ins(int i, int x, int k, int l, int r){
	if (l==r){
		tr[i]+=k;
		return;
	}
	int mid=(l+r)/2;
	if (x<=mid) ins(i<<1, x, k, l, mid);
	else ins(i<<1|1, x, k, mid+1, r);
	if (!tr[i<<1] || !tr[i<<1|1]) tr[i]=0;
	else tr[i]=1;
}

int find(int i, int l, int r){
	if (l==r) return l;
	int mid=(l+r)/2;
	if (!tr[i<<1]) return find(i<<1, l, mid);
	else return find(i<<1|1, mid+1, r);
}

int main(){
	scanf("%d%d", &n, &m);
	for (int i=1; i<=n; i++) scanf("%d", &a[i]);
	for (int i=1; i<=m; i++) ins(1, a[i], 1, 0, n);
	ans=find(1, 0, n);
	for (int i=m+1; i<=n; i++){
		ins(1, a[i], 1, 0, n);
		ins(1, a[i-m], -1, 0, n);
		ans=min(find(1, 0, n), ans);
	}
	printf("%d", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<long long, long long> pll;

typedef pair<long long, pll> plp;

priority_queue<plp, vector<plp>, greater<plp> > q;

long long n, m, a[507][507], b[507][507], vis[507][507], dist[507][507], v, x, y;

void check(long long x, long long y, long long d)
{
    if(x==0 || y==0 || x>n || y>m) return;
    if(dist[x][y]<=d) return;
    dist[x][y]=d;
    q.push({d, {x, y}});
    return;
}

int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m-1; j++)
        {
            dist[i][j]=1000000000000000;
            cin>>a[i][j];
        }
    }
    for(int i=1; i<=n-1; i++)
    {
        for(int j=1; j<=m; j++)
        {
            dist[i][j]=100000000000000;
            cin>>b[i][j];
        }
    }
    dist[1][1]=0;
    dist[n][m]=100000000000000000;
    q.push({0, {1, 1}});
    while(!q.empty())
    {
        v=q.top().first;
        x=q.top().second.first;
        y=q.top().second.second;
        q.pop();
        if(vis[x][y]==1) continue;
        vis[x][y]=1;
        check(x, y+1, a[x][y]+v);
        check(x, y-1, a[x][y-1]+v);
        check(x+1, y, b[x][y]+v);
        for(int i=1; i<x; i++)
        {
            check(x-i, y, 1+i+v);
        }
    }
    cout<<dist[n][m];
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using TIII = tuple<int, int, int>;

template<class T> inline bool chmin(T& a, T b) {
  return (a > b) ? (a = b, true) : false;
}

const int INF = 1 << 30;

int main(void)
{
  int R, C; cin >> R >> C;
  vector<vector<int>> A(R, vector<int>(C-1)), B(R-1, vector<int>(C));
  rep(i, R) rep(j, C-1) cin >> A.at(i).at(j);
  rep(i, R-1) rep(j, C) cin >> B.at(i).at(j);
  vector<vector<int>> D(R, vector<int>(C, INF));
  D.at(0).at(0) = 0;
  priority_queue<TIII, vector<TIII>, greater<TIII>> candidate;
  candidate.push(make_tuple(0, 0, 0));
  while (!candidate.empty()) {
    TIII t = candidate.top(); candidate.pop();
    int tD = get<0>(t);
    int p1 = get<1>(t);
    int p2 = get<2>(t);
    if (tD > D.at(p1).at(p2)) continue;
    if (p1 == R - 1 && p2 == C - 1) {
      cout << tD << endl; return 0;
    }
    if (p2 < C - 1 && chmin(D.at(p1).at(p2 + 1), tD + A.at(p1).at(p2))) {
      candidate.push(make_tuple(tD + A.at(p1).at(p2), p1, p2 + 1));
    }
    if (p2 > 0 && chmin(D.at(p1).at(p2 - 1), tD + A.at(p1).at(p2 - 1))) {
      candidate.push(make_tuple(tD + A.at(p1).at(p2 - 1), p1, p2 - 1));
    }
    if (p1 < R - 1 && chmin(D.at(p1 + 1).at(p2), tD + B.at(p1).at(p2))) {
      candidate.push(make_tuple(tD + B.at(p1).at(p2), p1 + 1, p2));
    }
    for (int i = 1; i <= p1; i++) {
      if (chmin(D.at(p1 - i).at(p2), tD + 1 + i)) {
        candidate.push(make_tuple(tD + 1 + i, p1 - i, p2));
      }
    }
  }
}


/*input
10
-31 -35
8 -36
22 64
5 73
-14 8
18 -58
-41 -85
1 -88
-21 -85
-11 82

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

int fun(pair<int,int> p1,pair<int,int> p2)
{
	int dy = abs(p2.second - p1.second);
	int dx = abs(p2.first - p1.first);
	// cout<<(dy*1.0/dx*1.0)<<" "<<(dy<=dx && dy>=(-1*dx))<<"\n";
	return  (dy<=dx);

}

int main()
{
	int T = 1; // cin>>T;
	while(T--)
	{
		int n;cin>>n;
		vector<pair<int,int>> p(n);
		for(int i=0;i<n;i++) cin>>p[i].first>>p[i].second;

		int ans=0;
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				ans+=fun(p[i],p[j]);
			}
		}

		cout<<ans<<"\n";

	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll mod=998244353;
const ll MAX=140010;

//gcd
ll gcd(ll a,ll b){
    if(min(a,b)==0)return max(a,b);
    if(max(a,b)%min(a,b)==0)return min(a,b);
    return gcd(min(a,b),max(a,b)%min(a,b));
}

signed main(){
    ll n;cin>>n;
    vector<ll>a(n);
    rep(i,n)cin>>a[i];
    sort(all(a));
    map<ll,pll>ma;
    ll ans=0;
    rep(i,n){
        REP(j,1,sqrt(a[i]+1)){
            if(a[i]%j==0){
                if(j<=a[0]){
                    if(ma.find(j)!=ma.end()){
                        ll g=ma[j].fi;
                        ll k=ma[j].se;
                        ma[j]=mp(gcd(g,a[i]),k+1);
                    }else{
                        ma[j]=mp(a[i],1);
                    }
                }
                if(a[i]/j<=a[0]){
                    if(ma.find(a[i]/j)!=ma.end()){
                        ll g=ma[a[i]/j].fi;
                        ll k=ma[a[i]/j].se;
                        ma[a[i]/j]=mp(gcd(g,a[i]),k+1);
                    }else{
                        ma[a[i]/j]=mp(a[i],1);
                    }
                }
            }
        }
    }
    for(auto e:ma){
        if((e.fi==e.se.fi&&e.se.se>=2)||e.fi==a[0]){
            //cout<<e.fi<<endl;
            ans++;
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i, n) rep(i, 1, n)
#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 5010, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
template <class o, class... O> void qr(o& x, O&... y) {
    qr(x);
    qr(y...);
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
template <class o, class... O> void pr1(o x, O... y) {
    pr1(x);
    pr1(y...);
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(10);
}
template <class o, class... O> void pr2(o x, O... y) {
    pr2(x);
    pr2(y...);
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

int n, m;
ll s[N], p[N];

void solve() {
    qr(n, m);
    p[0] = 1;
    FOR(i, n) p[i] = p[i - 1] * m % mod;
    rep(i, 0, m - 1) {
        ll t = 1;
        FOR(j, n) ad(s[j], t), t = t * i % mod;
    }
    ll ans = p[n] * n % mod;
    FOR(i, n) {
        FOR(j, i - 1) { dl(ans, s[i - j] * p[n - (i - j) - 1] % mod); }
    }
    pr2(ans);
}

int main() {
    int T = 1;
    // qr(T);
    while (T--)
        solve();
    return 0;
}


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

const int64_t MOD = 1000000007LL;

struct modint {
	int64_t x;
	modint() { }
	modint(int _x) : x(_x) { }
	operator int() const { return (int)x; }
	modint operator+(int y) { return (x + y + MOD) % MOD; }
	modint operator+=(int y) { x = (x + y + MOD) % MOD; return *this; }
	modint operator-(int y) { return (x - y + MOD) % MOD; }
	modint operator-=(int y) { x = (x - y + MOD) % MOD; return *this; }
	modint operator*(int y) { return (x * y) % MOD; }
	modint operator*=(int y) { x = (x * y) % MOD; return *this; }
	modint operator/(int y) { return (x * modpow(y, MOD - 2)) % MOD; }
	modint operator/=(int y) { x = (x * modpow(y, MOD - 2)) % MOD; return *this; }
	static modint modinv(int a) { return modpow(a, MOD - 2); }
	static modint modpow(int a, int b) {
		modint x = a, r = 1;
		for (; b; b >>= 1, x *= x) if (b & 1) r *= x;
		return r;
	}
};

typedef pair<modint, modint> II;

int64_t solve(int64_t N, std::vector<int> A) {
	vector<vector<II>> dp(N + 1, vector<II>(2, II(0, 0)));
	dp[0][0] = II(1, A[0]);
	for (int i = 1; i < N; ++i) {
		II& a = dp[i][0];
		a = dp[i - 1][0];
		a.first += dp[i - 1][1].first;
		a.second += dp[i - 1][1].second;
		a.second += a.first * A[i];
		II& b = dp[i][1];
		b = dp[i - 1][0];
		b.second -= b.first * A[i];
	}
	modint ans = dp[N - 1][0].second + dp[N - 1][1].second;
	return ans;
}

int main() {
#if DEBUG || _DEBUG
	freopen("in.txt", "r", stdin);
//	freopen("in_1.txt", "r", stdin);
#endif
	int64_t N;
	std::cin >> N;
	std::vector<int> A(N);
	for (int i = 0; i < N; i++) {
		std::cin >> A[i];
	}
	cout << solve(N, std::move(A)) << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

template <int MOD_> struct modnum {
	static constexpr int MOD = MOD_;
	static_assert(MOD_ > 0, "MOD must be positive");

private:
	using ll = long long;

	int v;

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

public:

	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int() const { return v; }
	friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
	friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }

	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

	modnum inv() const {
		modnum res;
		res.v = minv(v, MOD);
		return res;
	}
	friend modnum inv(const modnum& m) { return m.inv(); }
	modnum neg() const {
		modnum res;
		res.v = v ? MOD-v : 0;
		return res;
	}
	friend modnum neg(const modnum& m) { return m.neg(); }

	modnum operator- () const {
		return neg();
	}
	modnum operator+ () const {
		return modnum(*this);
	}

	modnum& operator ++ () {
		v ++;
		if (v == MOD) v = 0;
		return *this;
	}
	modnum& operator -- () {
		if (v == 0) v = MOD;
		v --;
		return *this;
	}
	modnum& operator += (const modnum& o) {
		v -= MOD-o.v;
		v = (v < 0) ? v + MOD : v;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		v = (v < 0) ? v + MOD : v;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}
	modnum& operator /= (const modnum& o) {
		return *this *= o.inv();
	}

	friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
	friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
	friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

using num = modnum<1000000007>;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	int N; cin >> N;
	vector<int> A(N); for (int& a : A) cin >> a;
	array<pair<num, num>, 2> dp{};
	dp[0].first = 1;
	dp[0].second = A[0];
	for (int i = 1; i < N; ++i) {
		array<pair<num, num>, 2> ndp{};
		// 0 -> 0
		ndp[0].first += dp[0].first;
		ndp[0].second += dp[0].second + dp[0].first * A[i];
		// 0 -> 1
		ndp[1].first += dp[0].first;
		ndp[1].second += dp[0].second - dp[0].first * A[i];
		// 1 -> 0
		ndp[0].first += dp[1].first;
		ndp[0].second += dp[1].second + dp[1].first * A[i];
		dp.swap(ndp);
	}
	cout << dp[0].second + dp[1].second << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

const ll MOD = 1e9+7;
ll mod(ll a, ll b=MOD) {
  return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
    if (a == 0) { x = 0; y = 1; return b;}
    ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}
ll mod_inverse(ll a, ll n=MOD) {
  ll x, y;
  ll d = extended_euclid(a, n, x, y);
  if (d > 1) return -1;
  return mod(x,n);
}
struct Mt {
    ll x = 0;
    Mt(){}
    Mt(ll y):x(mod(y)){}
    bool operator==(Mt &y){return x==y.x;}
    Mt operator+(Mt y){return mod(x+y.x);}
    Mt operator-(){return mod(-x);}
    Mt operator-(Mt y){return mod(x-y.x);}
    Mt operator*(Mt y){return mod(x*y.x);}
    Mt operator/(Mt y){return mod(x*mod_inverse(y.x));}
    Mt operator+=(Mt y){return x=(*this+y).x;}
    Mt operator-=(Mt y){return x=(*this-y).x;}
    Mt operator*=(Mt y){return x=(*this*y).x;}
    Mt operator/=(Mt y){return x=(*this/y).x;}
};
ostream& operator<<(ostream& os, const Mt&m){
    os << m.x; return os;
}

const ll N = 1e5+7;
ll a[N];
ll n;
ll CNT[N][2];
Mt cnt(ll x, bool lm) {
    ll &ret = CNT[x][lm];
    if(~ret) return ret;
    if(x==n) return 1;
    Mt r = cnt(x+1,0);
    if(x && !lm) r += cnt(x+1, 1);
    return ret=r.x;
}

ll DP[N][2];
Mt dp(ll x, bool lm) {
    ll &ret = DP[x][lm];
    if(~ret) return ret;
    if(x==n) return 0;
    Mt r = Mt(a[x])*cnt(x+1,0) + dp(x+1, 0);
    if(x && !lm) r += Mt(-a[x])*cnt(x+1,1) + dp(x+1, 1);
    return ret=r.x;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    CL(DP,-1); CL(CNT,-1);
    cin >> n;
    F(n) cin >> a[i];
    cout << dp(0,0) << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define For(i,a,b) for(register int i=a;i<=b;++i)
#define Down(i,a,b) for(register int i=a;i>=b;i--)
#define reg register
namespace yspm{
	inline int read(){
		int res=0,f=1; char k;
		while(!isdigit(k=getchar())) if(k=='-') f=-1;
		while(isdigit(k)) res=res*10+k-'0',k=getchar();
		return res*f;
	}
	const int N=1e5+10;
	inline int min(int x,int y){return x<y?x:y;}
	inline int max(int x,int y){return x>y?x:y;}
	inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
	vector<int>g[N];
	int sz[N],f[N],n;
	inline void dfs(int x){
		sz[x]=1; vector<int> ev,od; f[x]=-1;
		for(auto t:g[x]) dfs(t),sz[x]^=sz[t],sz[t]?od.push_back(f[t]):ev.push_back(f[t]);
		sort(od.begin(),od.end()); reverse(od.begin(),od.end());
		sort(ev.begin(),ev.end()); reverse(ev.begin(),ev.end());
		int ne=0,no=0,fl=0;
		while(ne<ev.size()&&ev[ne]>0) f[x]+=ev[ne],++ne;
		while(no<od.size()) fl?f[x]-=od[no]:f[x]+=od[no],++no,fl^=1;
		while(ne<ev.size()) fl?f[x]-=ev[ne]:f[x]+=ev[ne],++ne;
		return ;
	}
	signed main(){
		n=read(); for(reg int i=2,tf;i<=n;++i) tf=read(),g[tf].push_back(i); dfs(1);
		printf("%lld\n",(n-f[1])/2);
		return 0;
	}
}
signed main(){return yspm::main();}


#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, m, q;
    cin >> n >> m >> q;
    vector<pair<int, int>> vw(n, make_pair(0, 0));
    vector<pair<int, int>> x(m, make_pair(0, 0));

    rep(i, n)
    {
        int a, b;
        cin >> a >> b;
        vw[i] = make_pair(b, a);
    }
    sort(vw.begin(), vw.end(), greater<pair<int, int>>());

    rep(i, m)
    {
        int a;
        cin >> a;
        x[i] = make_pair(a, i);
    }
    sort(x.begin(), x.end());

    rep(i, q)
    {
        int l, r;
        cin >> l >> r;
        --l, --r;
        int ans = 0;
        vector<int> seen(m, 0);
        rep(i, n)
        {
            int xmin = INF;
            int xmin_idx = -1;
            rep(j, m)
            {
                if (x[j].second >= l && x[j].second <= r)
                    continue;
                if (vw[i].second <= x[j].first && seen[x[j].second] == 0 && x[j].first < xmin)
                {
                    xmin = x[j].first;
                    xmin_idx = x[j].second;
                }
            }
            if (xmin_idx == -1)
                continue;
            seen[xmin_idx] = 1;
            ans += vw[i].first;
        }
        cout << ans << endl;
    }

    return 0;
}

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

#define ll long long
int mod = 1e9+7;
#define int ll

int d, x, y;
void extendedEuclid(int A, int B) {
    if(B == 0) {
        d = A;
        x = 1;
        y = 0;
    }
    else {
        extendedEuclid(B, A%B);
        int temp = x;
        x = y;
        y = temp - (A/B)*y;
    }
}

int modInverse(int A, int M)
{
    extendedEuclid(A,M);
    return (x%M+M)%M;    //x may be negative
}

void test(){
	int n , s , k;
	cin >> n >> s >> k;
	int g = __gcd(n,k);
	if(s%g != 0){
		cout<<"-1\n";
		return;
	}
	n/=g , s/=g , k/=g;
	int ans = ((n-s) * modInverse(k,n))%n;
	cout<<ans<<"\n";
}

int32_t main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	int t;
	cin >> t;
	while(t--){
		test();
	}
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, init, end) for(ll i = init; i < end; i++)
#define REP(i, init, end) for(ll i = init; i < end + 1; i++)
#define rev(i, end, init) for(ll i = init - 1; i >= end; i--)
#define REV(i, end, init) for(ll i = init; i >= end; i--)
#define PI 3.14159265359
// #define EPS 0.0000000001
// #define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;

ll getSize(ll N){
    ll count = 0;
    while(N > 0){
        count++;
        N /= 10;
    }

    return count;
}

int main(){
    string S[3];
    rep(i, 0, 3){
        cin >> S[i];
    }

    ll N[3][26];
    ll x;
    rep(i, 0, 3){
        rep(j, 0, 26){
            x = 0;
            rep(k, 0, S[i].size()){
                x *= 10;
                if(S[i][k] - 'a' == j){
                    x += 1;
                }
            }
            N[i][j] = x;
        }
    }

    vector<ll> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    ll p;
    ll Nnum[3];
    ll Nsize[3];
    bool moveP;
    bool found = false;
    do{
        p = 0;
        rep(i, 0, 3){
            Nnum[i] = 0;
        }

        rep(j, 0, 26){
            moveP = false;
            rep(i, 0, 3){
                if(N[i][j] != 0){
                    moveP = true;
                }
                Nnum[i] += N[i][j] * v[p];
            }
            if(moveP){
                p++;
            }
        }

        if(Nnum[0] + Nnum[1] == Nnum[2]){
            found = true;
            rep(i, 0, 3){
                if(S[i].size() != getSize(Nnum[i])){
                    found = false;
                }
            }
            if(found){
                break;
            }
        }
    }while(next_permutation(v.begin(), v.end()));

    if(found){
        rep(i, 0, 3){
            cout << Nnum[i] << endl;
        }
    }else{
        cout << "UNSOLVABLE" << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, begin, end) for (i=begin;i<end;i++)
#define printint(i0, i1) printf("%d %d\n", i0, i1)
#define MAX_N 1000

using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
ll i, j, n, m, k, ans;
ll corr[27], exist[27] = {};
ll a[10], b[10], c[10];
vector<ll> chars;

int main() {
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    rep(i,0,s1.length()){
        a[s1.length() - i - 1] = s1[i] - 'a';
        exist[s1[i] - 'a'] = 1;
    }
    rep(i,0,s2.length()){
        b[s2.length() - i - 1] = s2[i] - 'a';
        exist[s2[i] - 'a'] = 1;
    }
    rep(i,0,s3.length()){
        c[s3.length() - i - 1] = s3[i] - 'a';
        exist[s3[i] - 'a'] = 1;
    }
    rep(i,0,27) if (exist[i] == 1) chars.push_back(i);
    if (chars.size() > 10) {
        printf("UNSOLVABLE\n");
        exit(0);
    }
    vector<ll> nums;
    rep(i,0,10) nums.push_back(i);
    do {
        bool flag = false;
        rep(i,0,chars.size()) corr[chars[i]] = nums[i];
        ll n1 = 0, n2 = 0, n3 = 0;
        rep(i,0,s1.length()) {
            n1 += corr[a[i]] * pow(10,i);
            if (i == s1.length()-1 && corr[a[i]] == 0) flag = true;
        }
        rep(i,0,s2.length()) {
            n2 += corr[b[i]] * pow(10,i);
            if (i == s2.length()-1 && corr[b[i]] == 0) flag = true;
        }
        rep(i,0,s3.length()) {
            n3 += corr[c[i]] * pow(10,i);
            if (i == s3.length()-1 && corr[c[i]] == 0) flag = true;
        }
        if (n1 + n2 == n3 && !flag) {
            printf("%lld\n%lld\n%lld\n", n1, n2, n3);
            exit(0);
        }
    } while (next_permutation(nums.begin(), nums.end()));
    printf("UNSOLVABLE\n");

}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;

double dp[110][110][110];

int main(){
	vector<int> A(3);
	rep(i,3) cin >> A[i];
	dp[A[0]][A[1]][A[2]] = 1.0;
	rep(i,100){
		rep(j,100){
			rep(k,100){
				if(i + j + k == 0) continue;
				double a = i, b = j, c = k;
				double s = a + b + c;
				dp[i+1][j][k] += dp[i][j][k] * (a / s);
				dp[i][j+1][k] += dp[i][j][k] * (b / s);
				dp[i][j][k+1] += dp[i][j][k] * (c / s);
			}
		}
	}
	double ans = 0;
	int S = A[0] + A[1] + A[2];
	rep(j,100){
		rep(k,100){
			if(j < A[1] or k < A[2]) continue;
			double num = 100.0 + j + k - S;
			ans += num * dp[100][j][k];
		}
	}
	rep(j,100){
		rep(k,100){
			if(j < A[0] or k < A[2]) continue;
			double num = 100.0 + j + k - S;
			ans += num * dp[j][100][k];
		}
	}
	rep(j,100){
		rep(k,100){
			if(j < A[0] or k < A[1]) continue;
			double num = 100.0 + j + k - S;
			ans += num * dp[j][k][100];
		}
	}
	cout << setprecision(15) << ans << endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int n = 100;
double dp[n + 1][n + 1][n + 1];

int main() {
  ios_base::sync_with_stdio(false);

  for (int i = n - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      for (int k = n - 1; k >= 0; k--) {
        int s = i + j + k;
        if (s > 0) {
          dp[i][j][k] += (1 + dp[i + 1][j][k]) * i / s;
          dp[i][j][k] += (1 + dp[i][j + 1][k]) * j / s;
          dp[i][j][k] += (1 + dp[i][j][k + 1]) * k / s;
        }
      }
    }
  }

  int a, b, c;
  cin >> a >> b >> c;
  cout << fixed << setprecision(10) << dp[a][b][c] << '\n';

  return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define all(x) begin(x), end(x)
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
const int INF=1e9;
const int MAX_N=200005;

int par[MAX_N];
int rnk[MAX_N];
map<int,int> cnt[MAX_N];
int c[MAX_N];

void init(int n) {
  for (int i=0; i<n; i++) {
    par[i]=i;
    rnk[i]=0;
    cnt[i][c[i]]=1;
  }
}
int find(int x) {
  if (par[x]==x) {
    return x;
  }
  else {
    return par[x]=find(par[x]);
  }
}

void unite(int x,int y) {
  x=find(x);
  y=find(y);
  if (x==y) return;
  if (rnk[x]<rnk[y]) {
    par[x]=y;
    for (auto p:cnt[x]) {
      cnt[y][p.first]+=p.second;
    }
  }
  else {
    par[y]=x;
    if (rnk[x]==rnk[y]) rnk[x]++;
    for (auto p:cnt[y]) {
      cnt[x][p.first]+=p.second;
    }
  }
}

bool same(int x, int y) {
  return find(x)==find(y);
}

int main(){
  int n,q;
  cin>>n>>q;
  rep(i,n) cin>>c[i];
  init(n);

  while(q--){
    int t,a,b;
    cin>>t>>a>>b;

    if(t==1){
      a--; b--;
      unite(a,b);
    }
    else{
      a--;
      int x=find(a);
      cout<<cnt[x][b]<<endl;
    }
  }
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2E5;
int par[MAXN];
int sz[MAXN];
int rep[MAXN];
pair<long long, int> a[MAXN];
pair<long long, int> f[MAXN];
long long ans[MAXN];

int getRoot(int x)
{
    if (par[x] != x)
        par[x] = getRoot(par[x]);
    return par[x];
}

void mge(int x, int y)
{
    x = getRoot(x);
    y = getRoot(y);
    if (x == y)
        return;
    int r = rep[y];
    if (sz[x] > sz[y])
        swap(x, y);
    par[x] = y;
    sz[y] += sz[x];
    rep[y] = r;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> f[i].first >> f[i].second;
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        cin >> a[i].first;
        a[i].second = i;
        par[i] = i;
        sz[i] = 1;
        rep[i] = i;
    }
    sort(a, a + q);
    int left = 0;
    int right = q - 1;
    long long delta = 0;
    for (int i = 0; i < n; i++)
    {
        if (f[i].second == 1)
        {
            delta += f[i].first;
        }
        else if (f[i].second == 2)
        {
            f[i].first -= delta;
            if (a[left].first < f[i].first)
            {
                while (left + 1 <= right && a[left + 1].first < f[i].first)
                {
                    mge(left, left + 1);
                    left++;
                }
                a[left].first = f[i].first;
            }
        }
        else if (f[i].second == 3)
        {
            f[i].first -= delta;
            if (a[right].first > f[i].first)
            {
                while (right - 1 >= left && a[right - 1].first > f[i].first)
                {
                    mge(right, right - 1);
                    right--;
                }
                a[right].first = f[i].first;
            }
        }
    }
    //cout << "left " << left << endl;
    //cout << "right " << right << endl;
    for (int i = 0; i < q; i++)
    {
        ans[a[i].second] = a[rep[getRoot(i)]].first + delta;
        //cout << "setting " << a[i].second << endl;
    }
    for (int i = 0; i < q; i++)
        cout << ans[i] << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cerr<<#x<<"="<<x<<endl;
#define db2(x,y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int rd(){
    int res=0,c,f=0;
    while(!isdigit(c=getchar()))f=c=='-';
    do res=(res<<1)+(res<<3)+(c^48);
    while(isdigit(c=getchar()));
    return f?-res:res;
}
int n,m,cnt[1<<20];
LL a[1<<20];
char s[30];
int main(){
    n=rd(),m=rd();
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        int v=0;
        for(int j=1;j<=m;j++)v=v<<1|(s[j]-'0');
        a[v]++;
    }
    for(int l=1;l<1<<m;l<<=1)
        for(int i=0;i<1<<m;i+=l<<1)
            for(int j=i;j<i+l;j++){
                LL x=a[j],y=a[j+l];
                a[j]=x+y,a[j+l]=x-y;
            }
    for(int i=0;i<1<<m;i++)a[i]*=a[i];
    for(int l=1;l<1<<m;l<<=1)
        for(int i=0;i<1<<m;i+=l<<1)
            for(int j=i;j<i+l;j++){
                LL x=a[j],y=a[j+l];
                a[j]=x+y,a[j+l]=x-y;
            }
    int t=1<<m;
    LL res=0;
    for(int i=1;i<1<<m;i++){
        cnt[i]=cnt[i&(i-1)]^1;
        if(cnt[i])res+=a[i]/t;
    }
    printf("%lld\n",res/2);
    return 0;
}

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

int main(){
  long long N,M,Even=0,Odd=0,ans=0;
  cin >> N >> M;

  string S;
  vector<long long> sum(N);

  for(long long i=0;i<N;i++){
    cin >> S;

    for(long long j=0;j<M;j++){
      if(S[j]=='1'){
        sum[i]++;
      }
    }
  }

  for(long long i=0;i<N;i++){
    if(sum[i]%2==0){
      Even++;
    }
    else
      Odd++;
  }

  ans = Even*Odd;
  cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0)
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

int32_t main(){
   ios;
   int v,t,s,d;
   cin>>v>>t>>s>>d;

   int l=v*t;
   int r=v*s;

   if(d>=l && d<=r) cout<<"No"<<endl;
   else cout<<"Yes"<<endl;
}

#include<bits/stdc++.h>
using namespace std;
//struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A>
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pint;
typedef pair<ll,ll> plint;
const int mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 3e6 + 10;

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    explicit operator int() const { return x; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r;
}

template<typename T, int FAC_MAX> struct Comb { vector<T> fac, ifac;
    Comb(){fac.resize(FAC_MAX,1);ifac.resize(FAC_MAX,1);for(int i=1;i<FAC_MAX;++i)fac[i]=fac[i-1]*i;
        ifac[FAC_MAX-1]=T(1)/fac[FAC_MAX-1];for(int i=FAC_MAX-2;i>=1;--i)ifac[i]=ifac[i+1]*T(i+1);}
    T aPb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b]; }
    T aCb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b] * ifac[b]; }
    T nHk(int n, int k) { if (n == 0 && k == 0) return T(1); if (n <= 0 || k < 0) return 0;
        return aCb(n + k - 1, k); } // nHk = (n+k-1)Ck : n is separator
    T pairCombination(int n) {if(n%2==1)return T(0);return fac[n]*ifac[n/2]/(T(2)^(n/2));}
    // combination of paris for n
};
typedef ModInt<mod> mint;
Comb<mint, N> com;

int main()
{
	int n, m, k;
	cin >> n >> m >> k;

	mint ans = com.aCb(n + m, n) - com.aCb(n + m, n - k - 1);
	if (n > m + k)  ans = 0;
	cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i, n) rep(i, 1, n)
#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 55, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
template <class o, class... O> void qr(o& x, O&... y) {
    qr(x);
    qr(y...);
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
template <class o, class... O> void pr1(o x, O... y) {
    pr1(x);
    pr1(y...);
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(10);
}
template <class o, class... O> void pr2(o x, O... y) {
    pr2(x);
    pr2(y...);
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

int n, p[N], a[N], tot;
ll ans = 1e18;
bool v[N], cho[N];

void dfs(int x, ll y) {
    if (y >= ans)
        return;
    if (x == tot + 1) {
        FOR(i, n) if (__gcd((ll)a[i], y) == 1) return;
        ans = y;
        return;
    }
    dfs(x + 1, y * p[x]);
    dfs(x + 1, y);
}

void solve() {
    n = 50;
    rep(i, 2, n) if (!v[i]) {
        for (int j = i * i; j <= n; j += i)
            v[j] = 1;
    }
    qr(n);
    FOR(i, n) {
        qr(a[i]);
        int x = a[i];
        for (int j = 2; j <= x; j++)
            if (x % j == 0) {
                while (x % j == 0)
                    x /= j;
                if (!cho[j])
                    cho[j] = 1, p[++tot] = j;
            }
    }
    dfs(1, 1);
    pr2(ans);
}

int main() {
    int T = 1;
    // qr(T);
    while (T--)
        solve();
    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using ll = long long;
ll INF = 2e18;
using namespace std;

int N;
ll ans=INF;
set<int> s;
vector<int> v;
int list[16] = {2,3,5,7,11,13,17,19,23,29,31,37,39,41,43,47} ;

void regP(int a){
	for(int e:s){
		if(a % e == 0)return;
	}
	for(int i = 2;i*i<=a;i++){
		if(a%i == 0){
			s.insert(i);
			s.insert(a/i);
			return;
		}
	}
	s.insert(a);
}

ll calc(int a){
	ll ret =1;
	for(int i=0;i<16;i++ ){
		if( a & (1<<i)) ret*=list[i];
	}
	return ret;
}
bool isOk(ll a,ll b){
	for(int i = 2; i < 50;i++){
		if(a % i == 0&& b % i == 0){
			return true;
		}
	}
	return false;
}
int main() {
	cin >> N;
	rep(i,N) {
		int a;
		cin >> a;
		v.push_back(a);
	}
	//cout << calc(0x1000);
	for(int i = 1;i<=0xffff;i++){
		int flag = 0;
		ll temp = calc(i);
		for(int e:v){
			if(!isOk(temp,e)){
				flag = 1;
				break;
			}
		}
		if(flag == 1) continue;
		if(ans > temp) ans = temp;
	}
	cout << ans;
	return 0;

}


#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
#define N 5050
int n,as;
char s[N];
map<pair<int,int>,int> tp;
int main()
{
	scanf("%d%s",&n,s+1);
	int v1=0,v2=0;
	tp[make_pair(0,0)]=1;
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='A')v1++;
		if(s[i]=='T')v1--;
		if(s[i]=='C')v2++;
		if(s[i]=='G')v2--;
		as+=tp[make_pair(v1,v2)];tp[make_pair(v1,v2)]++;
	}
	printf("%d\n",as);
}

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

int main()
{
  string S;
  cin >> S;

  for(int i = 0; i < S.size(); ++i)
  {
    if(S.at(i) == '6') S.at(i) = '9';
    else if(S.at(i) == '9') S.at(i) = '6';
  }

  reverse(S.begin(), S.end());
  cout << S << endl;

}


#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define inrange(a, x, b) (a <= x && x <= b)
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using vc = vector<char>;
using vcvc = vector<vc>;
using pll = pair<ll, ll>;
template<class T> void print(T x,bool cl=1){cout<<(cl?"\x1b[36m":"")<<x<<(cl?"\x1b[39m":"")<<'\n';}
template<class T> void print1d(T x,ll n=-1,bool cl=true){if(n==-1)n=x.size();rep(i,0,n){cout<<(cl?"\x1b[36m":"")<<x[i]<<(i==n-1?'\n':' ');}cout<<(cl?"\x1b[39m":"");}
template<class T> void print2d(T x,ll r=-1,ll c=-1,bool cl=1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c,cl);}
template<class T, class U> bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T> bool even(T n) { return ! (n & 1); }
template<class T> bool odd(T n) { return n & 1; }
template<class T> ll rup(T a, T b) { return a % b ? a / b + 1 : a / b; }
template<class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;
ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; }
ll modc(ll a, char op, ll b, ll _mod=mod)
{
        a %= _mod; b %= _mod; ll res = 1;
        switch (op) {
        case '+': res = (a + b) % _mod; break;
        case '-': res = (a - b) % _mod; break;
        case '*': res = a * b % _mod; break;
        case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break;
        case '^': res = intpow(a, b, _mod); break;
        case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break;
        case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break;
        }
        if (res < 0) { res += _mod; } return res;
}

int main()
{
        ll N;
        cin >> N;
        vll a(N);
        rep(i, 0, N) cin >> a[i];
        ll ans = 0;
        rep(i, 0, N) ans = __gcd(ans, a[i]);

        cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std ;
#define rep(i,n) for( int i = 0 ; i < n ; i++ )
typedef long long ll ;

const ll mod = 1e9 + 7 ;

int main(){
    ll n ;
    cin >> n ;
    set<ll> A ;
    rep(i,n){
        ll a ;
        cin >> a ;
        A.insert(a) ;
    }
    while( A.size() != 1 ){
        ll ma = *A.rbegin() ;
        ll mi = *A.begin() ;
        A.insert(ma - mi) ;
        A.erase(ma) ;
    }
    auto ans = *A.begin() ;
    cout << ans << endl ;
}


  #include <bits/stdc++.h>
  #define endl "\n"
  #define int long long int
  #define vi vector<int>
  #define vll vector<ll>
  #define vvi vector < vi >
  #define pii pair<int,int>
  #define pll pair<long long, long long>
  #define mod 1000000007
  #define inf 1000000000000000001;
  #define all(c) c.begin(),c.end()
  #define mp(x,y) make_pair(x,y)
  #define clr(a,val) memset(a,val,sizeof(a))
  #define repp(i, a, b)  for(int i = a; i < b; i++)
  #define rep(i, a, b)  for(int i = a; i <= b; i++)
  #define pb push_back
  #define f first
  #define s second

  using namespace std;

  void solve(){
     int n, k;
     cin >> n >> k;
     int a[n+1][n+1];
     rep(i, 1, n){
         rep(j, 1, n){
             cin >> a[i][j];
         }
     }
     int p[n];
     repp(i, 0, n){
         p[i] = i+1;
     }
     int cnt = 0;
     do{
         int prev = p[0];
         int sum = 0;
        //  for(int i = 0; i <n; i++)
        //     cout << p[i] << " ";
        //  cout << endl;
         for(int i = 1; i < n; i++){
            sum += a[prev][p[i]];
            prev = p[i];
         }
         sum += a[p[n-1]][1];
         if(sum == k)  cnt++;
     }while(next_permutation(p+1, p+n));
     cout << cnt;
  }

  int32_t main()
  {
    int T = 1;  //cin >> T;
    for(int t=1;t<=T;t++)
    {
      solve();
      //cout<<"Case #"<<t<<": "<<ans<<endl;
    }

    return 0;
  }

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

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
#define int ll

#define pb push_back
#define F first
#define S second
#define _sz(x) ((int)x.size())

#define fastio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

const int N = 10;
int g[N][N];
int st[N];
int n,T;
int res;
void dfs(int u,int step,int c)
{
    if(step==n-1){
        if(c+g[u][1]==T) res++;
        return;
    }
    for(int i = 1; i <= n; i++){
        if(!st[i]){
            if(c+g[u][i] < T){
                st[i] = 1;
                dfs(i,step+1,c+g[u][i]);
                st[i] = 0;
            }
        }
    }
}
signed main()
{
    fastio;
    cin >> n >> T;
    memset(st,0,sizeof(st));
    for(int i = 1; i <=  n; i++)
        for(int j = 1; j <= n; j++) cin >> g[i][j];
    st[1] = 1;
    dfs(1,0,0);
    cout << res <<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define ip pair<ll,ll>
#define vii vector<ll>
#define vpp vector<ip>
#define vdd vector<ld>
#define vb vector<bool>
#define pi  pair<ll,ip>
#define sc(x) scanf("%lli",&x)
#define hu 200010
#define pq priotity_queue<ll>
#define pqs priority_queue<ll,vii,greater<ll>>
#define ppq priority_queue<ip,vpp,greater<ip>>
#define INF INT_MAX
#define ff first
#define ss second
#define fo(i,k,n) for(ll i=k;i<n;i++)
using namespace std;
const ll M = 1000000000+7;
//Grid Movement
ll dx[] = {-1, 0, 1, 0};
ll dy[] = {0, -1, 0, 1};

ld Pi= acos(-1);

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll mex(ll x, ll y)
{
	ll res = 1;
	x = x % M;
	if (x == 0) return 0;
	while (y > 0)
	{
		if (y & 1)
		res = (res*x) % M;
		y = y>>1;
		x = (x*x) % M;
	}
	return res;
}

bool comp(ll comp1,ll comp2)
{
    return comp1>comp2;
}

void yes()
{
    printf("YES\n");
}

void no()
{
    printf("NO\n");
}

void solve(ll t)
{
    ll n;
    sc(n);

    ll dp[n][2];
    vector<string>vec(n);
    fo(i,0,n) cin >> vec[i];

    dp[0][1] = (vec[0]=="OR")?3:1;
    dp[0][0] = (vec[0]=="OR")?1:3;

    fo(i,1,n)
    {
        if(vec[i]=="OR")
        {
            dp[i][0] = dp[i-1][0];
            dp[i][1] = dp[i-1][1]*2 + dp[i-1][0];
        }
        else
        {
            dp[i][1] = dp[i-1][1];
            dp[i][0] = dp[i-1][0]*2 + dp[i-1][1];
        }
    }
    ll ans = dp[n-1][1];
    cout << ans << endl;

}

int main()
{
	//ios_base::sync_with_stdio(0); cin.tie(0);
    ll t=1;
    //sc(t);
    fo(i,1,t+1) solve(i);
}

#pragma GCC optimize("-Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
// #include <atcoder/all>
// #include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;
// using namespace atcoder;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<p64,p64> pp64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef pair<ll,p64> tp;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln '\n'
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zero ll(0)
#define set_bits(x) __builtin_popcountll(x)
// #define mint modint998244353

ll mpow(ll a, ll b){
	if(a==0) return 0;
	if(b==0) return 1;
	ll t1 = mpow(a,b/2);
	t1 *= t1;
	t1 %= MOD;
	if(b%2) t1 *= a;
	t1 %= MOD;
	return t1;
}

ll mpow(ll a, ll b, ll p){
	if(a==0) return 0;
	if(b==0) return 1;
	ll t1 = mpow(a,b/2,p);
	t1 *= t1;
	t1 %= p;
	if(b%2) t1 *= a;
	t1 %= p;
	return t1;
}

ll modinverse(ll a, ll m){
	ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1){
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0) x += m0;
    return x;
}

mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

ll range(ll l, ll r){
	return l + mt()%(r-l+1);
}

ll rev(ll v){
	return mpow(v,MOD-2);
}

ll nc2(ll n){
	return (n*(n-1))/2;
}

vv64 a;
v64 r,c;
ll h,w;

void set_column(ll, ll);

void set_row(ll index, ll v){
	if(r[index]) return;
	r[index]=v;
	forn(i,w) if(a[index][i]) set_column(i,v);
}

void set_column(ll index, ll v){
	if(c[index]) return;
	c[index]=v;
	forn(i,h) if(a[i][index]) set_row(i,v);
}

void solve(){
	cin >> h >> w;
	a.resize(h,v64(w,0));
	r.resize(h,0);
	c.resize(w,0);
	forn(i,h){
		forn(j,w){
			char c;
			cin >> c;
			if(c=='#') a[i][j]=1;
		}
	}
	set_row(0,1);
	set_row(h-1,1);
	set_column(0,1);
	set_column(w-1,1);
	ll val = 2;
	forn(i,h){
		forn(j,w) if(a[i][j]){
			set_row(i,val);
			set_column(j,val);
			val++;
		}
	}
	ll a1=0,a2=0;
	set<ll> s1,s2;
	for(auto it : r) {
		if(it) s1.insert(it);
		else a1++;
	}
	for(auto it : c) {
		if(it) s2.insert(it);
		else a2++;
	}
	cout << min(a1+sz(s1)-1,a2+sz(s2)-1) << ln;
}

int main()
{
	fast_cin();
	ll t=1;
	// cin >> t;
    forn(i,t) {
    	// cout << "Case #" << i+1 << ": ";
    	solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RNG(i, from, to) for (ll i = (from); i < (ll)(to); i++)
#define gcd(i, j) __gcd((i), (j))
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vecll;
template <typename S, typename T>
string to_string(pair<S, T> p) { return "(" + to_string(p.first) + "," + to_string(p.second) + ")"; }
template <typename T>
void println(T e) { cout << to_string(e) << endl; }
template <typename T>
void println(const vector<T> &v)
{
    cout << "[";
    for (const auto &e : v)
    {
        cout << to_string(e) << ",";
    }
    cout << "]";
    cout << endl;
}
template <typename T>
void println(const vector<vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        println(v);
    }
}
template <typename Iter>
void println(Iter first, Iter last)
{
    for (auto it = first; it != last; it++)
    {
        cout << *it << " ";
    };
    cout << endl;
}
template <typename T>
T mod_pow(T x, T n, const T &p)
{
    T ret = 1;
    while (n > 0)
    {
        if (n & 1)
            (ret *= x) %= p;
        (x *= x) %= p;
        n >>= 1;
    }
    return ret;
}
template <typename T>
T mod_inv(T x, const T &p) { return mod_pow(x, p - 2, p); }
const ll DVSR = 1e9 + 7;

void dfs(ll i, ll bef, map<ll, vecll> &TR, map<ll, vecll> &DF, vecll &BUF)
{
    for (const auto j : TR[i])
        if (j != bef)
        {
            auto item = DF.find(i + (j << 32));
            if (item != DF.end())
                for (const auto q : item->second)
                {
                    BUF[j] += q;
                    if (q < 0)
                        BUF[0] -= q;
                }
            dfs(j, i, TR, DF, BUF);
        }
}

void dfs2(ll i, ll bef, ll sm, vecll &res, map<ll, vecll> &TR, map<ll, vecll> &DF, const vecll &BUF)
{
    res[i] = sm + BUF[i];
    for (const auto j : TR[i])
    {
        if (j != bef)
        {
            dfs2(j, i, sm + BUF[i], res, TR, DF, BUF);
        }
    }
}
int main(int argc, char const *argv[])
{
    ll N, Q;
    cin >> N;
    vector<vector<ll>> MP;
    vector<pll> INL;
    map<ll, vecll> DF;
    map<ll, vecll> TR;
    vecll BUF;
    BUF.assign(N, 0);
    MP.assign(N, vector<ll>{});

    REP(k, N - 1)
    {
        ll i, j;
        cin >> i >> j;
        i--;
        j--;
        INL.push_back(pll(i, j));
    }
    cin >> Q;

    REP(k, Q)
    {
        ll t, e, x;
        cin >> t >> e >> x;
        e--;
        auto [i, j] = INL[e];
        ll ii = i + (j << 32);
        ll jj = j + (i << 32);

        auto iter = DF.find(ii);
        ll sign = t != 1 ? 1 : -1;
        if (iter == DF.end())
        {
            DF[ii] = vecll{x * sign};
            DF[jj] = vecll{-x * sign};
        }
        else
        {
            DF[ii].push_back(x * sign);
            DF[jj].push_back(-x * sign);
        }
    }
    for (const auto [a, b] : INL)
    {
        const auto nda = TR.find(a);
        const auto ndb = TR.find(b);
        if (nda == TR.end())
        {
            TR[a] = vecll{b};
        }
        else
        {
            nda->second.push_back(b);
        }
        if (ndb == TR.end())
        {
            TR[b] = vecll{a};
        }
        else
        {
            ndb->second.push_back(a);
        }
    }
    dfs(0, 0, TR, DF, BUF);
    vecll res;
    res.assign(N, 0);
    dfs2(0, 0, 0, res, TR, DF, BUF);
    for (const auto i : res)
    {
        cout << i << endl;
    }
    return 0;
}

  #include <bits/stdc++.h>
    // #include <ext/pb_ds/assoc_container.hpp>
  using namespace std;
  // using namespace __gnu_pbds;
  // template <class c, class cmp = less<c> > using ordered_set = tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
  #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  #define TRACE
  #ifdef TRACE
  #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
  template <typename Arg1>
  void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
  }
  template <typename Arg1, typename... Args>
  void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
  }
  #else
  #define trace(...)
  #endif
  template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
  template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
  template <typename T,typename U>pair<T,U> operator+(const pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second};}
  typedef long long int ll;
  const ll mod =1e9+7;
  const int maxn = 300005;
  #define endl '\n'
  #define ld long double
  #define int ll
  #define all(x)  (x).begin(),(x).end()

int c(int x,int y){
  int cnt = 0;
  while(x%3 == 0){
    x/=3;
    cnt++;
  }
  x = y;
  while(y%3 == 0){
    y/=3;
    cnt--;
  }
  return cnt;
}

int get(int x){
  while(x%3 == 0) x/=3;
  return x;
}

int32_t main(){

    IOS

    int n;
    cin >> n;
    string s;
    cin >> s;

    vector<int> coef(n);
    coef[0] = 1;
    map<char,int> m;
    m['R'] = 0;
    m['W'] = 1;
    m['B']  = 2;
    int powers = 0;
    int last = 1;
    for(int i = 1; i < n;i++){
      powers += c(n - i, i);
      last = (last * get(n - i)*get(i))%3;
      assert(powers >= 0);
      if(powers > 0){
        coef[i] = 0;
      }
      else{
        coef[i] = last;
      }
    }

    int ans = 0;
    for(int i = 0; i < n;i++){
      ans = (ans + coef[i] * m[s[i]])%3;
    }

    if((n- 1)%2){
      ans = (3 - ans)%3;
    }

    for(auto i: m){
      if(i.second == ans) cout << i.first << endl;
    }

}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<int>>;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define fi first
#define se second
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
#define set20 cout<<fixed<<setprecision(20) ;
// ミント

const ll mod = 1e9+7 ;
  //  998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//昆布
struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
  mint p(int n,int k){
    return fact[n]*ifact[n-k] ;
  }
  }  ;

mint modpow(ll a,ll b){
 if(b==0) return 1 ;
 mint c= modpow(a,b/2) ;
 if(b%2==1) return c*c*a ;
 else return c*c ;
}

mint komb(ll n,ll m){
  mint x=1 ;mint y=1 ;
  rep(i,m){
    x*= n-i ;
    y*= i+1 ;
  }
  return x/y ;
}

 map<ll,ll> factor(ll n){  //素因数とオーダーをマップで管理
  map <ll,ll> ord ;
  for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            int res=0;
            while(n%i==0){
                n/=i;
                res++;
            }
            ord[i]=res;
        }
    }
   if(n!=1) ord[n]++;
   return ord ;
 }

///
template< typename T >
T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if(b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int main(){
  ios::sync_with_stdio(false) ;
  cin.tie(nullptr) ;
 ll n ,p ; cin>>n>>p ;
  mint ans=p-1 ;
  ans*= modpow(p-2,n-1) ;
  cout<<ans<<endl ;
  return 0 ;
  }

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main()
{
  int64_t n, k;
  cin >> n >> k;

  for (int i = 0; i < k; i++)
  {
    if (n % 200 == 0)
    {
      n = n / 200;
    }
    else
    {
      n = n * 1000 + 200;
    }
  }
  cout << n << endl;
  return 0;
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<queue>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

short A[501][501], B[501][501];
int D[1 << 18];

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const int ma0 = 269488144;
const int ma1 = 252645135;
const int ma2 = 16711935;
const int ma3 = 65535;
inline int tiisaikazu() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	int tmp = *(int*)ci;
	int dig = 36 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	ci += 40 - dig >> 3;
	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int H = tiisaikazu(), W = tiisaikazu();
	rep(i, H) rep(j, W - 1) A[i][j] = tiisaikazu() << 1;
	rep(i, H - 1) rep(j, W) B[i][j] = tiisaikazu() << 1;

	priority_queue<ll, vector<ll>, greater<ll>> q;
	D[0] = -2e9 + 1;
	q.push((ll)D[0] << 32);
	int owa = (H - 1) << 9 | W - 1;

	while (q.size()) {
		auto tmp = q.top();
		q.pop();
		int d0 = tmp >> 32;
		int hwp = tmp & (1 << 20) - 1;
		if (D[hwp] != d0) continue;

		int h = hwp >> 9;
		int w = hwp & 511;
		int p = (d0 & 1) << 1 | 1;
		d0 |= 1;

		if (hwp == owa) break;

		if (w > 0) if (D[hwp - 1] > d0 + A[h][w - 1]) {
			D[hwp - 1] = d0 + A[h][w - 1];
			q.push((ll)(d0 + A[h][w - 1]) << 32 | hwp - 1);
		}
		if (w < W - 1) if (D[hwp + 1] > d0 + A[h][w]) {
			D[hwp + 1] = d0 + A[h][w];
			q.push((ll)(d0 + A[h][w]) << 32 | hwp + 1);
		}
		if (h > 0) if (D[hwp - 512] > d0 + p) {
			D[hwp - 512] = d0 + p;
			q.push((ll)(d0 + p) << 32 | hwp - 512);
		}
		if (h < H - 1) if (D[hwp + 512] > d0 + B[h][w]) {
			D[hwp + 512] = d0 + B[h][w];
			q.push((ll)(d0 + B[h][w]) << 32 | hwp + 512);
		}
	}

	co(D[owa] - D[0] >> 1);

	Would you please return 0;
}

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
// #define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int M = (1e9)+7;
const int N = 1000005;
const int INF = 1e9;

signed main()
{
  FAST
  int tc=1;
//   cin>>tc;
  for(int ti=1;ti<=tc;ti++)
  {
      int r,c;
      cin>>r>>c;
      int a[r+1][c+1], b[r+1][c+1];
      for(int i=1;i<=r;i++) for(int j=1;j<c;j++) cin>>a[i][j];
      for(int i=1;i<r;i++) for(int j=1;j<=c;j++)
      {
          cin>>b[i][j];
          if(i > 1) b[i][j] += b[i-1][j];
      }
      set<pair<int, pii>> dist;
      int val[r+1][c+1], vis[r+1][c+1];
      for(int i=1;i<=r;i++) for(int j=1;j<=c;j++) dist.insert({INF, {i,j}}), val[i][j] = INF, vis[i][j] = 0;
      dist.erase({INF, {1,1}});
      dist.insert({0, {1,1}});
      val[1][1] = 0;
      vis[1][1] = 1;
      int ans = INF;
      while(true)
      {
          auto it = *dist.begin();
          int d = it.ff;
          pii curr = it.ss;
          int x = curr.ff, y = curr.ss;
          if(x == r && y == c)
          {
              ans = d;
              break;
          }
          vis[x][y] = 1;
          dist.erase(it);
          for(int i=1;i<x;i++)
          {
              if(!vis[i][y])
              {
                  int new_val = d + x-i+1;
                  if(new_val < val[i][y])
                  {
                      dist.erase({val[i][y], {i,y}});
                      val[i][y] = new_val;
                      dist.insert({val[i][y], {i,y}});
                  }
              }
          }
          if(x < r && !vis[x+1][y])
          {
              int new_val = d + b[x][y] - (x-1 > 0 ? b[x-1][y] : 0);
              if(new_val < val[x+1][y])
              {
                  dist.erase({val[x+1][y], {x+1,y}});
                  val[x+1][y] = new_val;
                  dist.insert({val[x+1][y], {x+1,y}});
              }
          }
          if(y > 1 && !vis[x][y-1])
          {
              int new_val = d + a[x][y-1];
              if(new_val < val[x][y-1])
              {
                  dist.erase({val[x][y-1], {x,y-1}});
                  val[x][y-1] = new_val;
                  dist.insert({val[x][y-1], {x,y-1}});
              }
          }
          if(y < c && !vis[x][y+1])
          {
              int new_val = d + a[x][y];
              if(new_val < val[x][y+1])
              {
                  dist.erase({val[x][y+1], {x,y+1}});
                  val[x][y+1] = new_val;
                  dist.insert({val[x][y+1], {x,y+1}});
              }
          }
      }
      cout<<ans<<endl;
  }
  return 0;
}


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

using namespace std;

int main()
{
	int a1, a2, a3, a4;
	cin >> a1 >> a2 >> a3 >> a4;

	if (a1 <= a2 && a1 <= a3 && a1 <= a4) {
		cout << a1 << endl;
	}
	else if (a2 <= a1 && a2 <= a3 && a2 <= a4) {
		cout << a2 << endl;
	}
	else if (a3 <= a1 && a3 <= a2 && a3 <= a4) {
		cout << a3 << endl;
	}
	else {
		cout << a4 << endl;
	}
}

#include <bits/stdc++.h>
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    a=min(a,b);
    c = min(c,d);
    d=min(a,c);
    cout<<d;
}


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

int main(){
  int A,B,C,D;
  cin >> A >> B >> C >> D;
  if(A+B==C+D){
    cout << "Yes" << endl;
  }
  else if(A+C==B+D){
    cout << "Yes" << endl;
  }
  else if(A+D==B+C){
    cout << "Yes" << endl;
  }
  else if(A==B+C+D){
    cout << "Yes" << endl;
  }
  else if(B==C+D+A){
    cout << "Yes" << endl;
  }
  else if(C==A+B+D){
    cout << "Yes" << endl;
  }
  else if(D==A+B+C){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
#define ALL(x) x.begin(), x.end();
#define SIZE(x) ll(x.size())
#define CHMIN(x,y) x = min(x, (y))
#define CHMAX(x,y) x = max(x, (y))
#define YES(b) cout << ((b) ? "Yes" : "No") << endl

int n, i;

int main() {
    cin >> n;
    int a[n];
    REP(i, n) cin >> a[i];
    int cnt = 0;
    REP(i, n) {
        if (a[i] > 10) {
            cnt += a[i] - 10;
        }
    }
    cout << cnt << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ll N;
    cin >> N;

    string S;
    cin >> S;
    queue<int> I;
    for (ll i = 0; i < N; ++i) {
        if (S[i] == '1') {
            I.emplace(i);
        }
    }

    string T;
    cin >> T;
    vector<int> E;
    for (ll i = 0; i < N; ++i) {
        if (T[i] == '1') {
            E.emplace_back(i);
        }
    }

    ll E_size = E.size();
    ll I_size = I.size();
    if ((E_size - I_size) % 2 == 1) {
        cout << -1 << endl;
        return 0;
    }

    ll ans = 0;
    ll I_first;
    ll I_second;
    for (auto E_num : E) {
        if (I.empty()) {
            cout << -1 << endl;
            return 0;
        }
        I_first = I.front();
        I.pop();
        while (I_first < E_num) {
            if (I.empty()) {
                cout << -1 << endl;
                return 0;
            }
            I_second = I.front();
            I.pop();
            ans += I_second - I_first;

            if (I.empty()) {
                cout << -1 << endl;
                return 0;
            }
            I_first = I.front();
            I.pop();
        }
        ans += I_first - E_num;
    }

    while (!I.empty()) {
        I_first = I.front();
        I.pop();
        I_second = I.front();
        I.pop();
        ans += I_second - I_first;
    }

    cout << ans << endl;
}


#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
using ll=long long;

using namespace std;
int main() {
    ll N,K,tmp_ll;
    string tmp_s;
    cin>>N>>K;
    for(ll i = 1;i<=K;i++){
        if(N%200==0){
            N = N / 200;
            // cout<<"=================="<<endl;
            // cout<<N<<endl;
        }else{
            tmp_s = to_string(N) + "200";
            N = stoll(tmp_s);
            // cout<<"--------------"<<endl;
            // cout<<N<<endl;
        }
    }
    cout<<N<<endl;
}

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int main() {
	int N, K;
	cin >> N >> K;

	int n = 2*N;
	vector<Int> d(n + 5, 0);
	for (int i = 1; i <= N; ++i) {
		++d[i + 1];
		--d[i + N + 1];
	}
	for (int i = 1; i < d.size(); ++i) { d[i] += d[i - 1]; }

	Int ans = 0;
	for (int i = 2; i <= n; ++i) {
		int idx = i - K;
		if (0 <= idx and idx <= n) { ans += d[i] * d[idx]; }
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

template <typename T> void print_vv(vector<vector<T>> &vv){
  for(int i=0;i<vv.size();i++){
    cout << i << " * ";
    for(auto j:vv[i]) cout << j << ' ';
    cout << endl;
  }
}
template <typename T> void print_s(set<T> &a){
  cout << "{ ";
  for(auto i:a) cout << i << ' ';
  cout << '}' << endl;
}

int main(){

  int n,t;
  cin >> n >> t;
  vector<int> a(n);
  for(auto& i : a) cin >> i;
  sort(a.rbegin(), a.rend());

  if(n<=20){
    ll ans = 0;
    for(int i=0; i<(1<<n); ++i) {
      ll count =0;
      for(int j=0; j<n; ++j) {
	count += (ll)(i>>j&1)*a[j];
      }
      if(count <=t) ans = max(ans,count);
    }
    cout << ans << endl;
    return 0;
  }

  set<ll> ans1,ans2;
  for(int i=0; i<(1<<20); ++i) {
    ll count =0;
    for(int j=0; j<20; ++j) {
      count += (ll)(i>>j&1)*a[j];
    }
    if(count <=t) ans1.insert(count);
  }
  //print_s(ans1);

  n-=20;
  for(int i=0; i<(1<<n); ++i) {
    ll count =0;
    for(int j=0; j<n; ++j) {
      count += (ll)(i>>j&1)*a[j+20];
    }
    if(count <=t) ans2.insert(count);
  }
  //print_s(ans2);

  vector<ll> a1(ans1.begin(), ans1.end());

  ll total = 0;
  for(auto& i : ans2) {
    auto it = lower_bound(a1.begin(), a1.end(), t-i+1);
    if(it == a1.begin()) continue;
    total = max(total, (ll)i+*(it-1));
  }

  cout << total << endl;

}

//@formatter:off
#include<bits/stdc++.h>
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i)
#define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d)
#define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,n) for (ll i = ll(n)-1; i >= 0; i--)
#define rrep2(i,n,t) for (ll i = ll(n)-1; i >= (ll)t; i--)
#define rrep3(i,n,t,d) for (ll i = ll(n)-1; i >= (ll)t; i-=d)
#define rrep(...) overload4(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define popcount(x) __builtin_popcountll(x)
#define pb push_back
#define eb emplace_back
#ifdef __LOCAL
#define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; }
#else
#define debug(...) void(0)
#endif
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using LP = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vvvb = vector<vvb>;
using vp = vector<P>;
using vvp = vector<vector<P>>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
template<class T> void vecout(const vector<T> &v,char div='\n') { rep(i,v.size()) cout<<v[i]<<(i==int(v.size()-1)?'\n':div);}
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
void scan(){}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); }
template<class T> void print(const T& t){ cout << t << '\n'; }
template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); }
template<class... T> void fin(const T&... a) { print(a...); exit(0); }
struct Init_io {
    Init_io() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        cout << boolalpha << fixed << setprecision(15);
    }
} init_io;
const string yes[] = {"no","yes"};
const string Yes[] = {"No","Yes"};
const string YES[] = {"NO","YES"};
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
    INT(n);
    vvi v(n, vi(5));
    cin >> v;
    int ok = 0, ng = inf;
    auto f = [&](int x) -> bool {
        vvvb dp(n + 1, vvb(4, vb(1 << 5)));
        dp[0][0][0] = true;
        rep(i, n) rep(j, 4) rep(k, 1 << 5) {
                    if (!dp[i][j][k]) continue;
                    dp[i + 1][j][k] = true;
                    int nk = k;
                    rep(l, 5) if (v[i][l] >= x) nk |= 1 << l;
                    if (j < 3) dp[i + 1][j + 1][nk] = true;
                }
        return dp[n][3][(1 << 5) - 1];
    };
    while (abs(ok - ng) > 1) {
        int mid = (ng + ok) / 2;
        if (f(mid)) ok = mid;
        else ng = mid;
    }
    fin(ok);
}