#include<bits/stdc++.h>
// no of bits to store no 2e5 is 19
// 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai
using namespace std;

#define int               long long
#define mod               1000000007
#define MOD               1000000007
#define MAX               1000000000
#define inf               1e18
#define ff                first
#define ss                second
#define mp                make_pair
#define pb                push_back
#define pii               pair<int,int>
#define mii               map<int, int>
#define all(x)            (x).begin(), (x).end()
#define fill(a,b)         memset(a, b, sizeof(a))
#define sp(x,y)           fixed<<setprecision(y)<<x
#define setbits(x)        __builtin_popcount(x)
#define fast_in_out       ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define ll                long long

//   powermod(a,b,m) ==  powermod(a,b%(m-1),m)  when m is prime fermat's theorem a^(p-1) %p=1 p is prime
int powermod(int a,int b,int c) //  gives (a^b)%c
{
    int ans = 1,tempans=a%c;
    while(b>0)
    {
        if(b&1==1)
        {
            ans=(ans*tempans)%c;
        }
             tempans=(tempans*tempans)%c;
             b=b>>1;
    }
    return ans;
}

int modinverse(int a,int m)   // gives a inverse wrt m and m is prime
{
    return powermod(a,m-2,m);
}

ll add(ll x, ll y) {
   x += y;
   if (x >= mod) return x - mod;
   return x;
}
ll sub(ll x, ll y) {
   x -= y;
   if (x < 0) return x + mod;
   return x;
}
ll mult(ll x, ll y) {
   return (x * y) % mod;
}
// my template                                              m
// prime factorization                                      pf
// ncr and factorials                                       ncr
// matrix exponentiation of fibonacci series in log(n)      matexpofib
// cycles in a graph                                        cycle
// prime factorization in log(n)                            spf

vector<pair<int,pii>> edges[100005];// city
void solve()
{
  int n,m,st,en;
  cin>>n>>m>>st>>en;
  while(m--)
  {
     int x,y,ti,ki;
     cin>>x>>y>>ti>>ki;
     edges[x].pb({y,{ti,ki}});
      edges[y].pb({x,{ti,ki}});

  }

  // dis ind
  priority_queue<pii> q;
  vector<int> dis(n+1,inf);
  vector<bool > vis(n+1,false);

  dis[st]=0;
  q.push({0,st});
  while(!q.empty())
  {
     pii p= q.top();
     q.pop();
     if(vis[p.ss])
      continue;
     vis[p.ss]=true;
    // cout<<p.ss<<endl;

     for(auto ch: edges[p.ss])
     {
       // cout<<"and " <<ch.ff<<" "<<ch.ss.ff<<" "<<ch.ss.ss<<endl;
        int k= ch.ss.ss;
        int z= dis[p.ss]%k;
       // cout<<dis[p.ss]<<" "<<k<<" "<<z<<endl;
        if(z!=0)
        {
           z= k-z;
        }
        z+=(dis[p.ss]+ch.ss.ff);

        if(!vis[ch.ff]&&z<dis[ch.ff])
        {
           dis[ch.ff]=z;
           q.push({-z,ch.ff});

        }

     }
  }
  if(dis[en]==inf)
   dis[en]=-1;
  cout<<dis[en]<<endl;

}

int32_t main()
{
  fast_in_out;

  int t=1;//cin>>t;
  int count=0;

   while(t--)
   {
       count++;
       solve();
   }
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rrep(i, n) for(ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
#define in(A, n)                                                                                \
    rep(i, n) {                                                                                \
        cin >> f;                                                                                \
        A.push_back(f);                                                                                \
    }

void print(vl x) {
    for(ll i : x) {
        cout << i << " ";
    }
    cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vector<vl> g(MAX_N);
vector<vl> A;
vl C, D;
ll co[1 << 17][17];
void input() {
    cin >> n;
    rep(i, n) {
        cin >> x >> y >> z;
        A.pb({x, y, z});
    }
}
void solve() {
    rep(i, 1 << n) {
        if(i) {
            rep(j, n) { co[i][j] = INF; }
        }
    }
    co[1][0] = 0;
    k = 1 << n;
    rep(i, k) {
        C.clear();
        D.clear();
        rep(j, n) {
            if(i & (1 << j)) {
                C.pb(j);
            } else {
                D.pb(j);
            }
        }
        for(ll x : C) {
            for(ll y : D) {
                co[i + (1 << y)][y] =
                    min(co[i + (1 << y)][y],
                        co[i][x] + abs(A[x][0] - A[y][0]) + abs(A[x][1] - A[y][1]) + max(0ll, A[y][2] - A[x][2]));
            }
        }
    }

    ans = INF;
    rep(i, n) {
        if(i)
            ans =
                min(ans, co[k - 1][i] + abs(A[0][0] - A[i][0]) + abs(A[0][1] - A[i][1]) + max(0ll, A[0][2] - A[i][2]));
    }

    print(ans);
}
int main() {
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    while(codeforces--) {
        solve();
    }
}


/*input
3 4 3
1 9
5 3
7 8
1 8 6 9
4 4
1 4
1 3

*/

// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll          long long int
#define pb          push_back
#define pii         pair<ll ,ll >
#define vpii        vector< pii >
#define vi          vector<ll >
#define vs			vector< string >
#define vvi         vector< vector< ll > >
#define inf			(ll)1e18
#define all(it,a)   for(auto it=(a).begin();it!=(a).end();it++)
#define F           first
#define S           second
#define sz(x)       (ll )x.size()
#define rep(i,a,b)	for(ll  i=a;i<b;i++)
#define repr(i,a,b) for(ll  i=a;i>b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define mp          make_pair
#define whatis(x)   cout << #x << " is " << x << "\n";
#define graph(n)    adj(n,vector< ll > () )
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define debug(x)     cout << #x << " " << x << endl;
#define debug_p(x)   cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x)   {cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;}
#define debug_vp(x)  {cout << #x << " "; for (auto ioi : x) cout << '[' << ioi.F << " " << ioi.S << ']'; cout << endl;}
#define debug_v_v(x) {cout << #x << "/*\n"; for (auto ioi : x) { for (auto ioi2 : ioi) cout << ioi2 << " "; cout << '\n';} cout << "*/" << #x << endl;}

bool cmp(pii p1,pii p2){
	if(p2.S==p1.S) return p1.F < p2.F;
	else return p1.S > p2.S;
}

int solve()
{
	ll N,M,Q; cin>>N>>M>>Q;
	vector<pii> a(N);
	vi X(M);
	rep(i,0,N) cin>>a[i].F>>a[i].S;
	rep(i,0,M) cin>>X[i];

	sort(a.begin(), a.end(),cmp);

	rep(q,0,Q){
		ll L,R;cin>>L>>R;
		L--,R--;

		multiset<ll> curX;
		for(ll i=0;   i<L; i++) curX.insert(X[i]);
		for(ll i=R+1; i<M; i++) curX.insert(X[i]);

		ll ans = 0;
		for(ll i=0; i<N; i++){
			auto it = curX.lower_bound(a[i].F);
			if(it != curX.end()) {
				ans += a[i].S;
				curX.erase(it);
			}
		}

		cout<<ans<<"\n";
	}

	return 0;
}

int main()
{
	auto start = chrono::high_resolution_clock::now();

    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll test_cases=1;
	//cin>>test_cases;
	while(test_cases--)
		solve();

	auto stop = chrono::high_resolution_clock::now();
	auto duration = chrono::duration_cast<chrono::milliseconds>(stop-start);
	//cout<<"\nduration: "<<(double)duration.count()<<" milliseconds";
}

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PI;
const int maxm=2e6+5;
PI e[maxm];
PI ee[maxm];
int n,m,q;

int main(){
   cin>>n>>m>>q;
   for(int i=1;i<=n;i++){
      int x,y;
      cin>>x>>y;
      e[i]={x,y};
   }
   for(int i=1;i<=m;i++){
      int x;
      cin>>x;
      ee[i]={x,i};
   }
   sort(e+1,e+1+n);
   sort(ee+1,ee+1+m);
   while(q--){
      int l,r;
      cin>>l>>r;
      priority_queue<int,vector<int>,less<int> >q;
      int ans=0;
      int j=1;
      for(int i=1;i<=m;i++){
         if(ee[i].second>=l&&ee[i].second<=r)continue;
         while(j<=n&&e[j].first<=ee[i].first){
            q.push(e[j].second);
            j++;
         }
         if(q.size()){
            ans+=q.top();
            q.pop();
         }
      }
      cout<<ans<<endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod = 1e9 + 7;
const int N = 5e6;
int binpow[N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int h, w;
    cin >> h >> w;

    vector<string> s(h);

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

    binpow[0] = 1;
    for (int i = 1; i <= h * w; i++) {
        binpow[i] = (binpow[i - 1] * 2) % mod;
    }

    vector<vector<pair<int, int> > > col(h, vector<pair<int, int>>(w, make_pair(-1, h))),
            row(h, vector<pair<int, int>>(w, make_pair(-1, w)));

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == '#') {
                row[i][j].first = j;
            } else if (j > 0) {
                row[i][j].first = row[i][j - 1].first;
            }
        }

        for (int j = w - 1; j >= 0; j--) {
            if (s[i][j] == '#') {
                row[i][j].second = j;
            } else if (j < w - 1) {
                row[i][j].second = row[i][j + 1].second;
            }
        }
    }

    int k = 0;

    for (int j = 0; j < w; j++) {
        for (int i = 0; i < h; i++) {
            k += s[i][j] == '.';
            if (s[i][j] == '#') {
                col[i][j].first = i;
            } else if (i > 0) {
                col[i][j].first = col[i - 1][j].first;
            }
        }

        for (int i = h - 1; i >= 0; i--) {
            if (s[i][j] == '#') {
                col[i][j].second = i;
            } else if (i < h - 1) {
                col[i][j].second = col[i + 1][j].second;
            }
        }
    }

    int ans = 0;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == '#')
                continue;
            int p = col[i][j].second - col[i][j].first + row[i][j].second - row[i][j].first - 3;
            ans = (ans + (binpow[k] - binpow[k - p] + mod) % mod) % mod;
        }
    }

    cout << ans << '\n';

}

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
	tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);

	int n, L;
	cin >> n >> L;

	map<int, vector<pii>> move_intvs;

	vi ai(n), bi(n);
	for (auto &x : ai) { cin >> x; }
	for (auto &x : bi) { cin >> x; }

	vector<bool> found(n);

	map<int, int> pos_start;
	for (int i = 0; i < n; ++i) {
		pos_start[i] = ai[i];
	}
	pos_start[-1] = 0;
	pos_start[n] = L + 1;

	map<int, int> M;
	M[L + 1 - n] = n;

	for (int i = 0; i < n; ++i) {
		found[i] = (ai[i] == bi[i]);
	}

	for (int i = n - 1; i >= 0; --i) {
		const int seek = bi[i] - i;
		debug() << imie(seek);
		auto iter = M.find(seek);
		if (iter == M.end()) { M[ai[i] - i] = i; continue; }
		found[i] = true;
		const int who = iter->nd;
		debug() << imie(i) << imie(who) << imie("R");
		if (pos_start[who] - pos_start[i] > 1) {
			move_intvs[who].emplace_back(i, who - 1);
		}

		M[ai[i] - i] = i;
	}

	M.clear();
	M[1] = -1;
	for (int i = 0; i < n; ++i) {
		const int seek = bi[i] - i;
		auto iter = M.find(seek);
		if (iter == M.end()) { M[ai[i] - i] = i; continue; }
		found[i] = true;
		const int who = iter->nd;
		debug() << imie(i) << imie(who) << imie("L");
		if (abs(pos_start[who] - pos_start[i]) > 1) {
			move_intvs[who].emplace_back(who + 1, i);
		}

		M[ai[i] - i] = i;
	}

	debug() << imie(found);

	if (count(ALL(found), false)) {
		cout << "-1\n";
		return 0;
	}

	ll ans = 0;

	for (auto &info : move_intvs) {
		debug() << imie(info.second);
		const int x = info.first;

		int L = x, R = x;

		for (auto &intv : info.second) {
			if (intv.second == x - 1) {
				L = min(L, intv.first);
			} else if (intv.first == x + 1) {
				R = max(R, intv.second);
			} else {
				assert(false);
			}
		}

		ans += R - L;
	}

	cout << ans << "\n";

}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define rg register
template<typename T>void read(T &x){
	x=0;rg int fh=1;
	rg char ch=getchar();
	while(ch<'0' || ch>'9'){
		if(ch=='-') fh=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	x*=fh;
}
const int maxn=1e6+5;
int n;
double f[maxn];
int main(){
	read(n);
	rg double tmp;
	for(rg int i=n-1;i>=1;i--){
		tmp=1.0-1.0*i/n;
		f[i]=(f[i+1]*tmp+1.0)/tmp;
	}
	printf("%.10f\n",f[1]);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;

void solve() {
        int ar[] = {0, 6, 5, 4, 3, 2, 1};
        int a, b, c;
        cin >> a >> b >> c;
        cout << ar[a] + ar[b] + ar[c];
}

int32_t main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(12);

        int t = 1;
        //cin >> t;
        while (t--) {
                solve();
        }

        return 0;
}


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

typedef long long ll;

int main()
{
    ll n, k;
    cin >> n >> k;
    ll sum = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= k; j++)
        {
            sum += i * 100 + j;
        }
    }
    cout << sum;
}

#include <bits/stdc++.h>
//#include <chrono>
#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

vec prime;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>A>>B;
    N = B - A + 1;
    reps(i, 2, 72){
        bool pr = true;
        reps(j, 2, i) if(i%j == 0) pr = false;
        if(pr) prime.push_back(i);
    }

    vector<int> pelem(N, 0);
    rep(i, 20){
        ll p = prime[i];
        rep(j, N){
            pelem[j] |= (((A + j)%p == 0) ? 1 : 0)<<i;
        }
    }

    vec dp(1<<20, 0), ndp(1<<20, 0);
    dp[0] = 1;
    rep(i, N){
        int a_s = pelem[i];
        //cout<<bitset<20>(a_s)<<endl;
        rep(s, 1<<20){
            if((s&a_s) == 0){
                ndp[s | a_s] += dp[s];
            }
        }
        rep(s, 1<<20) {
            dp[s] += ndp[s];
            ndp[s] = 0;
        }
    }
    cout<<accumulate(ALL(dp), 0)<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
const ll INF = 1LL << 60;
// const int MOD = 1000000007;
const ll MOD = 998244353;

ll dp[5100][5100];
char field[5100][5100];

ll modpow(ll m, ll n)
{
    if (n == 1)
        return m;
    if (n == 0)
        return 1;
    ll res;
    ll t = modpow(m, n / 2);
    ll tmp;
    if (n % 2 == 0)
    {
        tmp = (t * t) % MOD;
    }
    else
    {
        tmp = (((t * t) % MOD) * m) % MOD;
    }
    return tmp;
}
ll pow(ll a, ll n)
{
    ll ret = 1;
    for (; n > 0; n >>= 1, a = a * a % MOD)
    {
        if (n % 2 == 1)
        {
            ret = ret * a % MOD;
        }
    }
    return ret;
}

ll inv(ll a)
{
    return pow(a, MOD - 2);
}

int main()
{
    int h, w, k;
    cin >> h >> w >> k;
    for (int i = 0; i < k; i++)
    {
        int x, y;
        char c;
        cin >> y >> x >> c;
        x--;
        y--;
        field[y][x] = c;
    }

    dp[0][0] = modpow(3, h * w - k);
    ll inv3 = inv(3);

    for (int y = 0; y < h; y++)
    {
        for (int x = 0; x < w; x++)
        {
            char c = field[y][x];
            if (c == 'X')
            {
                dp[y][x + 1] += dp[y][x];
                dp[y + 1][x] += dp[y][x];
            }
            else if (c == 'R')
            {
                dp[y][x + 1] += dp[y][x];
            }
            else if (c == 'D')
            {
                dp[y + 1][x] += dp[y][x];
            }
            else
            {
                dp[y][x + 1] += dp[y][x] * 2 * inv3;
                dp[y + 1][x] += dp[y][x] * 2 * inv3;
            }
            dp[y][x + 1] %= MOD;
            dp[y + 1][x] %= MOD;
        }
    }

    cout << dp[h - 1][w - 1] << endl;
}

#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9;
int MOD = 1e9 + 7;
const ll LINF = 1e18;

const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

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

inline int mul (int a, int b, int m = MOD) {
    return ((ll)a * b) % m;
}

inline int binpow (int a, int n, int m = MOD) {
    int ans = 1;
    for (; n; n >>= 1) {
        if (n & 1) ans = mul(ans, a, m);
        a = mul(a, a, m);
    }
    return ans;
}

inline void add (int &a, int x, int m = MOD) {
    a += x;
    if (a >= m) a -= m;
    if (a < 0) a += m;
}

inline int sum (int a, int b, int m = MOD) {
    a += b;
    if (a >= m) a -= m;
    if (a < 0) a += m;
    return a;
}

inline int inv (int x, int m = MOD) {
    return binpow(x, m - 2, m); /// only if m is prime
}

vector <int> f, fi;

inline void precalc_f (int n, int m = MOD) {
    f.resize(n);
    fi.resize(n);
    f[0] = 1;
    for (int i = 1; i < n; i++) {
        f[i] = mul(f[i - 1], i, m);
    }
    fi[n - 1] = inv(f[n - 1], m);
    for (int i = n - 2; i >= 0; i--) {
        fi[i] = mul(fi[i + 1], i + 1, m);
    }
}

inline int A (int n, int k, int m = MOD) {
    return mul(f[n], fi[n - k], m);
}

inline int C (int n, int k, int m = MOD) {
    return mul(A(n, k), fi[k], m);
}

const int N = 101;
int dp[N][N * N * (N + 1) / 2] = {};
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k >> MOD;
    dp[0][0] = 1;
    for (int x = 1; x <= n; x++) {
        vector <int> S(x);
        for (int s = 0, y = 0; s <= k * n * (n + 1) / 2; s++) {
            add(S[y], dp[x - 1][s]);
            if ((k + 1) * x <= s) {
                add(S[y], -dp[x - 1][s - (k + 1) * x]);
            }
            dp[x][s] = S[y];
            add(y, 1, x);
        }
    }
//    for (int i = 1; i <= n; i++) {
//        for (int j = 0; j <= k * n * (n + 1) / 2; j++) {
//            cout << dp[i][j] << ' ';
//        }
//        cout << '\n';
//    }
    for (int x = 1; x <= n; x++) {
        int ans = 0;
        for (int s = 1; s <= k * n * (n + 1) / 2; s++) {
            add(ans, mul(dp[x - 1][s], dp[n - x][s]));
        }
        ans = mul(ans, k + 1);
        add(ans, k);
        cout << ans << '\n';
    }
}


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

using namespace std;

int main(){
    int N, K, M;
    cin >> N >> K >> M;
    int sumA = 0;
    for(int i = 0; i < N-1; ++i){
        int tmp ;
        cin >> tmp;
        sumA += tmp;
    }
    for(int i = 0; i <= K; ++i){
        if((sumA + i) / N >= M){
            cout << i << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for(int (i)=0; (i)<(n); (i)++)
#define REP2(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1,) (__VA_ARGS__)
#define MOD 1000000007

template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(size_t i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }

int main(){
    int N, K, M;
    cin >> N >> K >> M;
    vector<int> A(N-1);
    REP(i, N-1) cin >> A[i];
    int sm = 0;
    REP(i, N-1) sm += A[i];
    int rem = max(0, M*N - sm);
    if(rem > K) cout << -1 << endl;
    else cout << rem << endl;
    return 0;
}


//BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> pii;
typedef vector<ii> vii;
typedef priority_queue<int,vector<int>,greater<int> > PQ;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int setBit(int mask, int pos){return mask = mask | (1<<pos);}
bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));}

ll arr[45];

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

    int n;

    ll t;

    cin>>n>>t;

    for(int i = 0; i < n; i++)
        cin>>arr[i];

    int k = n / 2;
    set<ll> s1;
    ordered_set s2;

    for(int i = 0; i < (1<<k); i++) {
        ll sum = 0;
        for(int j = 0; j < k; j++) {
            if(checkBit(i, j))
                sum += arr[j];
        }

        s1.insert(sum);
    }
    s1.insert(0);

    int rem = n - k;
    for(int i = 0; i < (1<<rem); i++) {
        ll sum = 0;
        for(int j = 0; j < rem; j++) {
            if(checkBit(i, j))
                sum += arr[k+j];
        }
        s2.insert(sum);
    }
    s2.insert(0);

    ll ans = 0;
    for(set<ll> :: iterator it = s1.begin(); it != s1.end(); it++) {
        ll tx = *it;
        if(tx <= t) {
            ll rm = t - tx;
            int pos = s2.order_of_key(rm);
            if(s2.find_by_order(pos) != s2.end()) {
                if(*s2.find_by_order(pos) == rm) {
                    ans = max(ans, tx + rm);
                }
            }
            if(pos > 0) {
                pos--;
                ans = max(ans, tx + *s2.find_by_order(pos));
            }
        }
        else {
            break;
        }
    }

    cout<<ans<<endl;

    return 0;
}

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

ll fact(int a, int b){
    ll res = 1;
    rep(i, b) res *= a;
    return res;
}

int main(){
    double k; cin >> k;
    string s, t;
    cin >> s;
    cin >> t;

    //二人のスコア
    ll tscore = 0;
    ll ascore = 0;

    //枚数の情報
    vector<ll> ta(10, 0);
    vector<ll> ao(10, 0);
    vector<ll> used(10, 0);//二人の情報の合計
    rep(i, 4){
        ta[s[i] - '0']++;
        used[s[i] - '0']++;
        ao[t[i] - '0']++;
        used[t[i] - '0']++;
    }

    //現在の得点
    rep(i, 10){
        tscore += i * fact(10, ta[i]);
        ascore += i * fact(10, ao[i]);
    }

    double win = 0.0;
    double sum = 9.0*k - 8.0;//残りカード枚数

    for(int i = 1; i <= 9; i++){
        ll tak = tscore;
        if(used[i] == k) continue;//1枚も残っていない

        int x = k - used[i];//iの残り枚数
        used[i]++;
        tak -= i * fact(10, ta[i]);
        tak += i * fact(10, ta[i] + 1);
        for(int j = 1; j <= 9; j++){
            if(used[j] == k) continue;

            int y = k - used[j];
            used[j]++;
            ll aok = ascore;
            aok -= j * fact(10, ao[j]);
            aok += j * fact(10, ao[j] + 1);

            if(tak > aok) {
                win += (double)(x) / (double) sum * (double)(y);
                //cout << tak << ", " << aok << " (i, j) = " << i << " " << j << endl;
            }
            //sum++;
            used[j]--;

        }
        used[i]--;
    }
    //cout << win << endl;
    //cout << sum << endl;
    cout << fixed << setprecision(15) << (double)(win) / (double)(sum-1.0) << endl;
    return 0;
}


//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>

#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long

#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<int,int>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }

#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,1} ,{-1,0},{1,1},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 1e9 + 7;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }

vector<int> adj[max_v];
vector<int> edge[max_v];
int t=-1;
setl in[max_v];

void dfs(int u,int d=0)
{
	in[u].first = ++t;
	edge[d].push_back(in[u].first);
	for (auto& v : adj[u]) dfs(v,d+1);
	in[u].second = t;
}

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int v; ci(v);

	fa(i, 1, v)
	{
		int x; ci(x); --x;
		adj[x].push_back(i);
	}

	dfs(0);

	fa(i, 0, v) sort(edge[i].begin(), edge[i].end());

	int q; ci(q);
	while (q--)
	{
		int x, y; ci(x >> y); --x;

		int ret = upper_bound(edge[y].begin(), edge[y].end(), in[x].second) - lower_bound(edge[y].begin(), edge[y].end(), in[x].first);
		co(ret << "\n");
	}

	return 0;
}

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

#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

const int maxn=2e5+5;

int n,siz[maxn],dep[maxn],idx,id[maxn];
vector <int> g[maxn],t[maxn];

void dfs(int u) {
	siz[u]=1; id[u]=++idx;
//	printf("co %d %d\n",u,id[u]);
	t[dep[u]].push_back(id[u]);
	for(int i=0;i<g[u].size();++i) {
		int v=g[u][i]; dep[v]=dep[u]+1;
		dfs(v);
		siz[u]+=siz[v];
	}
}

int cal(int op,int L,int R) {
	int l=0,r=t[op].size()-1,mid,ansl=-1,ansr=-1;
//	rep(i,l,r) print(t[op][i],'\n');
//	printf("HHH %d %d\n",L,R);
	while(l<=r) {
		mid=l+r>>1;
		if(t[op][mid]>=L) ansl=mid,r=mid-1;
		else l=mid+1;
	}
	l=0,r=t[op].size()-1;
	while(l<=r) {
		mid=l+r>>1;
		if(t[op][mid]<=R) ansr=mid,l=mid+1;
		else r=mid-1;
	}
	if(ansl==-1 || ansr==-1) return 0;
	return ansr-ansl+1;
}

int main() {
	n=read(9);
	rep(i,2,n) g[read(9)].push_back(i);
	dfs(1);
	rep(q,1,read(9)) {
		int x=read(9),y=read(9);
		print(cal(y,id[x],id[x]+siz[x]-1),'\n');
	}
	return 0;
}


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

int main(){
    long long n;
    cin >> n;
    double N = sqrt(n);
    int count = 0;
    /*vector<long long> vec(n, 2000000000000);*/
    priority_queue<long long, vector<long long>, greater<long long>> Q1;
    for (int i = 1; i <= N; i++){
        if (n % i == 0 && i != n /i){
            Q1.push(i);
            Q1.push(n / i);
            /*vec[i] = i;
            vec[n / i] = n / i;*/
            count += 2;
        }
      	if (n % i == 0 && i == n /i){
            Q1.push(i);
            count++;
        }
    }
    /*sort(vec.begin(), vec.end());*/

    /*for(int i = 0; i < count; i++){*/
    while(count > 0){
        cout << Q1.top() << endl;
        Q1.pop();
        count--;
    }

}

#include <bits/stdc++.h>
using namespace std;
// clang-format off
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353

using ll = int64_t;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto& a : v) cin >> a;
    return is;
}
template <class T>
istream& operator>>(istream& is, vector<pair<T, T>>& v) {
    for (auto& a : v) cin >> a.first >> a.second;
    return is;
}
template <class T>
istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) {
    for (auto& a : v) {
        T a1, a2, a3;
        cin >> a1 >> a2 >> a3;
        a = {a1, a2, a3};
    }
    return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
    for (auto& a : v) os << a << " ";
    os << endl;
    return os;
}
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;
}
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define TRC1(a) cout << #a ":" << a
#define TRC2(a, b) TRC1(a); cout << " "; TRC1(b)
#define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c)
#define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d)
#define GET_NAME(_1,_2,_3,_4,NAME,...) NAME
#define TRC(...) GET_NAME(__VA_ARGS__, TRC4, TRC3, TRC2, TRC1) (__VA_ARGS__) << endl
using veci = vector<int>;
using vecll = vector<ll>;
using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
const int IINF = INT32_MAX;
const ll LINF = INT64_MAX;

// cout << fixed << setprecision(15);
void solve();
int main() {
    cin.tie(0); ios::sync_with_stdio(0);
    cout << fixed << setprecision(15); solve(); return 0; }
// clang-format on

void calc(int n) {
    int i = 1;
    veci a;
    while (n > 0) {
        if (n & 1) a.push_back(i);
        ++i;
        n >>= 1;
    }
    cout << a.size() << " ";
    for (int x : a) {
        cout << x << " ";
    }
    cout << endl;
}

void solve() {
    int N;
    cin >> N;
    veci A(N);
    cin >> A;
    N = min(N, 8);
    veci s(200);
    rep(i, 1, 1 << N) {
        int sum = 0;
        rep(j, N) if (i >> j & 1) sum = (sum + A[j]) % 200;
        if (s[sum] == 0) s[sum] = i;
        else {
            cout << "Yes" << endl;
            calc(s[sum]);
            calc(i);
            return;
        }
    }
    cout << "No" << endl;
}


#include<bits/stdc++.h>
using namespace std;
long long int power(long long  int x,long long int y,long long  int p)
{
    long long int res = 1;

    x = x % p;
    if (x == 0) return 0;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int gcdExtended(long long int a,long long int b, long long int *x,long long  int *y)
{
	if (a == 0)
	{
		*x = 0, *y = 1;
		return b;
	}

	long long int x1, y1;
	long long int gcd = gcdExtended(b%a, a, &x1, &y1);
	*x = y1 - (b/a) * x1;
	*y = x1;
    return gcd;
}
long long int modInverse( long long int b,long long int m)
{
	long long int x, y;
	long long int g = gcdExtended(b, m, &x, &y);
	if (g != 1)
		return -1;
	return (x%m + m) % m;
}
long long modDivide(long long int a,long long int b,long long int m)
{
	a = power(10,a,m);
	long long int inv = modInverse(b, m);
	return (inv*a)%m;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int mod=1000000007;
    int t=1;
//    cin>>t;
    while(t--){
      int n;
      cin>>n;
      vector<int> arr(n);
      for(int i=0;i<n;i++){
      	cin>>arr[i];
	  }
	  int sum=0;
	  for(int i=0;i<n;i++){
	  	int b;
	  	cin>>b;
	  	arr[i]*=b;
	  	sum+=arr[i];
	  }
	  if(sum==0){
	  	cout<<"Yes\n";
	  }
	  else
	  cout<<"No\n";
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=2e5+1,mod=998244353;
V check(int&x){x-=mod,x+=x>>31&mod;}
V cmax(int&x,int y){if(x-y>>31)x=y;}
V cmin(int&x,int y){if(y-x>>31)x=y;}
ll n,m;
V input(){
	cin>>n;
	m=sqrt(n);
	FOR(i,1,m)if(n%i==0)
		cout<<i<<'\n';
	if(n/m==m)m--;
	ROF(i,m,1)if(n%i==0)
		cout<<n/i<<'\n';
}
V init(){

}
V work(){

}
int main(){
//	for(scanf("%d",&T);T--;){
		input();
		init();
		work();
//	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

void solve()
{
    int k, n, m;
    cin >> k >> n >> m;
    vector<int> A(k);
    for(int &x: A)
        cin >> x;
    vector<int> Aux(k);
    int s = 0;
    vector<pii> Ps_B(k);
    F(int, i, 0, k - 1, 1){
        s += ((A[i] * m) % n);
        Ps_B[i].first = ((A[i]*m) % n);
        Ps_B[i].second = i;
    }
    s /= n;
    sort(all(Ps_B));
    RF(int, i, s, 1, 1){
        Ps_B[k - i].first -= n;
    }
    vector<int> fin_ans(k);
    F(int,i, 0, k - 1, 1){
        fin_ans[Ps_B[i].second] = ((A[Ps_B[i].second] * m) - Ps_B[i].first) / n;
    }
    for(int x: fin_ans){
        cout << x << ' ';
    }
    cout << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        // freopen("Debug.txt", "w", stderr);
    #else
    #endif
    // cout << fixed << setprecision(10);
    // int _t;
    // cin>>_t;
    // F(int, i, 1, _t, 1){
        // cout << "Case #" << i << ": ";
        solve();
    // }
}

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける
const ll llMAX=9223372036854775807LL;
const ll llMIN=-9223372036854775808LL;

void myprint1D(vi &data)
{
  REP(i,data.size())
    cout<<data[i]<<" ";
  cout<<endl;
}

//配列を[y][x]で表示
void myprint2D_T(vvi &data)
{
  REP(i,data.size())
    myprint1D(data[i]);
}

//配列を[x][y]で表示
void myprint2D(vvi &data)
{
  ll l1=data.size();
  ll l2=data[0].size();
  REP(j,l2){
    REP(i,l1)
      cout<<data[i][j]<<" ";
    cout<<endl;
  }
}

//print(a,b...)って使い方
void print1(ll a){cout<<a<<endl;}
void print2(ll a,ll b){cout<<a<<" "<<b<<endl;}
void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;}
void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;}
void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;}

//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
	ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
	if (t==0LL) return a/b;
		for(int i=0;i<900;i++){
      m=x0/x1;
      x2=x0-x1*m;
      n2=(n0-m*n1)%c;
      if (x2==1LL){
        ans=(n2+c)%c;
        break;
      }
      x0=x1;x1=x2;
      n0=n1;n1=n2;
    }
	return (a+((t*ans)%c)*b-t)/b;
}
//剰余の割ざん

//aのn乗 mod c
ll expmod(ll a,ll n,ll c){
  ll ans=1LL,aa=a,beki=n;
  for(int i=0;i<64;i++){
    if (beki%2==1LL) ans=ans*aa%c;
    aa=aa*aa%c;
    beki/=2LL;
    if (beki==0LL)break;
  }
  return ans;
}

//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
  mulmod[0]=1LL;
  for(ll i=1;i<500001;i++){
    mulmod[i]=mulmod[i-1]*i%MOD;
  }
}
ll nCk(ll nn,ll kk){
  return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}

ll modinv(ll a,ll b){
  return moddiv(1,a,b);
}

ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){
  __int128_t tmp=(br-ar)%b*modinv(a,b)%b;
  if (tmp<0)tmp+=b;
  return ar+tmp*a;
}

ll a_,b_,c_;

int main(){
  ll ans=0;
  cin >> a_>>b_>>c_;
/*
  a_=moddiv(a_*(a_+1LL)%MOD,2LL,MOD);
  b_=moddiv(b_*(b_+1LL)%MOD,2LL,MOD);
  c_=moddiv(c_*(c_+1LL)%MOD,2LL,MOD);
  */
 a_=(a_*(a_+1)/2)%MOD;
 b_=(b_*(b_+1)/2)%MOD;
 c_=(c_*(c_+1)/2)%MOD;
  ans=(((a_*b_)%MOD)*c_)%MOD;
  cout<<ans<<endl;
  return 0;
}


#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll MOD = 998244353;

#ifndef ONLINE_JUDGE
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;}
ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print_sim_py(void) {cout << endl;}
template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();}
template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);}

#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...);
#endif

template <typename... Ts>
std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); }

#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

#define Rrepr(i, a, b) for (int i = b; i >= a; i--)
#define Rrep(i, n) for (int i = n-1; i >= 0; i--)

bool dfs(ll p, vector<vector<ll>>& v,vector<ll>& fin_list, vector<ll>& cv,ll c, ll depth){
    bool closed = false;
    if(fin_list[p]){
        if(depth >= 3) return true;
        else           return false;
    }
    cv[p] = c;
    fin_list[p] = 1;
    rep(i, v[p].size()){
        if(v[p][i]){
            closed |= dfs(i, v, fin_list, cv, c, depth+1);
        }
    }
    return closed;
}

class Vsum2{
public:
    vector<vector<ll>> vsum;

    Vsum2(vector<vector<ll>>& a,
          function<ll (ll&)> func=[](auto& a){ return a; }){

        ll h = a.size();
        ll w = a[0].size();
        vsum.resize(h+1, vector<ll>(w+1, 0));

        repr(i,0,h){
            ll jsum = 0;
            rep(j,w){
                jsum += func(a[i][j]);
                vsum[i+1][j+1] = jsum + vsum[i][j+1];
            }
        }
        //print("#", vsum)
    }

    ll range(ll y1,ll x1, ll y2,ll x2){
        return (vsum[y2][x2] - vsum[y2][x1] - vsum[y1][x2] + vsum[y1][x1]);
    }
};

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

    ll n,k;
    IN(n,k);

    vector<vector<ll>> a(n, vector<ll>(n, 0));
    ll amax = 0;

    rep(i,n){
        rep(j,n){
            IN(a[i][j]);
            amax = max(amax, a[i][j]);
        }
    }

    ll l = -1 ,r = 1e9;
    while(l+1<r){
        ll m = (l+r)/2;
        bool ok = false;

        Vsum2 asum(a, [&](auto& a){ return a > m; });

        rep(i,n-k+1){
            rep(j, n-k+1){
                ll sumr = asum.range(i,j, i+k,j+k);
                if(sumr < k*k/2+1){
                    ok = true;
                }
            }
        }

        if(ok){
            r=m;
        }else{
            l=m;
        }
    }
    ll ans = r;

    cout << ans << endl;

    return 0;
}

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+3;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int n,a[N],b[N],f[N];
LL s[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin>>n;
  LL sum=0;
  for(int i=1;i<=n;i++)cin>>a[i]>>b[i],f[i]=i,sum+=a[i]+b[i];
  sort(f+1,f+1+n,[](int x,int y){
    return 2ll*a[x]+b[x]<2ll*a[y]+b[y];
  });
  int tot=n;
  //sum - b -2*x
  for(int i=1;i<=n;i++){
    if(sum-b[f[i]]-2ll*a[f[i]]>0){
      sum-=b[f[i]]+2ll*a[f[i]];
      tot--;
//      cout<<sum<<endl;
    }else break;

  }
  cout<<tot<<'\n';
  return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll long long
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
//const double pi=acosl(-1);

void solve()
{
    int n;
    cin>>n;
    vector<pair<int,int>> a(n);
    fi(0,n) cin>>a[i].first>>a[i].second;
    int m;
    cin>>m;
    vector<pair<int,int>> op(m);
    fi(0,m)
    {
        cin>>op[i].first;
        if(op[i].first>2) cin>>op[i].second;
    }
    int Q;
    cin>>Q;
    vector<ar<int,3>> q(Q);
    fi(0,Q)
    {
        cin>>q[i][0]>>q[i][1];
        --q[i][0],--q[i][1];
        q[i][2]=i;
    }
    sort(all(q));
    vector<pair<int,int>> ans(Q);
    int j=0;
    while(j<Q&&q[j][0]==-1)
    {
        ans[q[j][2]]=a[q[j][1]];
        j++;
    }
    int rot=0;
    int flipx=0,flipy=0;
    int sx=1,sy=1;
    fi(0,m)
    {
        if(op[i].first>2)
        {
            if(op[i].first==3)
            {
                sx=-sx;
                flipx=2*op[i].second-flipx;
            }
            else
            {
                sy=-sy;
                flipy=2*op[i].second-flipy;
            }
        }
        else
        {
            if(op[i].first==1)
            {
                // rot++;
                swap(flipx,flipy);
                swap(sx,sy);
                rot=!rot;
                sy=-sy;
                flipy=-flipy;
            }
            else
            {
                // rot--;
                swap(flipx,flipy);
                swap(sx,sy);
                rot=!rot;
                sx=-sx;
                flipx=-flipx;
                // if(rot<0) rot+=4;
            }
        }
        while(j<Q&&q[j][0]==i)
        {
            int x=a[q[j][1]].first;
            int y=a[q[j][1]].second;
            //apply
            if(rot==1)
            {
                swap(x,y);
            }
            ans[q[j][2]]={x*sx+flipx,y*sy+flipy};
            j++;
        }
    }
    for(auto temp:ans)
    {
        cout<<temp.first<<' '<<temp.second<<'\n';
    }

}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;
    while(tt--)
    {
        solve();
    }
}

///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define     FAST        ios_base::sync_with_stdio(false);cin.tie(NULL)
#define     ll 	        long long
#define 	pb 	        push_back
#define     pp          pop_back
#define 	mp 	        make_pair
#define 	pi 	        2*acos(0.0)
#define     scan(i)     scanf("%lld",&i)
#define     scann(i,j)  scanf("%lld%lld",&i,&j)
#define     print(i)    printf("%lld\n",i)
#define     printt(i,j) printf("%lld %lld\n",i,j)
#define     loop(i,n) 	for(int i = 0 ; i <n ; i++)
#define     loopp(i,n) 	for(int i = 0 ; i <n-1 ; i++)
#define     rep(i,n)    for(int i=n-1;i>=0;i--)
#define     rev(v)      reverse(v.begin(),v.end())
#define     reset(a,b)  memset(a,b,sizeof(a))
#define 	vect 	    vector<ll>
#define 	vec_it 	    vector<ll>::reverse_iterator vi
#define     sorta(v)    sort(v.begin(),v.end())
#define     sortd(v)    sort(v.begin(),v.end(),greater<>())
#define     sorted(v)   is_sorted(v.begin(),v.end())
#define 	mapp 	    map<ll,ll>
#define     rmap        map<ll,ll,greater<>>m
#define 	pairr 	    pair<ll,ll>
#define 	sett	    set<ll>
#define     rset        set<ll,greater<>>s
#define 	set_it 	    set<ll>::reverse_iterator si
#define     map_it      map<ll,ll> :: reverse_iterator mi
#define     digit(n)    floor(log10(n)+1)
#define 	yes 	    cout<<"YES"<<endl
#define 	no 	        cout<<"NO"<<endl
#define 	ff 	        first
#define 	ss 	        second
#define     mx          INT_MAX
#define     lmx         LLONG_MAX
#define     mn          INT_MIN
#define     lmn         LLONG_MIN
#define     inf         0x3f3f3f3f
#define     inff        10e18
#define     GCD         __gcd(a,b)
#define     LCM         (a/GCD)*b
template <typename T> T gcd(T a,T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
ll modulo (ll a, ll b) { return a >= 0 ? a % b : ( b - abs ( a%b ) ) % b; }

void solve()
{
   // freopen ("(input).txt" , "r",  stdin) ;
    string st,stt;
    ll low=0,high=0;
    ll l,r; char ch;
    ll n,k,t,x,y,z,d=0,cnt=0,temp=0,sum=0,f=0,flag=0;
    cin>>n>>k>>x>>y ; vect v ,a ; sett s ; mapp m,mm ; pairr p ;
    if(n==k)
        cout<<x<<endl;
    else if(n>k)
    {
        sum+=x;d=n;
        d--;
        if(2*x<=y)
            sum+=(d-k)*2*x;
        else
            sum+=(d-k)*y;
        cout<<sum<<endl;
    }
    else
    {
        sum+=x;
        if(2*x<=y)
            sum+=(k-n)*2*x;
        else
            sum+=(k-n)*y;
        cout<<sum<<endl;
    }

}

main()
{
    int t;
  //  cin>>t;
 //   while(t--)
        solve();
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;

#define pq_max          priority_queue<ll>
#define pq_min          priority_queue<ll,vi,greater<ll>>
#define iint int
#define f(i,a,b)        for(ll i=a;i<b;i++)
#define f0(i,n)         f(i,0,n)
#define fr(i,a,b)       for(ll i=a;i>=b;i--)
#define for1(i,n)       for(ll i=1;i<=n;i++)
#define pb              push_back
#define tc(t)           int t;cin >>t;while(t--)
#define lol             std::ios::sync_with_stdio(false); cin.tie(NULL);
#define endl            "\n"
#define ss              second
#define ff              first
#define ll              long long
#define lld             long double
#define int             long long
#define pii             pair< int,int >
#define pll             pair< ll,ll >
#define sz(a)           a.size()
const long long  inf = 1e18;
#define all(a)          a.begin(),a.end()
#define arr(a,n)        int a[n];f0(i, n) cin >> a[i];
#define ini(a,n)        memset(a,n,sizeof(a))
#define printArr(a,n)   f0(i,n) cout<<a[i]<<" ";
#define in              cin>>
#define rr              return 0;
#define vi              vector< int >
#define vs              vector<string>
#define l_b             lower_bound
#define u_b             upper_bound
#define mod             1000000007
#define pi              3.141592653589793238
#define fmap(m)         for(auto it:m)
#define deb(args...)    { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define debarr(arr,a,b) for(int i=(a);i<(b);i++) cout<<(arr[i])<<" ";cout<<endl;
#define long_max        numeric_limits<ll>::max()
vs tokenizer(string str, char ch) {std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) {v.pb(t);} return v;}

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}
int dx[8] = { +1, +1, +1, 0, 0, -1, -1, -1};
int dy[8] = { +1, 0, -1, +1, -1, +1, 0, -1};

int dx4[4] = { +1 , -1 , +0 , +0};
int dy4[4] = { 0  , 0 , +1 , -1};

int dx2[2] = {0, 1};
int dy2[2] = {1, 0};

const int N = 1e4 + 10;

void solve()
{
    vector<vi>ncr(61 , vi(61, 0));
    ncr[0][0] = 1 ;

    for (int i = 1; i < 61; i++)
    {
        ncr[i][0] = 1;
        for (int j = 1 ; j < i; j++)
            ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j];
        ncr[i][i] = 1;
    }

    int a, b, k;
    cin >> a >> b >> k;
    int n = a + b;
    f0(i, n)
    {
        int cnt = ncr[a - 1 + b][b];
        if (cnt >= k) //we put a
        {
            cout << 'a';
            a--;
        }
        else
        {
            cout << 'b';
            b-- ;
            k -= cnt;
        }
    }
    cout << '\n';
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    lol;

    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}

#include<bits/stdc++.h>

typedef   long long  int ll;
typedef  long double ld;
typedef std::vector<ll> vi;
typedef std::vector<std::vector<ll> > vv;
typedef std::vector<std::pair<ll,ll> > pii;
typedef std::pair<ll,ll> pi;

#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,b) for(i=a;i<b;i++)
#define forr(i,a,b) for(i=a;i>=b;i--)
#define mp make_pair
#define pb(x) push_back(x)
#define fi first
#define se second
#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;
#define all(a) a.begin(),a.end()
#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];
#define ms(a,val) memset(a,val,sizeof(a))
using namespace std;

const int N = 1e5+ 5;

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

ll modInverse(ll a, ll m = mod) {
	return expo_pow(a, m - 2);
}

void solve(){
            ll i,j,n,m,k;
            cin>>n;
            vi t(n,0);
            input(t,0,n);

            ll pos[N] = {0};
            pos[0] = 1;

            fo(j,0,n)
            {
                fo(i,0,N)
                {
                    if(i + t[j] >= N)
                     continue;
                    if(pos[i] == 1 && pos[i+t[j]] == 0)
                        pos[i+t[j]] = 2;
                }
                fo(i,0,N)
                {
                    if(i + t[j] >= N)
                     continue;
                    if(pos[i+t[j]] == 2)
                        pos[i+t[j]] = 1;
                }
            }

            ll sum = 0;

            fo(i,0,n)
             sum+=t[i];

            ll val = ceil(1.0*sum/2);

            fo(i,val,N-1)
            {

                if(pos[i] == 1)
                {
                    cout<<i<<endl;

                    return;
                }
            }

}

int main()
{
     //IO;
      ll t=1,i;
      //cin>>t;

      while(t--)
      {

          solve();
      }
    return 0;
}


/* How To Solve It
 1st. Understanding The Problem
    未知なもの、データ(与件), 条件 は何か？
    その条件は未知を決定するために十分か？
    条件を分解せよ, 図を描け
 2nd. Devising A Plan
    類似、関連のある問題、典型問題を知らないか？
    役立ちそうな定理やアルゴリズムを知らないか？
    問題の一部を解決できるか、一部だけ残して他を切り捨ててみろ
    既知のものやデータを変えることはできるか？
 3rd. Carrying Out The Plan
    各ステップで誤りがないか、正しいと説明できるか？
 4th. Looking Back
    別解、他の問題に使えそうなものはあるか？
*/
/*{{{ #include */
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <tuple>
#include <algorithm>
#include <utility>
#include <cmath>/*}}}*/
/*{{{ namespace R2357 */
namespace R2357 {
    inline void IN(void){return;}
    template<class F,class... R>inline void IN(F& f,R&... r){std::cin>>f;IN(r...);}
    template <class T>inline void OUT(T x){std::cout<<x<<'\n';}
    template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
    template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
    template<class T=int>T read(){
        T x=0; char c;
        while(((c=getchar())>'9'||c<'0')&&c!='-');
        const bool f=(c=='-')&&(c=getchar());
        while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
        return f?-x:x;
    }
    template<class T>std::istream& operator>>(std::istream& is,std::vector<T>& V){
        for(auto& v:V)is>>v; return is;
    }
    template<class T1,class T2>
    std::istream& operator>>(std::istream& is,std::pair<T1,T2>& P){
        return is>>P.first>>P.second;
    }
    template<class T>inline T ceil_div(T a,T b){
    	return (a+(b-1))/b;
    }
    namespace debug {
        using namespace std;
        void Write(void){cerr<<endl;}
        template<class F,class...R>void Write(F f,R...r){cerr<<' '<<f;Write(r...);}
        template<class T>void Write(vector<T>& V,int n=-1){
            if(n==-1) n = V.size();
            for(int i=0;i<n;i++)cerr<<' '<<V[i]; cerr<<endl;
        }
        template<class T,class U> void Write(pair<T,U>& p){cerr<<' '<<'{'<<p.first<<','<<p.second<<'}'<<endl;}
        #include <cxxabi.h>
        template<class T>string TypeName(T& x){
            return string(abi::__cxa_demangle(typeid(T).name(), 0, 0, 0));
        }
    }
}
/*}}}*/
/*{{{ using */
using namespace std; using namespace R2357;
using ll=long long; using ld=long double;
using pint=pair<int,int>;/*}}}*/
/*{{{ #define */
#define rep(i,a,b) for(ll i=a;i<ll(b);i++)
#define repr(i,a,b) for(ll i=a;i>=ll(b);i--)
#define each(x,v) for(auto& x:v)
#define el '\n'
#define ALL(x) x.begin(),x.end()
#define ALLR(x) x.rbegin(),x.rend()
#define Unique(x) x.erase(unique(x.begin(),x.end()),x.end())
#define Fill(v,n) fill(v,v+sizeof(v)/sizeof(*v),n)
#define max_element(x) *max_element(x.begin(),x.end())
#define min_element(x) *min_element(x.begin(),x.end())
#define INF 1073741824 // 2e30
#define INFL 2305843009213693952ll // 2e61
#define _FASTIO_ ios::sync_with_stdio(false),cin.tie(nullptr);
/*}}}*/

/* NOTE
 0.
*/
// using namespace debug;

vector<pair<char, int>> RunLength_encode(const string& s) {
    vector<pair<char, int>> res;
    ll len = s.size();
    ll l = 0;
    while(l < len){
        int r = l + 1;
        while(r < len && s[l] == s[r]) r++;
        res.push_back({s[l], r-l});
        l = r;
    }
    return res;
}
int main(){ _FASTIO_
    int n, m; IN(n, m);
    string f(n, '.');
    rep(_, 0, m) f[read()-1] = '#';
    auto r = RunLength_encode(f);

    int k = INF;
    each(x, r) if(x.first == '.') chmin(k, x.second);
    ll ans = 0;
    each(x, r) if(x.first == '.') ans += ceil_div(x.second, k);
    OUT(ans);
    return 0;
}


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

#define rep(i, a, b)        for(register int (i) = (a); (i) <= (int)(b); (i)++)
#define repd(i, b, a)       for(register int (i) = (b); (i) >= (int)(a); (i)--)
#define mset(a, x)          memset((a), (x), sizeof(a))
#define x                   first
#define y                   second

#define int                 long long    // int 变成 long long
#define LOCAL

typedef pair<int, int> pii;
typedef long long ll;

// 重载to_string
template <typename A, typename B>string to_string(pair<A, B> p);template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);string to_string(const string& s) {  return '"' + s + '"';}string to_string(const char* s) {  return to_string((string) s);}string to_string(bool b) {  return (b ? "true" : "false");}string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {    if (!first) {      res += ", ";    }    first = false;    res += to_string(v[i]);  }  res += "}";  return res;}template <size_t N>string to_string(bitset<N> v) {  string res = "";  for (size_t i = 0; i < N; i++) {    res += static_cast<char>('0' + v[i]);  }  return res;}template <typename A>string to_string(A v) {  bool first = true;  string res = "{";  for (const auto &x : v) {    if (!first) {      res += ", ";    }    first = false;    res += to_string(x);  }  res += "}";  return res;}template <typename A, typename B>string to_string(pair<A, B> p) {  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
// 声明debug    不能打印普通数组, 自定义结构体
void debug_out() { cout << endl; }template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {  cout << " " << to_string(H);  debug_out(T...);}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 404
#endif
// read  ---  只能输入整数数字(int , ll)
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}  // read end
///////////////////////////////   ABOUT  ///////////////////////////
/*

*/
////////////////////////////// START CODE ///////////////////////

const int mod = 1e9 + 7;
const int maxn = 1000 + 10;

int n, m;

int a[maxn], b[maxn];
int vis[maxn];

void solve(){

	read(n, m);

	rep(i, 1, n){
		read(a[i]);
		vis[a[i]] ++;
	}
	rep(i, 1, m){
		read(b[i]);
		vis[b[i]] ++;
	}

	rep(i, 1, maxn){
		if(vis[i] == 1){
			cout << i << ' ';
		}
	}

}

signed main(){

#ifdef LOCAL
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
//    debug("LOCAL");
#endif

    int T = 1;
    // cin >> T;
    rep(i, 1, T){
        // cout << "Case #" << i << ": ";
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;
//using namespace __gnu_pbds;

//template<typename T> using indexed_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());//uniform_int_distribution<int> (l, r)

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 1003;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

char c[2][2];

int n;

void add(int &a, int b){
    a += b;
    if (a >= MOD) a -= MOD;
}

map<string, bool> w;

int cnt = 0;

void go(string s){
    if (w[s]) return;
    w[s] = 1;
    if (sz(s) == min(n, 10)){
        cnt++;
        return;
    }
    for (int i = 1; i < sz(s); i++){
        string t = s;
        t.insert(t.begin()+i, c[s[i-1]-'A'][s[i]-'A']);
        go(t);
    }
}
int f[N];
void solve(){
    cin >> n;
    f[3] = f[2] = 1;
    for (int i = 4; i <= n; i++){
        f[i] = (f[i-1] + f[i-2]) % MOD;
    }
    //cout << n;

    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            char x;
            cin >> c[i][j];

        }
    }
    go("AB");
    if (n <= 10){
        cout << cnt;
        return;
    }
    if (cnt == f[10]){
        cout << f[n];
        return;
    }
    if (cnt == 1){
        cout << 1;
        return;
    }
    int ans = 1;
    for (int i = 4; i <= n; i++){
        ans = (ans+ans)%MOD;
    }
    cout << ans;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; i++){
        //cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef KAZAKH
      //  cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
    #endif
    return 0;
}


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

#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a, b) memset(a, b, sizeof(a))
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }

template<class T> void DBG(T&& x) { cerr << x << " "; }
template<class T, class...Args> void DBG(T&& x, Args&&... args) { DBG(x); DBG(args...); }
#define DBG(...) { cerr << "[" << #__VA_ARGS__ << "]: "; DBG(__VA_ARGS__); cerr << endl; }
template<class num> inline void rd(num& x) { cin >> x; }
template <class Ty, class... Args> inline void rd(Ty& x, Args&... args) { rd(x); rd(args...); }
template<class num> inline void print(num&& x) { cout << x; }
template <class Ty, class... Args> inline void print(Ty&& x, Args&&... args) { print(x); print(' '); print(args...); }
#define print(...) { print(__VA_ARGS__); print('\n'); }

struct Solver {
	const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f;

	inline void run_test(int test_case) {
		int n; rd(n);
		vector<pair<string, bool>> a(n);
		for (int i = 0; i < n; i++) {
			string s; rd(s);
			if (s[0] == '!') {
				s.erase(s.begin(), s.begin()+1);
				a[i] = {s, true};
			} else {
				a[i] = {s, false};
			}
		}

		sort(a.begin(), a.end());

		string ans = "satisfiable";
		for (int i = 0; i + 1 < n; i++) {
			if (a[i].fi == a[i+1].fi && a[i].se != a[i+1].se) ans = a[i].fi;
		}

		print(ans);
	}

	inline void prepare() { // first thing to run
		ios::sync_with_stdio(false); cin.tie(nullptr);
	}
};

/* **************************************************** */

// CHANGE HERE if there are multiple test cases
const enum TEST_TYPE { SINGLE, MULTIPLE, MULTIPLE_EOF } test_type = SINGLE;

inline void go_for_it() {
	if (test_type == SINGLE) {
		Solver solver;
		solver.prepare();
		solver.run_test(1);
	} else if (test_type == MULTIPLE) {
		Solver solver;
		solver.prepare();
		int test_count; rd(test_count);
		for (int test_number = 1; test_number <= test_count; test_number++)
			solver.run_test(test_number);
	} else {
		Solver solver;
		solver.prepare();
		for (int test_number = 1;; test_number++)
			solver.run_test(test_number);
	}
}

int main() {
    go_for_it();
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define INF LONG_MAX/3
//#define DIV 1000000007
//#define DIV 998244353

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; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

struct UnionFind
{
  // par[i]：データiが属する木の親の番号。i == par[i]のとき、データiは木の根ノードである
  vector<int> par;
  // sizes[i]：根ノードiの木に含まれるデータの数。iが根ノードでない場合は無意味な値となる
  vector<int> sizes;

  UnionFind(int n) : par(n), sizes(n, 1) {
	  // 最初は全てのデータiがグループiに存在するものとして初期化
	  for(int i = 0; i < n; i++){
		  par[i] = i;
	  }
  }

  // データxが属する木の根を得る
  int find(int x) {
    if (x == par[x]) return x;
    return par[x] = find(par[x]);  // 根を張り替えながら再帰的に根ノードを探す
  }

  // 2つのデータx, yが属する木をマージする
  void unite(int x, int y) {
    // データの根ノードを得る
    x = find(x);
    y = find(y);

    // 既に同じ木に属しているならマージしない
    if (x == y) return;

    // xの木がyの木より大きくなるようにする
    if (sizes[x] < sizes[y]) swap(x, y);

    // xがyの親になるように連結する
    par[y] = x;
    sizes[x] += sizes[y];
    // sizes[y] = 0;  // sizes[y]は無意味な値となるので0を入れておいてもよい
  }

  // 2つのデータx, yが属する木が同じならtrueを返す
  bool same(int x, int y) {
    return find(x) == find(y);
  }

  // データxが含まれる木の大きさを返す
  int size(int x) {
    return sizes[find(x)];
  }
};

int main(){
	ll N;
	cin >> N;
	vector<double> X(N);
	vector<double> Y(N);
	rep(i, N) cin >> X[i] >> Y[i];

	double ok = 0;
	double ng = 200;
	while(ok + 0.00000001 < ng) {
		double mid = (ok + ng)/2.0;

		UnionFind uf(200);
		rep(i, N) if(Y[i] + mid >= 100) uf.unite(i, 100);
		rep(i, N) if(Y[i] - mid <= -100) uf.unite(i, 101);

		rep(i, N) rep(j, N) if( (X[i] - X[j]) * (X[i] - X[j]) + (Y[i] - Y[j]) * (Y[i] - Y[j]) <= mid * mid) uf.unite(i, j);

		if(uf.find(100) == uf.find(101)) {
			ng = mid;
		} else {
			ok = mid;
		}

	}
	printf("%.10f\n", ok/2);

}


#include<iostream>
#include<vector>
using namespace std;
int N,M;
vector<int>G[20];
int col[20];
bool dfs(int u) {
	for(int v:G[u]) {
		if(col[v]==-1) {
			col[v]=3-col[u];
			if(!dfs(v))return false;
		}
		else if(col[u]==col[v])	{
			return false;
		}
	}
	return true;
}

int main() {
	cin>>N>>M;
	vector<pair<int,int> >E(M);
	for(int i=0;i<M;i++) {
		int a,b;cin>>a>>b;
		a--,b--;
		E[i]=make_pair(a,b);
		G[a].push_back(b);
		G[b].push_back(a);
	}

	long ans=0;
	for(int k=0;k<1<<N;k++)	{
		for(int i=0;i<N;i++) {
			col[i]=-1;
			if(k>>i&1)col[i]=0;
		}
		bool ok=true;
		for(pair<int,int>e:E)if(col[e.first]==0&&col[e.second]==0) {
			ok=false;
			break;
		}
		if(!ok)continue;
		int cnt=0;
		for(int i=0;i<N;i++)if(col[i]==-1)	{
			col[i]=1;
			if(!dfs(i))
			{
				ok=false;
				break;
			}
			cnt++;
		}
		if(ok)ans+=1<<cnt;
	}
	cout<<ans<<endl;
}

#include <bits/stdc++.h>

#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())

using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin>>n>>m;
    vector<int> a(m), b(m);
    for (int i=0; i<m; i++) {
        cin>>a[i]>>b[i];
        a[i]--;
        b[i]--;
    }
    vector<int> c(n);
    for (auto& i : c) cin>>i;
    vector<bool> ab(m);
    vector<vector<pii>> g(n);
    for (int i=0; i<m; i++) {
        if (c[a[i]]>c[b[i]]) ab[i]=true;
        else if (c[a[i]] < c[b[i]]) ab[i]=false;
        else {
            g[a[i]].emplace_back(b[i], i);
            g[b[i]].emplace_back(a[i], i);
        }
    }
    vector<int> dep(n, -1);
    auto dfs=[&](auto& dfs, int u, int p) -> void {
        for (auto [v, e] : g[u]) {
            if (v==p) continue;
            if (dep[v]==-1) {
                dep[v]=dep[u]+1;
                ab[e]=a[e]==v;
                dfs(dfs, v, u);
            } else if (dep[u]>dep[v]) ab[e]=a[e]==v;
        }
    };
    for (int i=0; i<n; i++) {
        if (dep[i]==-1) {
            dep[i]=0;
            dfs(dfs, i, i);
        }
    }
    for (auto i : ab) cout<<(i ? "->" : "<-")<<'\n';
    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
typedef long long ll;
using namespace std;
#define prt(var) cout << (var) << endl;
#define rep(i,n) for(int i=0; i<int(n); i++)

int main() {
  int n;
  string s;
  cin>>n>>s;

  ll ans=0;

  for(int i=0; i<n-1; i++){
    int l=i, r=i+1;
    int ca=0, ct=0, cg=0, cc=0;
    while(l>=0 && r<=n-1){
      if(s[l]=='A') ca++;
      if(s[l]=='T') ct++;
      if(s[l]=='G') cg++;
      if(s[l]=='C') cc++;
      if(s[r]=='A') ca++;
      if(s[r]=='T') ct++;
      if(s[r]=='G') cg++;
      if(s[r]=='C') cc++;
      if(ca==ct && cc==cg){
        ans++;
      }
      l--; r++;
    }
  }

  prt(ans);
}

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

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

    int n, ans = 0;
    string s;
    cin >> n >> s;

    for (int i = 0; i < n; i++) {
        int cnt[26] = {};
        for (int j = i; j < n; j++) {
            cnt[s[j] - 'A']++;
            if (cnt[0] == cnt['T' - 'A'] && cnt['C' - 'A'] == cnt['G' - 'A'])
                ans++;
        }
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>

#define ll long long

using namespace std;
vector<ll> subsets(vector<int> a){
    int n = a.size();
    vector<ll> ret;
    for(int mask = 0; mask < (1<<n); mask++){
        ll sum = 0;
        for(int i=0; i<n; i++){
            if( (mask&(1<<i)) != 0){
                sum += a[i];
            }
        }
        ret.push_back(sum);
    }
    return ret;
}
int main() {
    int n, t;
    cin>>n>>t;
    int a[n];
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    vector<int> s1(a, a+(n/2));
    vector<int> s2(a+(n/2), a+n);
    vector<ll> sum1 = subsets(s1), sum2 = subsets(s2);
    sort(sum1.begin(), sum1.end());
    sort(sum2.begin(), sum2.end());
    ll mn = 1e15;
    for(ll sum: sum1){
        if(sum > t) continue;
        ll rem = t - sum;
        auto it = upper_bound(sum2.begin(), sum2.end(), rem);
        it--;
        rem -= *it;
        mn = min(mn, rem);
    }
    cout<<t-mn<<'\n';

}

#include<bits/stdc++.h>
#define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Freed freopen ("0in.txt","r",stdin);
#define Fout freopen ("0out.txt","w",stdout);
#define ll long long int
#define pb push_back
#define mp make_pair
#define pi acos(-1.0)
#define Mod 1000000007
#define limit 48
using namespace std;
ll D[limit],W;

void middle(ll pos,ll n,ll sum,vector<ll> &A)
{
    if(pos>n)
    {
        if(sum<=W) A.pb(sum);
        return ;
    }
    middle(pos+1,n,sum,A);
    middle(pos+1,n,sum+D[pos],A);
}

void solve(int t)
{
    ll n,i,j;
    scanf("%lld %lld",&n,&W);
    for(ll i=1; i<=n; i++)
    {
        scanf("%lld",&D[i]);
    }
    sort(D,D+n+1);
    vector<ll>A,B;
    A.pb(0);
    middle(1,n/2,0,A);
    middle(n/2+1,n,0,B);
    sort(A.begin(),A.end());
    ll mx=0,sz=A.size();
    for(i=B.size()-1; i>=0; i--)
    {
        n = (W-B[i]);
        ll l=0,h=sz-1,m;
        while(l<=h)
        {
            m = (l+h)/2;
            if(n>=A[m])
            {
                if((B[i]+A[m])<=W && mx<(B[i]+A[m]))
                    mx = B[i]+A[m];
                l = m+1;
            }
            else
            {
                h = m-1;
            }
        }
    }
    printf("%lld\n",mx);
    return ;
}
int  main()
{
//    Fast
//    Freed
//    Fout
    int t,tt=1;
   // cin >> tt;
    for(t=1; t<=tt; t++)
        solve(t);
    return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define pi pair<int, int>
#define ll long long int
#define pll pair<ll, ll>
#define ppll pair<pll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define vppll vector<pair<pll, ll>>
#define vvll vector<vector<ll>>
#define vvpll vector<vector<pll>>
#define repi(i,a,n) for(ll i=a;i<n;i++)
#define repr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define pr(i,v) for(auto i:v)
static int row[4] = {-1, 0, 0, 1};
static int col[4] = {0, -1, 1, 0};
static int dagb[4] = {1, -1, 1, -1};
static int daga[4] = {1, -1, -1, 1};
//its not the same code
// why dd you not understand;
using namespace std;

int main( )
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     #ifndef ONLINE_JUDGE
        freopen("input.txt" , "r"  ,stdin);
        freopen("ott.txt" , "w" ,stdout);
        freopen("error.txt" , "w" ,stderr);
     #endif

      ll t=1;
     // cin>>t;

   while(t--){
    string s;
      ll l,n,m,r,w=0,y=0,z,x=0,flag=0,k=-1, mn=1e10,e=0,cnt=0,f=INT_MAX,sum=0,de=1,mx=0,ans=0;
     cin>>x>>y>>z>>w;
        if(w*z-y<=0)cout<<-1<<endl;
        else{
          if(x%(w*z-y)!=0)cout<<x/(w*z-y)+1<<endl;
          else cout<<x/(w*z-y)<<endl;
        }

        }
   return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long   ll;

int main(){
    ll a, b, c, d;

    cin >> a >> b >> c >> d;

    ll lo = ll(1LL), hi = ll(1e6);
    ll r = -1LL;

    while(lo <= hi){
        ll x = (lo + hi) >> 1LL;

        if(a + x * b <= d * x * c){
            r = x;
            hi = x - 1;
        }else{
            lo = x + 1;
        }
    }

    printf("%lld\n", r);
    return 0;
}


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

const int N=4e5+1e3+7;

int C(int n,int m)
{
	if(n<m||n<0||m<0)
		return 0;
	long long ret=1;
	for(int i=n;i>=n-m+1;i--)
		ret=ret*i;
	for(int i=1;i<=m;i++)
		ret=ret/i;
	return ret%3;
}

int Lucas(int n,int m)
{
	if(n<3||m<3)
		return C(n,m);
	return C(n%3,m%3)*Lucas(n/3,m/3)%3;
}

int get(char c)
{
	return c=='R'?0:c=='B'?1:2;
}

int n;

char s[N];

string ans="RBW";

int main()
{
	scanf("%d",&n);
	scanf("%s",s);
	n--;
	int r=0;
	for(int i=0;i<=n;i++)
		(r+=Lucas(n,i)*get(s[i]))%=3;
	if(n&1)
		r=(3-r)%3;
	putchar(ans[r]);
}


#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    while(n--)
    {
        string s;
        cin >> s;
        string s1 = "atcoder";
        int ans1 = 1e9;
        for(int i = 0; i <= min(7, sz(s)); i++)
        {
            vector <bool> used(sz(s));
            int j = 0;
            int ans = 0;
            while(j < i)
            {
                bool fl = 0;
                for(int p = 0; p < sz(s); p++)
                {
                    if(s1[j] == s[p] && !used[p])
                    {
                        used[p] = 1;
                        fl = 1;
                        break;
                    }
                    if(!used[p])
                    {
                        ans++;
                    }
                }
                if(!fl)
                {
                    break;
                }
                j++;
            }
            if(j < i)
            {
                continue;
            }
            if(i == 7)
            {
                if(i != sz(s))
                {
                ans1 = min(ans1, ans);
                }
            }
            else
            {
                bool fl = 0;
                for(int p = 0; p < sz(s); p++)
                {
                    if(!used[p] && s[p] > s1[i])
                    {
                        fl = 1;
                        break;
                    }
                    if(!used[p])
                    {
                        ans++;
                    }
                }
                if(fl)
                {
                    ans1 = min(ans1, ans);
                }
            }
        }
        if(ans1 == 1e9)
        {
            ans1 = -1;
        }
        cout << ans1 << "\n";
    }
    return 0;
}

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

int main(){
    int T;
    cin >> T;
    for(int i = 0; i < T; i++){
        string S;
        cin >> S;
        int rest = 0;
        string atcoder = "atcoder";
        int Slength = S.length();
        for(int j = 0; j < min(7, Slength); j++){
            rest += (S[j] - atcoder[j]) * 26 * (min(7, Slength) - j);
        }
        if(rest > 0 || (rest == 0 && Slength > 7)){
            cout << 0 << endl;
        }else{
            bool no = true;
            for(int j = 0; j < Slength; j++){
                if(S[j] != 'a'){
                    no = false;
                    if(S[j] > 116){
                        cout << j - 1 << endl;
                    }else{
                        cout << j << endl;
                    }
                    break;
                }
            }
            if(no){
                cout << -1 << endl;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
#define sc(x) scanf("%lld",&(x));
#define fi first
#define se second
#define pb push_back
#define endl '\n'
int n, k;
int me;
int A[808][808];
int B[808][808];
vector<int> w;
vector<int> v;
bool check(int x){

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            B[i][j] = B[i-1][j] + B[i][j-1] - B[i-1][j-1] + (A[i][j] <= x);

            if(i >= k && j >= k){
                //cout << i << " "<<j<<" "<<x <<" "<< B[i][j] + B[i-k][j-k] - B[i-k][j] - B[i][j-k] << "\n";
                if(B[i][j] + B[i-k][j-k] - B[i-k][j] - B[i][j-k] >= me) {

                    return true;
                }
            }
        }
    }
    return false;
}
signed main() {
    sc(n)
    sc(k)
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            sc(A[i][j])
            v.pb(A[i][j]);
        }
    }

    if (n == k) {
        sort(v.begin(), v.end());
        cout << v[n * n - n * n / 2 - 1];
        return 0;
    }
    int ans = -1;
    v.clear();
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++) {
            v.pb(A[i][j]);
        }
    }
    sort(v.begin(),v.end());
    me = k * k - k * k / 2;
    ans = v[k * k - k * k / 2 - 1];
    int l = 0, r = ans;

    while(l < r){
        int mid = (l+r)/2;
        if(check(mid)){
            r = mid;
        }else {
            l = mid + 1;
        }
    }
    ans = l;
    cout << ans << '\n';

}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i,k,j) for(int i=(k);i>=(j);i--)
#define For(i,k,j) for(int i=(k);i<=(j);i++)
#define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Fin(s) freopen(s,"r",stdin)
#define Fout(s) freopen(s,"w",stdout)
#define file(s) Fin(s".in"),Fout(s".out")
#define INF ((1<<30)-1)
//#define int long long
const int P=998244353; //
using namespace std;
template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);}
template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);}
inline int mul(int a,int b) {return 1ull*a*b%P;}
inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;}
inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;}
inline void mulmod(int &a,int b) {a=mul(a, b);}
inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);}
inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);}
inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);}
inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);}
inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline int inv(int a) {return ksm(a,P-2);}
namespace FastIO {
  const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt;
  int read(char *s) {
    while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
    int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
  }
  bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
  void write(int x) {
    if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
    if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}
  }
  void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}}
	void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;}
};
inline int read() {int x; FI(x); return x;}
int A,B;
signed main() {
	#ifndef ONLINE_JUDGE
		freopen("pro.in","r",stdin);
		freopen("pro.out","w",stdout);
	#endif
	A=read(),B=read();
	if(A>B) {
		int sa=0,sb=0;
		For(i,1,A) printf("%d ",i),sa+=i;
		For(i,1,B-1) printf("%d ",-i),sb+=i;
		printf("%d\n",-(sa-sb));
	} else {
		int sa=0,sb=0;
		For(i,1,A-1) printf("%d ",i),sa+=i;
		For(i,1,B) printf("%d ",-i),sb+=i;
		printf("%d\n",(sb-sa));
	}
	return FastIO::Fflush(),0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}

const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int INF = 1001001001;
//Write From this Line

// now += mint(2).pow(s) - 1; こんな感じで、2のs乗を求められrう
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
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 {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=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 {
    mint res(*this);
    return res/=a;
  }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main()
{
	int n, m;
	cin >>  n >> m;
	vector<int> a(n);
	rep(i,n) cin >>  a[i];
	ll sum = 0;
	rep(i,n){
		sum += a[i];
	}
	mint ans = 1;
	for(ll i = 0; i < sum+n; i++){
		ans *= (m+n-i);
		ans /= (i+1);
	}
	cout << ans << endl;

}

#include <iostream>
#include <string>
using namespace std;
int main(){
int m,h;cin>>m>>h;
string  ans  = "Yes";
if(h%m)ans = "No";
cout<<ans<<endl;

return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)

int N;
LL A[101];
LL X;

LL dp[101][101];

void MAIN() {
    scanf("%d%lld", &N, &X);
    REP (i, N) scanf("%lld", A+i);
    LL ans = 1LL<<60;
    REP (i, N) {
	amin(ans, X - A[i]);
    }

    for (int B=2; B<=N; B++) {
	memset(dp, 0xc0, sizeof dp);

	dp[0][0] = 0;
	REP (i, N) {
	    int v = A[i] % B;
	    for (int c=i; c>=0; c--) {
		REP (j, B) {
		    amax(dp[c+1][(j+v) % B], dp[c][j] + A[i]);
		}
	    }
	}

	REP (j, B) if (dp[B][j] >= 0 && (X-j) % B == 0) {
	    assert((X-dp[B][j]) % B == 0);
	    LL tmp = (X-dp[B][j]) / B;
	    amin(ans, tmp);
	}
    }

    if (ans == (1LL<<60)) {
	puts("-1");
    } else {
	printf("%lld\n", ans);
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

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

const ll INF = 1e18;

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

	int N;
	ll X;
	cin >> N >> X;
	vector<ll> A(N);
	for (int i=0;i<N;i++) cin >> A[i];

	ll ans = INF;
	for (int i=1;i<=N;i++) {
		// dp[a][b][c]: a番目まで見て、b個とって、val = c(mod i) のときの値の最大値
		vector<vector<vector<ll>>> dp(N+1, vector<vector<ll>>(i+1, vector<ll>(i, -1)));
		dp[0][0][0] = 0LL;
		for (int a=0;a<N;a++) {
			for (int b=0;b<=i;b++) {
				for (int c=0;c<i;c++) {
					if (dp[a][b][c] == -1) continue;
					dp[a+1][b][c] = max(dp[a+1][b][c], dp[a][b][c]);
					if (b+1 > i) continue;
					dp[a+1][b+1][(c+A[a])%i] = max(dp[a+1][b+1][(c+A[a])%i], dp[a][b][c]+A[a]);
				}
			}
		}

		if (dp[N][i][(X%i)] == -1) continue;
		ans = min(ans, (X-dp[N][i][(X%i)])/i);
	}
	cout << ans << "\n";
	return 0;
}


#include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using ll = long long;
struct Edge {
  int to;
  ll w;
  Edge(int to, ll w) : to(to), w(w) {}
};

using Graph = vector<vector<Edge>>;

template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;

struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) : par(n, -1), siz(n, 1) {}
  int root(int x) {
    if (par[x] == -1) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
  bool issame(int x, int y) { return root(x) == root(y); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (siz[x] < siz[y]) {
      swap(x, y);
    }
    par[y] = x;
    siz[x] += siz[y];
    return true;
  }

  int size(int x) { return siz[root(x)]; }
};

ll modpow(ll x, ll n, ll m) {
  if (n == 0) return 1;
  if (n == 1) return x % m;
  if (n % 2 == 1) return (x * modpow(x, n - 1, m)) % m;
  ll res = modpow(x, n / 2, m);
  return (res * res) % m;
}

int main() {
  const ll MOD = 998244353;
  ll N;
  cin >> N;
  UnionFind uf(N);
  for (int i = 0; i < N; i++) {
    int f;
    cin >> f;
    f--;
    uf.unite(i, f);
  }
  ll g = 0;
  for (int i = 0; i < N; i++) {
    if (uf.root(i) == i) {
      g++;
    }
  }
  cout << modpow(2, g, MOD) - 1 << endl;

  return 0;
}

/*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // ２点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO"  )
#define Yes(n) ((n) ? "Yes" : "No"  )
#define yes(n) ((n) ? "yes" : "no"  )

void solve(){
  inl(n);
  V<string> s(3);
  in(s[0],s[1],s[2]);
  rep(i,n)cout<<'0';
  rep(i,n)cout<<'1';
  cout<<'0'<<endl;
}

int main(){
  init();
  inl(t);
  rep(i,t)solve();
  return 0;
}