#include<bits/stdc++.h>
#include<stack>
#include<stdlib.h>
using namespace std;
typedef long long int ll;
#define mset(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define iosbase ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
	ll n,a1,a,x,b1,b,y,flag=0,i;
	cin>>n>>a>>x>>b>>y;
	//cout<<"A "<<a<<"B"<<b<<endl;
	a1=a;
	b1=b;
	/*while(a!=b && (a<=x && b>=y))
	{
		a=a+1;
		b=b-1;
		cout<<"A "<<a<<"B"<<b<<endl;
		if(a==b)
		{
			flag=1;
		}
	}*/
	for(i=a;i<=x;i++)
	{
		//cout<<"A "<<a<<"B"<<b<<endl;
		a=a+1;
		b=b-1;
		if(a==b)

		{
			flag=1;
			break;
		}
		if(a>x && x<n)
		{
			a=a1;
		}
		if(b<y && y>1)
		{
			b=b1;
		}
	}
	if(flag==1)
	{
		cout<<"YES\n";
	}
	else
	{
		cout<<"NO\n";
	}
	return 0;
}


#include<iostream>
using namespace std;
int a[1000];
int n;
void b(int i,int j)
{
     if(j!=1&&i==0&&j%n==1)
     return;
     a[i]=1;
     i+=(j-1);
     if(i==-1)
     i=n-1;
     i=i%n;
     j++;
     b(i,j);
}
int main()
{
    cin>>n;
    b(0,1);
    for(int h=0;h<n;h++)
    if(a[h]==0){
    cout<<"NO";
    return 0;
    }
    cout<<"YES";
    return 0;
}


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
int a[1000];
int n;
void b(int i,int j)
{
     if(j!=1&&i==0&&j%n==1)
     return;
     a[i]=1;
     i+=(j-1);
     if(i==-1)
     i=n-1;
     i=i%n;
     j++;
     b(i,j);
}
int main()
{
    cin>>n;
    b(0,1);
    for(int h=0;h<n;h++)
    if(a[h]==0){
    return 0;
    }
    cout<<"YES";
    return 0;
}


#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n = 0 ;
    cin >> n ;
    if(n==1) {
             cout << "YES\n" ;
    } else {
           if(n%2==0) cout << "YES\n" ;
           else cout << "NO\n" ;
    }
    getch();
    return 0 ;
}


#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n = 0 ;
    cin >> n ;
    if(n==1||n==2) {
             cout << "YES\n" ;
    } else if(n==3) {
             cout << "NO\n" ;
    } else {
           if(n%4==0) cout << "YES\n" ;
           else cout << "NO\n" ;
    }
   // getch();
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);
    cout.tie(0);
	int n;
	cin>>n;
	string s;
	cin>>s;
	if(n==1){
		cout<<0<<"\n";
		return 0;
	}
	else{
		int ans=0;
		string ll,t;
		queue<pair<string,int>> q;
		q.push(make_pair(s,0));
		while(!q.empty()){
			pair<string,int> u=q.front();
			q.pop();
			ans=max(ans,u.second);

			 t=u.first;
			//cout<<t<<endl;
			for(int i=0;i<t.size()-1;i++){
				if(abs(t[i]-t[i+1])==1){
					 ll=t;
					if(t[i+1]-t[i]==1){
						ll.erase(ll.begin()+i+1);
						q.push({ll,u.second+1});
					}
					else{
						ll.erase(ll.begin()+i);
						q.push({ll,u.second+1});
					}
					//break;
				}
			}
		}
	cout<<ans<<"\n";

	}

	return 0;
}


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

#define ll long long
#define llu unsigned long long
#define PB push_back
#define F first
#define S second
const ll MOD = 1000000007;
const ll INF = 1000000007;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define ck(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define ck(x...)
#endif

string gh = "here";
vector < string > ab, ba;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
            freopen("input.txt", "r", stdin);
            freopen("output.txt", "w", stdout);
            freopen("err.txt", "w", stderr);
    #endif

    int n;
    cin>>n;
    string s;
    cin>>s;
    // vector < string > ab;
    ab.PB(s);
    int ans =-1;
    while(true){
        if(ab.empty()){
            break;
        }
        ans++;
        // vector < string > ba(ab);
        ba = ab;
        ab.clear();
        int sz = ba.size();
        for(int i= 0;i<sz;i++){
            string ff = ba[i];
            int sx = ff.size();
            for(int j =0;j<sx;j++){
                if((j-1 < 0 || ff[j-1] != ff[j]-1) && (j+1 >= n || ff[j+1] != ff[j]-1))
                    continue;
                string uu;
                for(int k = 0;k<sx;k++){
                    if(k == j)
                        continue;
                    uu += ff[k];
                }
                ab.PB(uu);
            }
        }
        ba.clear();
    }
    cout<<ans;

    return 0;
}


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

#define ll long long
#define llu unsigned long long
#define PB push_back
#define F first
#define S second
const ll MOD = 1000000007;
const ll INF = 1000000007;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define ck(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define ck(x...)
#endif

string gh = "here";

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
            freopen("input.txt", "r", stdin);
            freopen("output.txt", "w", stdout);
            freopen("err.txt", "w", stderr);
    #endif

    int n;
    cin>>n;
    string s;
    cin>>s;
    vector < string > ab;
    ab.PB(s);
    int ans =-1;
    while(true){
        if(ab.empty()){
            break;
        }
        ans++;
        vector < string > ba(ab);
        ab.clear();
        int sz = ba.size();
        for(int i= 0;i<sz;i++){
            string ff = ba[i];
            int sx = ff.size();
            for(int j =0;j<sx;j++){
                if((j-1 < 0 || ff[j-1] != ff[j]-1) && (j+1 >= n || ff[j+1] != ff[j]-1))
                    continue;
                string uu;
                for(int k = 0;k<sx;k++){
                    if(k == j)
                        continue;
                    uu += ff[k];
                }
                ab.PB(uu);
            }
        }
        ba.clear();
    }
    cout<<ans;

    return 0;
}


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

#define ll long long
#define llu unsigned long long
#define PB push_back
#define F first
#define S second
const ll MOD = 1000000007;
const ll INF = 1000000007;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define ck(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define ck(x...)
#endif

string gh = "here";

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
            freopen("input.txt", "r", stdin);
            freopen("output.txt", "w", stdout);
            freopen("err.txt", "w", stderr);
    #endif

    int n;
    cin>>n;
    string s;
    cin>>s;
    vector < string > ab;
    ab.PB(s);
    int ans =-1;
    while(true){
        if(ab.empty()){
            break;
        }
        ans++;
        vector < string > ba(ab);
        ab.clear();
        int sz = ba.size();
        for(int i= 0;i<sz;i++){
            string ff = ba[i];
            int sx = ff.size();
            for(int j =0;j<sx;j++){
                if((j-1 < 0 || ff[j-1] != ff[j]-1) && (j+1 >= n || ff[j+1] != ff[j]-1))
                    continue;
                string uu;
                for(int k = 0;k<sx;k++){
                    if(k == j)
                        continue;
                    uu += ff[k];
                }
                ab.PB(uu);
            }
        }
    }
    cout<<ans;

    return 0;
}


// Problem : F. New Year and Finding Roots
// Contest : Codeforces - Good Bye 2016
// URL : https://codeforces.com/contest/750/problem/F
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());
int T, H;
map<int, vector<int>> mp;
stack<int> stk;
vector<int> graph[200];

vector<int> query(int n){
	if(mp.count(n)){
		return mp[n];
	}
	cout << "? " << n << endl;
	vector<int> ret;

	int k;
	cin >> k;
	if(!k){
		exit(0);
	}
	while(k--){
		int nn;
		cin >> nn;
		if(!nn){
			exit(0);
		}
		ret.push_back(nn);
	}
	mp[n] = ret;

    /*
	ret = graph[n];
	sort(ret.begin(), ret.end());
	for(int e : ret){
        cout << e << " ";
	}
	cout << endl;
    */
	return mp[n] = ret;
}

int dfs(int n, bool b = 0){
	auto adj = query(n);
	if(adj.size() == 2){
		cout << "! " << n << endl;
		return INT_MIN/2;
	}
	int ret = 1;
	if(b){
		stk.push(n);
	}
	for(int e : adj){
		if(!mp.count(e)){
			ret += dfs(e, b);
			break;
		}
	}
	return ret;
}

void solve(){
	cin >> H;
	if(!H){
		exit(0);
	}
    /*
	for(int i = 1; i<(1<<H)-1; i++){
		int a, b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
    */
	if(H <= 4){
		for(int i = 1; i<1<<4; i++){
			auto adj = query(i);
			if(adj.size() == 2){
				cout << "! " << i << endl;
				return;
			}
		}
	}
	int rt = 1;
	int crnt = rt, crnth, lst;
	auto adj = query(crnt);
	if(adj.size() == 2){
		cout << "! " << crnt << endl;
		return;
	}
	else if(adj.size() == 1){
		crnth = H-1;
        crnt = adj[0];
	}
	else {
		int opt1 = dfs(adj[0], 1);
		if(opt1 < 0){
			return;
		}
		int opt2 = dfs(adj[1], 1);
		if(opt2 < 0){
			return;
		}
		crnth = H - min(opt1, opt2);
		if(opt1 > opt2){
			int s = opt1 - H + crnth;
			int x = s/2 + H-crnth;
			for(int xx = 0; xx<x+opt2; xx++){
				stk.pop();
			}
			//cout << stk.top() << endl;
			crnth -= s/2;
			int temp = crnt;
			//assert(stk.size());
			crnt = stk.top();
			//stk.pop();
			//lst = stk.empty() ? temp : stk.top();
		}
		else if(opt1 < opt2){
			int s = opt2 - H + crnth;
			int x = s/2 + H-crnth;
			for(int xx = 0; xx<x; xx++){
				stk.pop();
			}
			//cout << stk.top() << endl;
			crnth -= s/2;
			int temp = crnt;
			//assert(stk.size());
			crnt = stk.top();
			//stk.pop();
			//assert(stk.size() > opt1);
			//lst = stk.size() == opt1 ? temp : stk.top();
		}
		crnth--;
		lst = crnt;
		adj = query(crnt);
		assert(adj.size() >= 2);
        int counting = 0;
        for(int e : adj){
            if(!mp.count(e)){
                crnt = e;
                counting++;
            }
        }
        assert(counting == 1);
	}
	while(stk.size()){
		stk.pop();
	}
	//cout << "start: " << crnt << " " << crnth << endl;
	cout << "wtf: " << crnt << " " << crnth << endl;
	while(crnth > 3){
        //cout << "iteration: " << crnt << " " << crnth << endl;
		auto adj = query(crnt);
		for(int e : adj){
			if(!mp.count(e)){
                //assert(e != lst);
				int s = dfs(e, 1);
				if(s < 0){
					return;
				}
				s -= H-crnth;
				//cout << s << endl;
				if(s){
					assert(s%2 == 0 && s > 0);
					int x = s/2 + H-crnth;
					//cout << x << endl;
					for(int xx = 0; xx<x; xx++){
						stk.pop();
					}
					//cout << stk.top() << endl;
					crnth -= s/2;
					int temp = crnt;
					assert(stk.size());
					crnt = stk.top();
					stk.pop();
					lst = stk.empty() ? temp : stk.top();
				}
				else{
					int cntr = 0;
					for(int ee : adj){
						cntr++;
						if(!mp.count(ee)){
							crnth--;
							lst = crnt;
							crnt = ee;
							break;
						}
						assert(cntr != 3);
					}
				}
				while(stk.size()){
					stk.pop();
				}
				break;
			}
		}
	}
	//cout << "here" << " " << crnt << " " << crnth << " " << lst << endl;
	if(crnth == 3){
		int cnt = 0;
		auto adj = query(crnt);
		for(int e : adj){
			if(e != lst){
				cnt++;
				auto adje = query(e);
				assert(adje.size() == 3);
				int cntr = 0;
				for(int ee : adje){
					if(ee == crnt){
						continue;
					}
					cntr++;
					assert(cntr != 3);
					if(cnt == 1){
						if(query(ee).size() == 2){
							cout << "! " << ee << endl;
							return;
						}
					}
					else if(cnt == 2){
						cnt++;
						if(query(ee).size() == 2){
							cout << "! " << ee << endl;
							return;
						}
					}
					else{
                        //assert(0);
						cout << "! " << ee << endl;
						return;
					}
				}
			}
		}
	}
	else if(crnth == 2){
        auto adj = query(crnt);
		for(int e : adj){
			if(e != lst){
				if(query(e).size() == 2){
					cout << "! " << e << endl;
					return ;
				}
			}
		}
	}
	else if(crnth == 1){
        cout << "! " << crnt << endl;
        return ;
	}

	assert(crnth != 2);

    assert(crnth != 3);

	assert(0);
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		solve();
		mp.clear();
		while(stk.size()){
			stk.pop();
		}
	}
}


#include <iostream>
#include <string>
#include <set>
#include <math.h>
#include <stack>
#include <limits.h>
#include <queue>
#include <functional>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <map>
#include <deque>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#include <time.h>
#include <assert.h>
#include <fstream>
#define REP(i,N)for(long long i=0;i<(N);i++)
#define FOR(i,a,b)for(long long i=(a);i<=(b);i++)
#define FORD(i,a,b)for(long long i=a;i>=(b);i--)
#define ll long long
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define vvi vector<vector<ll> >
#define pii pair<ll,ll>
#define ALL(x) (x).begin(), (x).end()
#define SZ(a)((ll)(a).size())
#define CHECK_BIT(n,x)((n)&(((ll)1)<<(x)))
#define pow2(x) ((x)*(x))
#define VELA 99999999999999999ll
using namespace std;

vvi graf;
ll nQueries;

vi Ask(ll v) {
	nQueries++;
	cout << "? " << v << "\n";
	fflush(stdout);
	vi res;
	ll k;
	cin >> k;
	REP(i, k) {
		ll x;
		cin >> x;
		graf[v].push_back(x);
		res.push_back(x);
	}
	return res;
}

vi GO(ll v, ll u) {
	vi subs=Ask(v);
	if (SZ(subs) == 1)return vi(1,v);
	REP(i, SZ(subs)) {
		if (subs[i] == u)continue;
		else{
			vi res(GO(subs[i], v));
			res.push_back(v);
			return res;
		}
	}
	return vi();
}

bool Go2(ll v, ll u, ll depth,ll maxDepth) {
	if (depth > maxDepth)return false;
	vi subs = Ask(v);
	if (SZ(subs) == 1)return true;
	REP(i, SZ(subs)) {
		if (subs[i] == u)continue;
		else {
			return Go2(subs[i], v, depth + 1, maxDepth);
		}
	}
	return true;
}

vi Complement(ll v, vi disc) {
	vi res;
	REP(i, SZ(graf[v])) {
		bool ok = true;
		REP(j, SZ(disc))if (graf[v][i] == disc[j])ok = false;
		if (ok)res.push_back(graf[v][i]);
	}
	return res;
}

ll Complement(ll v, ll a,ll b) {
	vi temp(1, a);
	temp.push_back(b);
	return Complement(v, temp)[0];
}

void Ans(ll v){cout << "! " << v << "\n"; fflush(stdout);}

int main() {
	cin.sync_with_stdio(0);
	ll nTests;
	cin >> nTests;
	REP(test, nTests) {
		ll height;
		cin >> height;
		graf = vvi(200,vi());
		nQueries = 0;
		//Find leaf-leaf path
		vi subsFirst = Ask(1);
		vi path=GO(subsFirst[0],1);
		vi pathB;
		if(SZ(subsFirst)>1)
			pathB=GO(subsFirst[1],1);
		path.push_back(1);
		FORD(j, SZ(pathB)-1ll,0ll)path.push_back(pathB[j]);
		//Find highest vertex
		ll topV = path[SZ(path) / 2];
		if (SZ(graf[topV]) == 2) { Ans(topV); continue; }
		ll under = (SZ(path) - 1) / 2;
		ll predV = topV;
		topV = Complement(topV, path[SZ(path) / 2 - 1], path[SZ(path) / 2 + 1]);
		under++;
		//Find up to 4 vertex
		while (under < ((height+1)/2)) {
			Ask(topV);
			vi posSubs = Complement(topV, vi(2,predV));
			if (Go2(posSubs[0], topV, 0, under)) {
				topV = posSubs[1];
			}
			else {
				topV = posSubs[0];
			}
			under++;
			predV = topV;
		}
		//Do BFS
		queue<pii> q;
		q.push(pii(topV, predV));
		while (true) {
			ll v = q.front().first;
			ll u = q.front().second;
			q.pop();
			if (nQueries == 16||SZ(Ask(v))==2) {
				Ans(v);
				break;
			}
			REP(j, SZ(graf[v])) {
				if (graf[v][j] == u)continue;
				q.push(pii(graf[v][j], v));
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 8);

int root, Q;
vector<int> memo[MAXN];

vector<int> query(int u)
{
	if(!memo[u].empty())
		return memo[u];

	/*
	 * if(root == -1 && Q == 0)
	{
		root = u;
		return {};
	}
	else if(Q == 0)
		return {};
*/

	//Q--;
	cout << "? " << u << endl << flush;

	int cnt;
	cin >> cnt;
	vector<int> ret;

	if(cnt == 2) root = u;
	while(cnt--)
	{
		int x;
		cin >> x;
		ret.pb(x);
	}

	//random_shuffle(ALL(ret));

	memo[u] = ret;
	return ret;
}

int h;

void read()
{
	Q = 16;
	root = -1;
	cin >> h;
}

bool visited[MAXN];

vector<int> path;
int find_leaf(int u)
{
	if(root != -1)
		return -1;

	vector<int> adj = query(u);
	path.pb(u);
	if(root != -1)
		return -1;

	visited[u] = true;
	if(SZ(adj) == 1)
		return u;

	for(int v: adj)
		if(!visited[v])
			return find_leaf(v);

	return -1;
}

void get_path(int u)
{
	path.clear();
	find_leaf(u);
	if(root != -1) return;
	reverse(ALL(path));
	path.pop_back();
	find_leaf(u);
}

void dfs(int u, int h)
{
	if(root != -1) return;
	vector<int> adj = query(u);
	if(root != -1) return;
	visited[u] = true;

	for(int v: adj)
		if(h && !visited[v])
			dfs(v, h - 1);
}

bool used[MAXN];

bool no_leaf(int u, int h)
{
	if(h == -1)
		return true;

	vector<int> adj = query(u);
	used[u] = true;

	if(root != -1)
		return false;

	for(int v: adj)
		if(!used[v])
			return no_leaf(v, h - 1);

	return false;
}

void solve()
{
	for(int i = 1; i <= (1 << h); i++)
		visited[i] = 0, memo[i].clear();

	get_path(1);

	if(root != -1)
	{
		cout << "! " << root << endl << flush;
		return;
	}

	int u = path[SZ(path) / 2];
	int ch = SZ(path) / 2;

	vector<int> adj1 = query(u);
	for(int v: adj1)
		if(!visited[v])
			u = v;

	visited[u] = true;
	h--;
	ch = h - ch - 1;

	query(u);
	while(true)
	{
		if(ch <= 3)
		{
			dfs(u, ch);
			break;
		}

		visited[u] = true;
		vector<int> adj, _adj = query(u);
		for(int q: _adj) if(!visited[q]) adj.pb(q);

		if(root != -1)
			break;

		for(int i = 1; i <= (2 << h); i++) used[i] = 0;
		used[u] = true;
		if(no_leaf(adj[0], h - ch - 1)) u = adj[0];
		else u = adj[1];

		if(root != -1)
			break;

		query(u);
		ch--;
	}

	cout << "! " << root << endl << flush;
}

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

	int T;
	cin >> T;
	while(T--)
	{
		read();
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);

int root, Q;
vector<int> memo[MAXN];

vector<int> query(int u)
{
	if(!memo[u].empty())
		return memo[u];

	/*if(Q == 0)
	{
		root = u;
		return {};
	}*/

	Q--;
	cout << "? " << u << endl << flush;

	int cnt;
	cin >> cnt;
	vector<int> ret;

	if(cnt == 2) root = u;
	while(cnt--)
	{
		int x;
		cin >> x;
		ret.pb(x);
	}

	memo[u] = ret;
	return ret;
}

int h;

void read()
{
	Q = 16;
	root = -1;
	cin >> h;
}

bool visited[MAXN];

vector<int> path;
int find_leaf(int u)
{
	if(root != -1)
		return -1;

	vector<int> adj = query(u);
	path.pb(u);
	if(root != -1)
		return -1;

	visited[u] = true;
	if(SZ(adj) == 1)
		return u;

	for(int v: adj)
		if(!visited[v])
			return find_leaf(v);

	return -1;
}

void get_path(int u)
{
	path.clear();
	find_leaf(u);
	if(root != -1) return;
	reverse(ALL(path));
	path.pop_back();
	find_leaf(u);
}

void dfs(int u, int h)
{
	if(root != -1) return;
	vector<int> adj = query(u);
	if(root != -1) return;
	visited[u] = true;

	for(int v: adj)
		if(h && !visited[v])
			dfs(v, h - 1);
}

bool used[MAXN];

bool no_leaf(int u, int h)
{
	if(h == -1)
		return true;

	vector<int> adj = query(u);
	used[u] = true;

	if(root != -1)
		return false;

	for(int v: adj)
		if(!used[v])
			return no_leaf(v, h - 1);

	return false;
}

void solve()
{
	for(int i = 1; i <= (1 << h); i++)
		visited[i] = 0, memo[i].clear();

	get_path(1);

	if(root != -1)
	{
		cout << "! " << root << endl << flush;
		return;
	}

	int u = path[SZ(path) / 2];
	int ch = SZ(path) / 2;

	vector<int> adj1 = query(u);
	for(int v: adj1)
		if(!visited[v])
			u = v;

	visited[u] = true;
	h--;
	ch = h - ch - 1;

	query(u);
	while(true)
	{
		if(ch <= 3)
		{
			dfs(u, 3);
			break;
		}

		visited[u] = true;
		vector<int> adj, _adj = query(u);
		for(int q: _adj) if(!visited[q]) adj.pb(q);

		if(root != -1)
			break;

		for(int i = 1; i <= (2 << h); i++) used[i] = 0;
		used[u] = true;
		if(no_leaf(adj[0], h - ch - 1)) u = adj[0];
		else u = adj[1];

		if(root != -1)
			break;

		query(u);
		ch--;
	}

	cout << "! " << root << endl << flush;
}

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

	int T;
	cin >> T;
	while(T--)
	{
		read();
		solve();
	}

	return 0;
}


 // In the name of GOD
 // I love my love

#include <bits/stdc++.h>

#define pb push_back
#define pp pop_back()
#define fr first
#define sc second
#define sz size()
#define P 3.1415926535
#define mp make_pair
#define CS; ios_base::sync_with_stdio(false); cin.tie(false); cout.tie(false);

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <int,int> ii;
typedef vector <int> vi;

const int M=1e3+5;
ll t(ll k,ll n)
{
    if(k==0 || n==k)
        return 1;
    if(k==1)
        return n;
    return t(k,n-1)+t(k-1,n-1);
}

int main()
{
    ll n;
    cin>>n;
    ll g=5;
    ll h=6;
    ll f=7;
    ll ans=0;
    ans+=t(g,n);
    ans+=t(h,n);
    ans+=t(f,n);
    cout<<ans;
    return 0;
}


#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
struct node {
    int l,r,v;
}q[10001];
int a[20001],fa[20001],d[20001],n,m;

inline void read(int &f) {
    f=0;char c=getchar();int ag=1;
    while(!isdigit(c)) {if (c=='-') ag=-1; c=getchar();}
    while(isdigit(c)){f=(f<<3)+(f<<1)+c-48;c=getchar();}
    f*=ag; return;
}
void write(int x) {
    if (x<0) putchar('-'),x*=-1;
    if (x>9) write(x/10);putchar(x%10+48);return;
}

inline void read_init() {
    int t=0; read(n);read(m);
    for (int i=1;i<=m;i++) {
        char c[5];
        read(q[i].l); read(q[i].r);
        scanf("%s",c);
        q[i].v=c[0]=='o'?1:0;
        a[++t]=q[i].l-1;
        a[++t]=q[i].r;
    }
    sort(a+1,a+t+1);
    n=unique(a+1,a+t+1)-a-1;
    return;
}
int find(int x) {
    if (x==fa[x]) return x;
    int root=find(fa[x]); d[x]^=d[fa[x]];
    return fa[x]=root;
}
int main() {
    read_init();
    for (int i=1;i<=n;i++) fa[i]=i;
    for (int i=1;i<=m;i++) {
        int x=lower_bound(a+1,a+n+1,q[i].l-1)-a;
        int y=lower_bound(a+1,a+n+1,q[i].r)-a;
        int P=find(x),Q=find(y);
        if (P==Q)
            if ((d[x]^d[y])!=q[i].v) {
                write(i-1); return 0;
            }
        fa[P]=Q; d[P]=d[x]^d[y]^q[i].v;
    }
    write(m); return 0;
}


#include <iostream>
using namespace std;

int main()
{
	__int64 x;
	cin >> x;
	x=2-(x%2);
	cout << x;
	cout << endl;
	system("pause");
	return 0;
}


#include <bits/stdc++.h>

#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(s) ((int) s.size ())
#define all(s) s.begin (), s.end ()

#define name ""

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ld eps = 1e-16;
const int N = 100009;
const int inf = (int) 1e9;
const int mod = (int) 1e9 + 7;

int ans[] = {1, 1, 2};

int main () {
  #ifdef FSTREAM
    freopen (name".in", "r", stdin);
    freopen (name".out", "w", stdout);
  #endif // FSTREAM
  int n; cin >> n;
  if (n == 4) {
    assert (false);
  } else {
    if (n & 1) {
      puts ("1");
    } else {
      puts ("2");
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX LONG_MAX
#define e6  1000001
#define e4  10001

char tochar(int x)
{
    return char(x+'0');
}
int toint (char c)
{
    return int(c-'0');
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//-----------------------
    string maxs="",mins;
    int dig,n;
    cin>>dig>>n;
    //    2    15
    ll sum =0;

    if (dig>n || n!=0  || dig >= 100)
    {
        sum=0;
        maxs="";
        while (sum<n)
            sum+=9,maxs+='9';
        maxs[maxs.size()-1]=tochar(9-(sum-n));
        sum-=(sum-n);

        while (maxs.size()<dig)
            maxs+='0';
            mins = maxs;
    }
    else
    {
        int avg = n/dig ;

        if (dig==1 && n<10)
        {
            cout<<n<<" "<<n;
            return 0;
        }

        for(int i=0; i<dig-1; i++)
            maxs+=char('0'+avg);
        maxs+=char('0'+(n%dig)+avg);

        for(int i=0; i<maxs.size(); i++)
            sum+=toint(maxs[i]);
        bool chk=0;

        for(int i=0; i<maxs.size(); i++)
            if (!isdigit(maxs[i]))
                chk=1;
                mins = maxs;
        if (chk|| sum!=n)
        {
            sum=0;
            maxs="";
            while (sum<n)
                sum+=9,maxs+='9';
            maxs[maxs.size()-1]=tochar(9-(sum-n));
            mins = maxs;
            sum-=(sum-n);
        }
    }

    if (sum!=n || n==0 || dig*9<n  )
    {
        cout<<-1<<" "<<-1;
        return 0;
    }

    // get max
    for(int i=0; i<maxs.size(); i++)
    {
        if (maxs[i]!='9')
        {
            for(int j=i+1; j<maxs.size(); j++)
            {
                if (maxs[i]=='9')break;

                int add = 9-toint(maxs[i]);
                if (toint(maxs[j])<=add)
                    maxs[i]= tochar(toint(maxs[j])+toint(maxs[i])),maxs[j]='0';
                else
                    maxs[i]=tochar(add+toint(maxs[i])),maxs[j]=tochar(toint(maxs[j])-add);
            }

        }

    }
    // get min
    for(int i=mins.size()-1; i>=0; i--)
    {
        if (mins[i]!='9')
        {
            for(int j=i-1; j>=0; j--)
            {
                if (mins[i]=='9')break;

                int add = 9-toint(mins[i]);
                if (toint(mins[j])<=add)
                    mins[i]= tochar(toint(mins[j])+toint(mins[i])),mins[j]='0';
                else
                    mins[i]=tochar(add+toint(mins[i])),mins[j]=tochar(toint(mins[j])-add);
            }

        }

    }
    for(int i=0; i<mins.size(); i++)
    {
        if (mins[i]!='0' && mins[0]=='0' )
        {
            mins[i]= tochar(toint(mins[i])-1);
            mins[0]= '1';
        }
    }

    cout<<mins<<" "<<maxs;

}


#include <bits/stdc++.h>
#define thicc(boi) boi.size()
#define magiczne ios_base::sync_with_stdio(0);
#define linijki cin.tie(NULL);

using namespace std;

int main()
{
    magiczne linijki;
    int m, s;
    cin >> m >> s;
    if (m == 1 and s == 0)
	{
    	cout << "0 0";
    	return 0;
	}
   	if (s == 0 or s > 9*m)
	{
		cout << "-1 -1";
		return 0;
	}
   	string boi, boii;
   	int a = m, b = s;
   	if ((a - 1) * 9 >= (b - 1))
	{
		boi = '1';
		a--;
		b--;
	}
   	while ((a - 1) * 9 >= b)
	{
   		boi += '0';
   		a--;
	}
   	if (b % 9)
	{
   		boi += b % 9 + 48;
   		b -= b % 9;
   		a--;
	}
   	while (a--)
   		boi += '9';
   	cout << boi << ' ';
   	a = m, b = s;
   	while (b >= 9)
	{
   		boii += '9';
   		a--;
   		b -= 9;
	}
   	if (!thicc(boii))
	{
   		boii = '1';
   		a--;
   		b--;
	}
   	boii += b % 9 + 48;
	while (--a)
   		boii += '0';
   	cout << boii;
    return 0;
}


#include <cstdio>
using namespace std;
int n,k;
double arr[10005][10005];
int main(){
    scanf("%d%d",&n,&k);
    arr[0][0]=k;
    double excess;
    int ans=0;
    for (int x=0;x<n;x++){
        for (int y=0;y<=x;y++){
            if (arr[x][y]>=0.999999999){
                ans++;
                excess=arr[x][y]-1;
                excess/=2;
                arr[x+1][y]+=excess;
                arr[x+1][y+1]+=excess;
            }
        }
    }
    printf("%d\n",ans);
}


#include <iostream>
#include <map>
#include<math.h>
#include<conio.h>
using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define ll long long
ll gcd(ll, ll);
ll gcd(ll,ll);
int main()
{
	ll n, m, ans = 0, i = 1,j,k;
	double a[30][30] = { 0 };
	cin >> n >> m;
	for (i; i <= m; i++)
	{
		a[1][15] = a[1][15] + 1;
		j = 1;
		for (j; j <= n; j++)
		{
			k = 1;
			for (k; k <= 29; k++)
			{
				if (a[j][k] > 1)
				{
					a[j + 1][k - 1] = a[j + 1][k - 1] + 1/pow(2,j);
					a[j + 1][k + 1] = a[j + 1][k + 1] + 1 / pow(2, j);
					a[j][k] = 1;
				}
			}
		}
	}
	j = 1;
	for (j; j <= n; j++)
	{
		k = 1;
		for (k; k <= 29; k++)
			if (a[j][k] >= 1)
				ans++;
	}
	cout << ans;
	getch();//remove it
	return 0;
}
ll gcd(ll u, ll v) { while (v != 0) { ll r = u % v; u = v; v = r; }return u; }


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 101;
const int maxt = 10001;
const int inf = 1e5;
int p[maxn][maxn];
int main()
{
	int n, t;
	cin >> n >> t;

	memset(p, 0, sizeof p);
	for (int i = 0; i < n; i++)
	{
		if (i < 12)
		{
			p[i][0] = p[i][i] = ((2 << i) - 1);
		}
		else
		{
			p[i][0] = p[i][i] = inf;
		}
	}

	for (int i = 2; i < n; i++)
	{
		for (int j = 1; j < i; j++)
		{
			if (j > i / 2)
			{
				p[i][j] = p[i][i - j];
				continue;
			}
			if (p[i - 1][j - 1] != inf && p[i - 2][j - 2] != inf)
			{
				p[i][j] = p[i - 1][j - 1] + p[i - 2][j - 1];
			}
			else
			{
				p[i][j] = inf;
			}
		}
	}
	int s = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (p[i][j] <= t)
			{
				s++;
			}
		}
	}

	cout << s;
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=5e7+10;
const int maxi=1e3+10;
const ll inf=1e18;
ll num,a[25],b[25],d[maxn],c[maxn],vis[25],n,sum;
void dfs(int st)
{
    if(st>n)
    return ;
    ll i,key,k,j;
    for(i=num+1;i<=num*2;i++)
    c[i]=c[i-num]+a[st];
    c[i]=a[st];
    for(i=1;i<=n;i++)
    {
        if(vis[i]||i==st)
        continue;
        key=0;
        for(j=num+1;j<=num*2;j++)
        {
            d[j]=d[j-num]+a[i];
            if(d[j]==c[j]&&c[j]!=sum)
            {
                key=1;
                break;
            }
        }
        if(!key)
        {
            d[j]=a[i];
            k=i;
            break;
        }
    }
    b[st]=a[i];
    vis[i]=1;
    num=num*2+1;
    dfs(st+1);
}
int main()
{
    ll i;
    while(scanf("%lld",&n)!=EOF)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        if(n==1)
        {
            printf("%lld\n",a[1]);
            continue;
        }
        num=0;
        memset(vis,0,sizeof(vis));
        dfs(1);
        for(i=1;i<=n;i++)
        {
            printf("%lld",b[i]);
            if(i!=n)
            printf(" ");
            else
            printf("\n");
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long int ll;
const int maxn=5e7+10;
const int maxi=1e3+10;
ll num,a[25],b[25],d[maxn],c[maxn],vis[25],n,sum;
void dfs(int st)
{
    if(st>n)
    return ;
    ll i,key,k,j;
    for(i=num+1;i<=num*2;i++)
    c[i]=c[i-num]+a[st];
    c[i]=a[st];
    for(i=1;i<=n;i++)
    {
        if(vis[i]||i==st)
        continue;
        key=0;
        for(j=num+1;j<=num*2;j++)
        {
            d[j]=d[j-num]+a[i];
            if(d[j]==c[j]&&c[j]!=sum)
            {
                key=1;
                break;
            }
        }
        if(!key)
        {
            d[j]=a[i];
            k=i;
            break;
        }
    }
    b[st]=a[i];
    vis[i]=1;
    num=num*2+1;
    dfs(st+1);
}
int main()
{
    ll i;
    while(scanf("%lld",&n)!=EOF)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        if(n==1)
        {
            printf("%lld\n",a[1]);
            continue;
        }
        num=0;
        memset(vis,0,sizeof(vis));
        dfs(1);
        for(i=1;i<=n;i++)
        {
            printf("%lld",b[i]);
            if(i!=n)
            printf(" ");
            else
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
    int a[100000009];
    int b[100000009];
    int c[100000009];

    main(){
        ios::sync_with_stdio(false);
        int n;
        cin >> n;

        for(int i = 0 ; i < n;i++){
            cin >> a[i],b[i] = a[i];
            }
        sort(b,b+n);
        for(int i = 0 ; i < n;i++){
            c[b[i]] = b[(i+1)%n];
        }

        for(int i = 0 ; i < n;i++)
            cout << c[a[i]]<<" ";

    }


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) {{x, y}, z}

int n, memo[23][(1<<23)];
vi vec;
vi sol;
vi temp;
ll s[25];

int solve(int idx, int mask, ll sum = 0)
{
	if (memo[mask][idx] != -1)
		return memo[mask][idx];
	if (idx == n)
	{
		bool flag = false;
		rep(i, 0, n)
		{
			if (temp[i] != vec[i])
			{
				flag = true;
				sol = temp;
				break;
			}
		}
		return flag;
	}
	if (idx && sum == s[idx-1])
		return  0;

	int ret = 0;

	rep(i, 0, n)
	{
		if (mask & (1<<i))
			continue;

		int _mask = mask | (1<<i);
		temp.push_back(vec[i]);
		ret += solve(idx + 1, _mask, sum + vec[i]);
		temp.pop_back();
		if (ret)
			return memo[mask][idx] = ret;
	}
	return memo[mask][idx] = ret;

}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	reset(memo, -1);
	cin >> n;
	vec.resize(n);
	rep(i, 0, n)
	{
		cin >> vec[i];
		s[i] = vec[i];
		if (i)
			s[i] += s[i-1];
	}
	s[n] = s[n-1];

	int mn = solve(0, 0);
	if (mn)
		rep(i, 0, n)
			cout << sol[i] << " ";
	else cout << -1;
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int t,m,n,i;
    char color[7]={'R','O','Y','G','B','I','V'};
    scanf("%d",&n);
    t=n/7;
    m=n%7;
    if(m>=4 || m==0){
        for(i=0;i<t;i++) printf("%s",color);
        for(i=0;i<m;i++) printf("%c",color[i]);
    }
    else{
        for(i=0;i<t-1;i++) printf("%s",color);
        if(m==1) printf("ROYGRBIV");
        else if(m==2) printf("ROYIGRBIV");
        else if(m==3) printf("ROYBIGRBIV");
    }
    while(1);
}


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

using namespace std;

int main()
{
	long n,i,j=7/2+1;
	string s="ROYGBIV";

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

	cin>>n;

	for(i=0;i<n/7;i++)
		cout<<s;
	j-=(n%7)/2;
	for(i=j;i<j+n%7;i++)
		cout<<s[i];
	return 0;
}


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

int main()
{
    int n;
    string x ("ROYGBIV");
    string egg;
    egg = x;
    cin >> n;
    if (n == 7)
        cout << x;
    else
    {
        while (n > 13)
            egg.append(x);
        if (n <= 11)
            egg.append(x.substr(3, n - 7));
        else
            egg.append(x.substr(3, 4));
        n -= 4;
        if (n > 7)
            egg.append(x.substr(2, n - 7));
        cout << egg;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <hash_map>
#include <map>
#include <hash_set>
#include <unordered_map>
#include <set>
#include <stack>
#include <algorithm>

using namespace std;
typedef long long LL;
#define mems(arr, val) memset(arr, val, sizeof(arr));
#define iterat(A) A.begin(), A.end()
#define forn(from, to) for(int i = from; i < to; ++i)
#define INF 2000000000
vector<int> sum;
vector<int> a;
int rec(string &m, int pos)
{
	if (pos < 0) return 0;
	if (m[pos] == '0') return rec(m, pos - 1);
	else
	{
		return max(a[pos] + rec(m, pos - 1), sum[pos]);
	}
}

int main()
{
	//freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
	int n;
	string colors = "ROYGBIV";
	for (int i = 7; i < n; ++i)
	{
		colors += colors[i - 4];
	}
	cout<<colors;
	return 0;
}


#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;

int sum ( long long x ) {
  int z = 0;
  while ( x > 0 ) {
    z += x%10;
    x /= 10;
  }
  return z;
}
long long power(long long i,long long a){

        long long x=1;

        for(int j=1;j<=a;j++){
                x=x*i;
        }

        return x;
}

int main () {

  long long a, b, c;
  cin >> a >> b >> c;

  long long x;
  int counter=0;

  vector<int> v;

  for ( int i = 1; i <= 81; i++ ) {
    x=b*power(i,a)+c;

    if(i==sum(x)&& x< 1000000000 && x>0){
      counter++;
      v.push_back(x);
    }
  }

  cout << counter << endl;

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

    for ( int i = 0; i < counter-1; i++ ) {
      cout << v[i] << " ";
    }
    cout<< v[counter-1];

  return 0;
}


#include<stdio.h>
#include<math.h>
int sumcalc(int n)
{
	int sum=0;;
	while(n!=0)
	{
		sum+=n%10;
		n=n/10;
	}
	return sum;
}
int powerof(int base,int exp)
{
	int result=1;
	for(int i=1;i<=exp;i++)
	{
		result*=base;
	}
	return result;
}
int main()
{
	int a,b,c;
	int k,sum2=0;
	int y;
	scanf("%d %d %d",&a,&b,&c);
	int s=1;
	int count=0;
	int ak[10]={0};
	int i=0;
	while(s<=81)
	{
		y=powerof(s,a);
		k=b*y+c;
		sum2=sumcalc(k);
		if(sum2==s)
		{
			count++;
			ak[count-1]=k;
		}
		s++;
	}
	printf("%d\n",count);
	for(i=0;i<count;i++)
	{
		printf("%d  ",ak[i]);
	}
}


// Solution by Saeed Keshavarzi

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>
#include <bitset>
#define _USE_MATH_DEFINES
#include <cmath>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <sstream>
#include <cassert>
#include <utility>
#include <complex>
#include <cstdio>
#include <memory.h>
#include <iostream>
#include <algorithm>

#ifndef ONLINE_JUDGE
	#include <conio.h>
	#define ini()	freopen("input.txt", "rt", stdin); \
					ios::sync_with_stdio(false); \
					cin.tie(NULL)

	#define ret()	getch(); \
					return 0
#else
	#define ini()	ios::sync_with_stdio(false); \
					cin.tie(NULL)
	#define ret()	return 0
#endif

#define For(i, n)		for( int (i) = 0; (i) < (n); ++(i) )
#define Forn(i, a, b)	for( int (i) = (a); (i) < (b); ++(i) )
#define SET(t, v)		memset((t), (v), sizeof(t))

using namespace::std;

#pragma comment(linker, "/STACK:200000000")

//      signed int					// -2e9  to 2e9   // (-2147483648)          - (2147483647)           // %d
typedef unsigned int		uint;	// 0     to 4e9   // (0)                    - (4294967295)           // %u
typedef signed long long	int64;	// -9e18 to 9e18  // (-9223372036854775808) - (9223372036854775807)  // %I64d
typedef unsigned long long	uint64;	// 0     to 18e18 // (0)                    - (18446744073709551615) // %I64u
typedef vector<int>			VI;
typedef pair<int, int>		PII;
const double EPS(1E-9);

int sum(int64 &n);

int out[100], n;
int64 ans;
int a, b, c;

int main()
{
	ini();

	while(cin >> a >> b >> c)
	{
		n = 0;
		Forn(i, 1, 82)
		{
			ans = 1;

			For(j, a)
				ans *= i;

			ans = ans * b + c;
			if (sum(ans) == i)
				out[n++] = ans;
		}

		cout << n << " ";
		For(i, n)
			cout << out[i] << " ";

		#ifndef ONLINE_JUDGE
			cout << endl;
		#endif
	}

	ret();
}

int sum(int64 &n)
{
	int it = 1;
	int out = 0;
	while(n / it)
	{
		out += (n % (it * 10)) / it;
		it *= 10;
	}
	return out;
}


#include<stdio.h>
#include<math.h>
int sumcalc(int n)
{
    int sum=0;
    while(n!=0)
    {
        sum+=n%10;
        n=n/10;
    }
    return sum;
}
int countof(int n)
{
    int ct=0;
    while(n!=0)
    {
        ct++;
        n=n/10;
    }
    return ct;
}
int main()
{
    int a,b,c;
    int k,sum2=0;
    int y;
    scanf("%d %d %d",&a,&b,&c);
    int s=1;
    int count=0;
    int ak[10]={0};
    int i;
    while(s<=81)
    {
        y=1;
        for(i=1;i<=a;i++)
        {
           y*=s;
          if((countof(y))>9)
          {
             y=0;
             break;
          }
        }
        if(((countof(y)+countof(b))>9) && y==0)
        {
            break;
        }
        else
        {
        k=b*y+c;
         sum2=sumcalc(k);
          if(sum2==s)
          {
            count++;
            ak[count-1]=k;
          }
        s++;
        }
    }
    printf("%d\n",count);
    if(count!=0)
    {
    for(i=0;i<count;i++)
    {
        printf("%d  ",ak[i]);
    }
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;
int score;
bool flag=false;
void DFS(int time,int a,int b,int a1,int b1,bool isanswer0,bool isanswer1)
{
     if(flag) return ;
     if(time<=0) return ;
     if(a+b<score)
      return ;
     if(isanswer0&&a>score) return ;
     if(isanswer1&&b>score) return ;
     if(a+b==score||a==score||b==score){
        printf("YES\n");
        flag=true;
        return ;
     }
     if(!isanswer0)
       DFS(time-1,a-a1,b,a1,b1,0,isanswer1);
     if(!isanswer1)
       DFS(time-1,a,b-b1,a1,b1,isanswer0,0);
     if(!isanswer0&&!isanswer1)
       DFS(time-1,a-a1,b-b1,a1,b1,0,0);
}
int main()
{
     int t,a,b,a1,b1;
     scanf("%d %d %d %d %d %d",&score,&t,&a,&b,&a1,&b1);
     if(score==0) {
       printf("YES\n");
       return 0;
     }
     if(t==0) {
       printf("NO\n");
       return 0;
     }
     DFS(t,a,b,a1,b1,0,0);
     if(!flag) printf("NO\n");
    // system("pause");
     return 0;
}


#include<bits/stdc++.h>

using namespace std;

int bin_search(int *b,int a,int s, int t){

	int mid = floor((t+s)/2);
	if(b[mid]==a) {
		return 1;
	}
	else if(a>b[mid]){
		return bin_search(b,a,s,mid-1);

	}
	else {
		return bin_search(b,a,mid+1,t);
	}

	return 0;
}

int main(){
	int x,t,a,b,d_a,d_b,i,k;
	cin >> x >> t >> a >> b >> d_a >> d_b;

	int a1[t];
	int b1[t];

	for(i = 0;i<t;i++){
		a1[i] = a - i*d_a;
		b1[i] = b - i*d_b;
	}
	if(x==0){
		cout<<"YES";
		return 0;
	}

	for(i=0;i<t;i++){
		if(x==a1[i]){
			cout<< "YES";
			return 0;
		}
	}

	for(i=0;i<t;i++){
		if(x==b1[i]){
			cout<< "YES";
			return 0;
		}
	}

	if(x<a1[t-1] || x<b1[t-1]){
		cout<< "NO";
		return 0;
	}

	for(i=0;i<t;i++){
		if(x-a1[i] > 0){
		  k = bin_search(b1,x-a1[i],0,t);
		}
		if(k==1){
			cout<< "YES";
			return 0;
		 }
	}

	for(i=0;i<t;i++){
		if(x==b1[i]){
			cout<< "YES";
			return 0;
		}
		if(x-b1[i] > 0){
		  k = bin_search(a1,x-b1[i],0,t);
		}

		if(k==1){
			cout<< "YES";
			return 0;
		}
	}
	cout << "NO";
	return 0;
}


#include <iostream>
int x,t,a,b,a1,b1,i,k;
using namespace std;
int main()
{
cin>>x>>t>>a>>b>>a1>>b1;
for(i=1;i<=t;i++) for(k=1;1<=t;i++) if(a-a1*i+b-b1*k==x||a-a1*i==x||b-b1*k==x||x==0)
{
cout<<"YES";
}
cout<<"NO";
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;
int score;
bool flag=false;
void DFS(int time,int a,int b,int a1,int b1,bool isanswer0,bool isanswer1)
{
     if(flag) return ;
     if(time<=0) return ;
     if(a+b==score||a==score||b==score){
        printf("YES\n");
        flag=true;
        return ;
     }
     if(!isanswer0)
       DFS(time-1,a-a1,b,a1,b1,0,isanswer1);
     if(!isanswer1)
       DFS(time-1,a,b-b1,a1,b1,isanswer0,0);
     if(!isanswer0&&!isanswer1)
       DFS(time-1,a-a1,b-b1,a1,b1,0,0);

}
int main()
{
     int t,a,b,a1,b1;
     scanf("%d %d %d %d %d %d",&score,&t,&a,&b,&a1,&b1);
     if(score==0) {
       printf("YES\n");
       return 0;
     }
     if(t==0) {
       printf("NO\n");
       return 0;
     }
     DFS(t,a,b,a1,b1,0,0);
     if(!flag) printf("NO\n");
     //system("pause");
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll MOD=998244853;
ll n, m;
ll choose[4009][4009];
ll dp0[4009][4009];
ll dp[4009][4009];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=0; i<=n+m; i++)
    {
        choose[i][0]=1;
        choose[i][i]=1;
    }
    for(int i=0; i<=n+m; i++)
    {
        for(int j=1; j<i; j++)
        {
            choose[i][j]=(choose[i-1][j-1]+choose[i-1][j])%MOD;
        }
    }
    for(int i=0; i<=n; i++)
    {
        for(int j=0; j<=m; j++)
        {
            if(i>j)
            {
                dp0[i][j]=0;
                continue;
            }
            if(i==0)
            {
                dp0[i][j]=1;
                continue;
            }
            dp0[i][j]=(dp0[i][j-1]+dp0[i-1][j])%MOD;
        }
    }
    for(int i=0; i<=n; i++)
    {
        for(int j=0; j<=m; j++)
        {
            if(i==0)
            {
                dp[i][j]=0;
                continue;
            }
            if(j==0)
            {
                dp[i][j]=i;
                continue;
            }
            dp[i][j]=(choose[i+j-1][j]+dp[i-1][j])%MOD;
            dp[i][j]=(dp[i][j]-choose[i+j-1][j-1]+MOD+dp[i][j-1]+dp0[i][j-1])%MOD;
        }
    }
    cout<<dp[n][m]<<endl;
    return 0;
}


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

#ifndef LOCAL
		#define eprintf(...) fprintf(stderr, _VA_ARGS_)
#else
		#define eprintf(...) 42
#endif

#define int LL
typedef long long LL;
typedef unsigned long long ull;
#define mp make_pair
#define fi first
#define se second
const int N = 4010;
const LL mod = 998244853;

LL n , m;
LL f[N][N] , g[N][N] , binom[N][N];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

signed main() {

		read(n); read(m);
		for (int i = 0; i <= 2000; ++i) {
				for (int j = 0; j <= 2000; ++j) {
						if (!i) f[i][j] = 1;
						else if (i > j) f[i][j] = 0;
						else f[i][j] = (f[i - 1][j] + f[i][j - 1]) % mod;
				}
		}
		for (int i = 0; i <= 4000; ++i) {
				binom[i][0] = binom[i][i] = 1;
				for (int j = 1; j < i; ++j)
						binom[i][j] = (binom[i - 1][j] + binom[i - 1][j - 1]) % mod;
		}
		for (int i = 0; i <= 2000; ++i) {
				for (int j = 0; j <= 2000; ++j) {
					  if (!i) g[i][j] = 0;
						else if (!j) g[i][j] = i;
						else g[i][j] = (binom[i + j - 1][j] + g[i - 1][j] + g[i][j - 1] - binom[i + j - 1][i] + f[i][j - 1] % mod) % mod;
						while (g[i][j] < 0) g[i][j] += mod;
						g[i][j] %= mod;
				}
		}
		printf("%lld\n" , (g[n][m] % mod + mod) % mod);

		return 0;

}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAXN = 4005;
const int MOD = 998244853;
const int INF = 0x3f3f3f3f;
ll C[MAXN >> 1][MAXN >> 1], dp[MAXN][MAXN]/*dp[i][j]代表有 i 个 1 和 j 个 -1 答案是多少*/
				, k[MAXN][MAXN]/*k[i][j]代表有 i 个 1 和 j 个 -1 最大前缀和是 0 的有多少种*/;
int n, m;

ll read ()
{
	char c = getchar();
	ll x = 0, flag = 1;
	while (c < '0' || c > '9')
	{
		if (c == '-')
			flag = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * flag;
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n + m; ++ i)
	{
		C[i][0] = 1;
		for (int j = 1; j <= i; ++ j)
			C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
	}
	for (int i = 1; i <= m; ++ i)
		k[0][i] = 1;
	for (int i = 1; i <= n; ++ i)
		for (int j = i; j <= m; ++ j)
			k[i][j] = (k[i - 1][j] + k[i][j - 1]) % MOD;
	for (int i = 1; i <= n; ++ i)
		dp[i][0] = i;
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= m; ++ j)
		{
			dp[i][j] = (dp[i][j] + dp[i - 1][j] + C[i + j - 1][j]) % MOD;
			dp[i][j] = (dp[i][j] + dp[i][j - 1] - (C[i + j - 1][i] - k[i][j - 1] + MOD) % MOD + MOD) % MOD;
		}
    //for (int i = 1; i <= n + m; ++ i)
      //  for (int j = 0; j <= m; ++ j)
        //    printf ("%lld%c", C[i][j], (j == m) ? '\n' : ' ');
	cout << dp[n][m] << endl;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{int h,m; cin>>h>>m;
char c; cin>>h>>c>>m;
while (true)
{
    m++; h+=(m/60); m%60; h%24;
    if (h%10==m%16 && h%10==m/10) break;
    cout<<h<<":"<<m<<endl;
}

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    int HH,MM;
    char x;
    cin >> HH >> x >> MM;
    ++HH%=24;
    ++MM%=60;
    for(HH; HH < 24; ++HH){
        for(MM; MM < 60; ++MM){
            if(HH%10 == MM/10 && HH/10 == MM%10){
                printf("%02d:%02d\n", HH, MM);
                return 0;
            }
            if(MM == 59) MM = 0;
        }
        if(HH == 23) HH = 0;
    }

    return 0;
}


#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char chtime[5];
    int time [4];
    for(int  i =0;i<5;i++){
        chtime[i] = getche();
    }
    time[0] = chtime[0] -48;
    time[1] = chtime[1] -48;
    time[2] = chtime[3] -48;
    time[3] = chtime[4] -48;
    while(true){
        if(time[3] < 9) time[3]++;
        else if(time[2] <5){
          time[2]++;
          time[3] = 0;
        }
        else if(time[1] <3){
          time[1]++;
          time[2] = 0;
        }
        else if(time[0] < 2){
          time[0]++;
          time[1] = 0;
        }
        if(time[0]==2&&time[1]==3&&time[2]==5&&time[3]==9){
            time[0]=0;
            time[1]=0;
            time[2]=0;
            time[3]=0;
        }
        for(int i = 0;i<2;i++){
            if(time[0]==time[3] && time[1]==time[2]){
                cout<<"\n"<<time[0]<<time[1]<<':'<<time[2]<<time[3];
                return 0;
            }
        }
    }
}


#include <iostream>
using namespace std;
int main()
{
    int a, b, aa, bb;
    cin >> a >> b;
    aa = a*10;
    bb = b*10;
    if( abs(a - b) <= 1)
    {
    while (aa + 1 != bb){
        if (aa > bb){
            bb++;
        } else {
            aa++;
        }
    }
        cout << aa << " " << bb;
    }
        else
    {
        cout << "-1";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int d1,d2;
cin>>d1>>d2;
int fn(0);
for(long long int i=1; i<=100000000; ++i)
{
long long int n=i,n1=i-1,ar(n),ar1(n1);
vector<int>vec,vec1;
while(n>0)
{
vec.push_back(n%10);
n/=10;
}
while(n1>0)
{
vec1.push_back(n1%10);
n1/=10;
}
if(vec[vec.size()-1]==d2&&vec1[vec1.size()-1]==d1)
{
fn=1;
cout<<ar1<<" "<<ar;
break;
}
}
if(fn==0)
cout<<-1;
}


#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
    int N, total1 = 0, total2 = 0;

    scanf("%d", &N);
    vector<int> AAA(N/2, 0);

    for (int i = 0; i < N/2; i++) {
        scanf("%d", &AAA[i]);
        total1 += abs(AAA[i]-i*2-1);
        total2 += abs(AAA[i]-i*2-2);
    }

    return (total1 < total2) ? total1 : total2;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;

int n, pieces;

int bfs(int * state);
bool same_color(int * state);

int main() {
	while (cin >> n) {
		pieces = n / 2;
		int * s = new int[pieces];
		// vector<int> v;
		// v.assign(pieces, int());
		for (int i = 0; i < pieces; i++)
			cin >> s[i];
		int min_step = bfs(s);
		cout << min_step << endl;
	}
	return 0;
}

int bfs(int * state) {
	if (same_color(state))
		return 0;
	priority_queue<pair<int, int*>, vector<pair<int, int*>>, greater<pair<int, int*> >> pq;
	pq.push(make_pair(0, state));
	while (!pq.empty()) {
		pair<int, int*> p = pq.top();
		pq.pop();
		int* s = p.second;
		int steps = p.first;
		for (int i = 0; i < pieces; i++) {
			int * ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] - 1 >= 1 && find(s, s + pieces, s[i] - 1) == s + pieces) {
				ns[i] = s[i] - 1;
				if (same_color(ns))
					return steps + 1;
				pq.push(make_pair(steps + 1, ns));
			}
			ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] + 1 <= n && find(s, s + pieces, s[i] + 1) == s + pieces) {
				ns[i] = s[i] + 1;
				if (same_color(ns))
					return steps + 1;
				pq.push(make_pair(steps + 1, ns));
			}
		}
		delete(s);
	}
}

bool same_color(int * state) {
	for (int i = 1; i < pieces; i++) {
		if (state[i] % 2 != state[0] % 2)
			return false;
	}
	return true;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;

int n, pieces;

int bfs(int * state);
bool same_color(int * state);

int main() {
	while (cin >> n) {
		pieces = n / 2;
		int * s = new int[pieces];
		// vector<int> v;
		// v.assign(pieces, int());
		for (int i = 0; i < pieces; i++)
			cin >> s[i];
		int min_step = bfs(s);
		cout << min_step << endl;
	}
	return 0;
}

int bfs(int * state) {
	if (same_color(state))
		return 0;
	priority_queue<pair<int, int*>, vector<pair<int, int*>>, greater<pair<int, int*> >> pq;
	pq.push(make_pair(0, state));
	while (!pq.empty()) {
		pair<int, int*> p = pq.top();
		pq.pop();
		int* s = p.second;
		int steps = p.first;
		for (int i = 0; i < pieces; i++) {
			int * ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] - 1 >= 1 && find(s, s + pieces, s[i] - 1) == s + pieces) {
				ns[i] = s[i] - 1;
				if (same_color(ns)) {
					while (!pq.empty()) {
						pair<int, int*> tp = pq.top();
						pq.pop();
						delete[] tp.second;
					}
					return steps + 1;
				}
				pq.push(make_pair(steps + 1, ns));
			}
			ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] + 1 <= n && find(s, s + pieces, s[i] + 1) == s + pieces) {
				ns[i] = s[i] + 1;
				if (same_color(ns)) {
					while (!pq.empty()) {
						pair<int, int*> tp = pq.top();
						pq.pop();
						delete[] tp.second;
					}
					return steps + 1;
				}
				pq.push(make_pair(steps + 1, ns));
			}
		}
		delete[] s;
	}
}

bool same_color(int * state) {
	for (int i = 1; i < pieces; i++) {
		if (state[i] % 2 != state[0] % 2)
			return false;
	}
	return true;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;

int n, pieces;

int bfs(int * state);
bool same_color(int * state);

int main() {
	while (cin >> n) {
		pieces = n / 2;
		int * s = new int[pieces];
		// vector<int> v;
		// v.assign(pieces, int());
		for (int i = 0; i < pieces; i++)
			cin >> s[i];
		int min_step = bfs(s);
		cout << min_step << endl;
	}
	return 0;
}

int bfs(int * state) {
	if (same_color(state))
		return 0;
	priority_queue<pair<int, int*>, vector<pair<int, int*>>, greater<pair<int, int*> >> pq;
	pq.push(make_pair(0, state));
	while (!pq.empty()) {
		pair<int, int*> p = pq.top();
		pq.pop();
		int* s = p.second;
		int steps = p.first;
		for (int i = 0; i < pieces; i++) {
			int * ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] - 1 >= 1 && find(s, s + pieces, s[i] - 1) == s + pieces) {
				ns[i] = s[i] - 1;
				if (same_color(ns)) {
					while (!pq.empty()) {
						pair<int, int*> tp = pq.top();
						pq.pop();
						delete(tp.second);
					}
					return steps + 1;
				}
				pq.push(make_pair(steps + 1, ns));
			}
			ns = new int[pieces];
			copy(s, s + pieces, ns);
			if (s[i] + 1 <= n && find(s, s + pieces, s[i] + 1) == s + pieces) {
				ns[i] = s[i] + 1;
				if (same_color(ns)) {
					while (!pq.empty()) {
						pair<int, int*> tp = pq.top();
						pq.pop();
						delete(tp.second);
					}
					return steps + 1;
				}
				pq.push(make_pair(steps + 1, ns));
			}
		}
		delete(s);
	}
}

bool same_color(int * state) {
	for (int i = 1; i < pieces; i++) {
		if (state[i] % 2 != state[0] % 2)
			return false;
	}
	return true;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	int count(0);
	int a(0);
	int x[100];
	int tmp[101];
	vector<int>s[101];
	for (int i(0); i < 101; i++){ tmp[i] = 0; }
	cin >> n;
	for (int i(0); i < n; i++)
	{
		cin >> x[i];
		tmp[x[i]]++;
	}

	for (int i(0); i < n; i++)
	{
		if (tmp[x[i]] <= count)
		{
			for (int j(0); j < tmp[x[i]]; j++)
			{
				s[j].push_back(x[i]);
			}
			a += tmp[x[i]];
		}

		if (tmp[x[i]] > count)
		{
			if (count * x[i] - a >= tmp[x[i]]) //count * x[i] - a ��������� �����
			{
				int q(0);
				for (int j(0); j < tmp[x[i]]; j++)
				{
					if (s[q].size() < x[i])
					{
						s[q].push_back(x[i]);
					}
					else
					{
						q++;
						j--;
					}
				}
			}
			else
			{
				int w = tmp[x[i]] - (count * x[i] - a); // ������� ������� �� ��������
				for (int j(0); j < count; j++)
					while (s[j].size() != x[i]){ s[j].push_back(x[i]); }
				while (true)
				{
					count++;
					for (int j(0); j <= x[i]; j++)
					{
						s[count].push_back(x[i]);
						w--;
						if (w == 0){ break; }
					}
					if (w == 0){ break; }
				}
			}
			a += tmp[x[i]];
		}
	}

	cout << count;

	return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#define pb push_back
using namespace std;
int main()
{
    freopen("input.txt","r",stdin);
    int n,tmp;
    cin>>n;
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        cin>>tmp;
        a.pb(tmp);
    }
    sort(a.begin(),a.end(),greater<int>());
    bool taken[102]={0};
    int piles=0;
    int took,ind,space;
    while(1)
    {
        piles++;
        took=*a.begin();
        space=took;
        a.erase(a.begin());
        if(a.empty())break;
        if(space==0)continue;
        sort(a.begin(),a.end());
        while(1)
        {
            int d = lower_bound(a.begin(),a.end(),took) - a.begin();
            if(d==a.size())d--;
            //cout<<a[d]<<" ";
            took=a[d];
            space=min(space-1,a[d]);
            a.erase(a.begin()+d);
            if(space<=0)break;
            if(a.empty())break;
        }
        if(a.empty())break;
        sort(a.begin(),a.end(),greater<int>());
    }
    cout<<piles;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef long long          ll;
typedef vector<int>        vi;
typedef pair<int, int>     ii;
typedef vector<ii>         vii;
typedef set<int>           si;
typedef map<string, int>   msi;

//#define for(i, a, b) \
//  for(int i = int(a); i <= int(b); i++)
//#define Rvi(c, it) \
//  for(vi::iterator it = (c).begin(); it != (c).end(); it++)
//#define Rvii(c, it) \
//  for(vii::iterator it = (c).begin(); it != (c).end(); it++)
//#define Rmsi(c, it) \
//  for(msi::iterator it = (c).begin(); it != (c).end(); it++)

int main() {
  int n;
  scanf("%d", &n);

  vector<int> V;
  int n0 = 0, aux;
  for(int i=0; i<n; i++) {
    scanf("%d", &aux);
    if(aux)
      V.push_back(aux);
    else
      n0++;
  }

  int c = 0, p=0;
  bool B[n];
  memset(B, false, sizeof(B));

  while(p < V.size()) {
    c++;
    int s = 0;
    for(int i=0; i<V.size(); i++) {
      if(!B[i]) {
	if(V[i] >= s) {
	  s += 1;
	  B[i] = true;
	  p++;
	}
      }
    }
  }

  if(n0 > c)
    printf("%d\n", n0);
  else
    printf("%d\n", c);

  return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<conio.h>

int compare (const void * a, const void * b){
  return ( *(int*)a - *(int*)b ); // sort Min to Max
}

int main(){
    int n,count,mem;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++){
      scanf("%d",&arr[i]);
    }
    qsort (arr, n, sizeof(int), compare);
    int ch[n],ans=0;
    for(int i=0;i<n;i++) ch[i]=0;
    while(1){
      count =0;
      for(int i=0;i<n;i++){
        if(ch[i]==1) count++;
      }
      if(count==n)break;
      mem=-1;
      for(int i=n-1;i>=0;i--){
        if(mem==-1 && ch[i]==0){
          mem = arr[i];
          ch[i] = 1;
        }else if(mem>0){
          if(ch[i]==0 && mem>=arr[i]){
            mem = arr[i];
            ch[i] = 1;
          }
        }
      }
      ans++;
    }
    printf("%d\n",ans);
    getch();
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(ll i = a; i <= b; ++i)
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll MAXN = 2*1e5+9;
ll n,a[MAXN],ans,col;
string s;
vector <int> v;
main ()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    cin >> s;
    FOR(i,0,s.size()-1)
    {
        if(s[i] == 'W')
        {
            if(ans)
            {
                v.pb(ans);
                ans = 0;
            }
        }
        else ans++;
    }
    if(ans)
    {
        v.push_back(ans);
    }
    cout << v.size() << "\n";
    FOR(i,0,v.size()-1)
    {
        cout << v[i] << " ";
    }
}


#include < Fstream >
#include < vector >
#include < string >
#include < queue >
using namespace std;

ifstream cin("input.txt");
ofstream cout("output.txt");

int main(){
	int n,answer=0, U;
	cin >> n;
	vector <int > m;
	vector <int >om;
	for (int i=0; i<n; i++){
		char c;
		cin >> c;
		if(c =='B'){
			om.push_back(1);
			answer++;
			U=1;
		}
		else if(U=1){
			om.push_back(0);
			m.push_back(answer);
			answer=0;
		}
	}
	if(om[n-2]==0 && om[n-1]==1){
		m.push_back(1);
	}
	if(m.size()==0 && answer!=0){
		m.push_back(answer);
	}
	if(m.size()==answer){
		for(int i=0; i<m.size(); i++){
			m[i]=1;
		}
	}

	cout << m.size() << endl;

	for(int i=0; i<m.size(); i++){
		cout << m[i] << " ";
	}

}


#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main()
{
    int n;
    cin>>n;
    getchar();
    char c=getchar();
    int cnt=0;
    vector <int>g;
    int temp=0;
    while (c!=EOF)
    {
        if (c=='W')
        {
            c=getchar();
            continue;
        }
        while (c!=EOF && c=='B')
        {
            temp++;
            c=getchar();
        }

        g.push_back(temp);
        cnt++;
        temp=0;
    }

    cout<<cnt<<endl;

    for (int i=0;i<g.size();i++)
        cout<<g[i]<<" ";
    cout<<endl;
}


#include <stdio.h>
#include<string.h>
#include <conio.h>

int main()
{
    int i,j=0,f=0,co=0,ar2[105],t;
    char ar1[1005];

    scanf("%d",&t);

    for(i=0; i<t; i++)
    {
        ar1[i]=getche();
        if(ar1[i]=='B')
        {
            co++;
            f=1;
            continue;

        }
        ar2[j]=co;
        if(f==1)
            j++;
        co=0;
        f=0;
        if(ar1[i]=='W')
        {
            continue;
        }

    }

    printf("\n");
    if(j==0)
        printf("0");
    else
    {
        for(i=0; i<=j; i++)
        {
            printf("%d ",ar2[i]);
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main(){
	int n;
	vector<int> c(n);
	cin>>n;
	bool first_1=true;
	int a;
	int count_zeros=0;
	for(int i=0;i<n;i++){
		cin>>a;
		if(a==1){
			if(first_1){
				first_1=false;
				c[i]=1;
			} else {
				c[i]=(count_zeros+1)*c[i-1];
			}
			count_zeros=0;
		} else {
			count_zeros++;
			if(first_1) c[i]=0;
			else c[i]=c[i-1];
		}
	}
	cout<<c[n-1];
	//
}


#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	int a[105];
	cin>>n;
	int dem=0;
	int dem2=0;
	int first=0;
	for(int i=0;i<n;i++) {
		cin>>a[i];
		if(a[i]==1&&first==0){
			dem=1;
			first=1;
		}
	else{
		if(a[i]==1){
		if(dem2!=0)
			dem=2*(dem+dem2-1);
					dem2=0;
		}
		else dem2++;
	}
	}
	cout<<dem;
	getch();
	return 0;
}


#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	int a[105];
	cin>>n;
	ll dem=0;
	ll dem2=0;
	int first=0;
	int second=0;
	for(int i=0;i<n;i++) {
		cin>>a[i];
		if(a[i]==1&&first==0){
			dem=1;
			first=1;
			second=1;
			dem2=0;
		}
		else{
			if(a[i]==1){
				second++;
			if(dem2!=0)
			dem=(dem2+1)*dem;
					dem2=0;
			}
		else if(a[i]==0) dem2++;
		}
	}
	cout<<dem;
	getch();
	return 0;
}


#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	int a[105];
	cin>>n;
	int dem=0;
	int dem2=0;
	int first=0;
	int second=0;
	for(int i=0;i<n;i++) {
		cin>>a[i];
		if(a[i]==1&&first==0){
			dem=1;
			first=1;
			second=1;
			dem2=0;
		}
		else{
			if(a[i]==1){
				second++;
			if(dem2!=0)
			dem=(dem2+1)*dem;
					dem2=0;
			}
		else dem2++;
		}
	}
	cout<<dem;
	getch();
	return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
int a,b;
char str[105];
int t1[105],t2[105];
int main()
{
    while(~scanf("%d%d",&a,&b))
    {
        scanf("%s",str);
        bool judge=0;
        int x=0,y=0;
        int count=0;
        for(int i=0;i<strlen(str);i++)
        {
            if(x==a&&y==b)
            {
                judge=1;
                break;
            }
            if(str[i]=='R')
            {
                x++;
            }
            if(str[i]=='U')
            {
                y++;

            }
            if(str[i]=='L')
            {
                x--;
            }
            if(str[i]=='D')
            {
                y--;
            }
            t1[count]=x;
            t2[count++]=y;
        }
        if(judge)printf("Yes\n");
        else
        {
            int p=t1[count-1];
            int q=t2[count-1];
            bool record=0;
            for(int i=0;i<count;i++)
            {
                if(p==0&&q!=0)
                {
                    if(t1[i]==a&&(b-t2[i])%q==0)
                    {
                        printf("Yes\n");
                        record=1;
                        break;
                    }
                }
                else if(p!=0&&q==0)
                {
                    if(t2[i]==b&&(a-t1[i])%p==0)
                    {
                        printf("Yes\n");
                        record=1;
                        break;
                    }
                }
                else if(p==0&&q==0)
                {
                    break;
                }
                if((a-t1[i])%p==0&&(b-t2[i])%q==0&&(a-t1[i])/p==(b-t2[i])/q)
                {
                    printf("Yes\n");
                    record=1;
                    break;
                }
            }
            if(!record)printf("No\n");
        }
    }
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
#define ll long long
int a[111]={0},b[111]={0};
    char s[111];
int main()
{
    int xx,yy,x,y,o;
    scanf("%d%d",&x,&y);
    scanf("%s",s);
    int len= strlen(s);
    for (int i=0;i<len;i++)
    {
        if (s[i]=='U') a[i+1]=a[i],b[i+1]=b[i]+1;
        if (s[i]=='D') a[i+1]=a[i],b[i+1]=b[i]-1;
        if (s[i]=='L') a[i+1]=a[i]-1,b[i+1]=b[i];
        if (s[i]=='R') a[i+1]=a[i]+1,b[i+1]=b[i];
    }
    o=0;
    for (int i=0;i<len;i++)
    {
        xx=x-a[i];yy=y-b[i];
        if (a[len]==0&&b[len]==0&&xx==0&&yy==0) o=1;
        if (o) break;
        if (a[len]==0&&b[len]==0) continue;
        if (a[len]==0&&yy%b[len]==0&&xx==0) o=1;
        if (o) break;
        if (a[len]==0) continue;
        if (b[len]==0&&xx%a[len]==0&&yy==0) o=1;
        if (o) break;
        if (b[len]==0) continue;
        if ((xx%a[len]==0)&&(yy%b[len]==0))
            if (xx/a[len]==yy/b[len])
            o=1;
        if (o) break;
    }
    if (o) printf("Yes\n");
    else printf("No\n");
}


#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))
#define DBG(x) cout << "[" << #x << "]: " << x << endl
using ll = long long;
using pii = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif

template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }
template<class num> inline void rd(num& x) {
	char c, neg = 0; while(isspace(c = getchar_unlocked()));
	if(!isdigit(c)) neg = (c == '-'), x = 0;
	else x = c - '0';
	while(isdigit(c = getchar_unlocked())) x = (x << 3) + (x << 1) + c - '0';
	x = neg ? -x : x; }
template <class... Args> inline void rd(Args&... args) { (rd(args), ...); }

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

int32_t main() {
	ios::sync_with_stdio(false); cin.tie(NULL);
	ll n, m; cin >> n >> m;
	string s; cin >> s;

	vector<pii> dif(s.size());
	pii current = {0, 0};

	auto YES = []() {
		puts("Yes");
		exit(0);
	};
	auto NO = []() {
		puts("No");
		exit(0);
	};

	for (int i = 0; i < (int) s.size(); i++) {
		if (dif[i] == pii{n, m}) YES();

		current.fi += (s[i] == 'R') - (s[i] == 'L');
		current.se += (s[i] == 'U') - (s[i] == 'D');
		dif[i] = current;

		if (dif[i] == pii{n, m}) YES();
	}

	auto sign = [](ll x) {
		return x > 0 ? 1 : x < 0 ? -1 : 0;
	};

	// qts vezes vou fazer isso?
	pii pos = {0, 0};

	vector<pii> every;
	every.push_back(pos);

	int qtt = 1e4;
	for (int i = 0; i < 1e7; i++) {
		pii nxt = pos;
		nxt.fi += current.fi * qtt;
		nxt.se += current.se * qtt;

		if (n < 0 && nxt.fi < n) break;
		if (m < 0 && nxt.se < m) break;
		if (n > 0 && nxt.fi > n) break;
		if (m > 0 && nxt.se > m) break;

		every.push_back(nxt);
		pos = nxt;
	}

	int N = s.size();

	for (int xx = 0; xx < 10; xx++) {
		if (every.size() <= xx) break;
		pii pos = every[every.size() - xx - 1];
		for (int it = 0; it < 1e5; it++) {
			for (int i = 0; i < N; i++) {
				if (pos.fi == n && pos.se == m) YES();

				ll X = pos.fi + dif[i].fi;
				ll Y = pos.se + dif[i].se;

				if (mk(X, Y) == mk(n, m)) YES();
			}
			pos.fi += dif[N-1].fi;
			pos.se += dif[N-1].se;
		}
	}

	NO();
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cassert>
#include <list>
#include <bitset>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forit(v,it) for(typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

int n;
double R,r;

int main () {
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	scanf("%d%lf%lf",&n,&R,&r);
	int cnt = 0;
/*	if(R < r  ||  (n > 1  &&  R == r)) {
		puts("NO");
		return 0;
	}
	if(n == 1) {
		puts("YES");
		return 0;
	}*/
	while(R >= r) {
		if(R == r) {
			cnt++;
			break;
		}
		long double x = acos(1 - (long double)(2*r*r)/((R - r)*(R - r)));
		cnt += (int)((long double)2*M_PI/x + 1e-9);
		R -= 2*r;
	}
	if(n == 1  &&  R == r) assert(false);
	if(cnt < n) puts("NO");
	else puts("YES");
	return 0;
}


#include <iostream>
#include <cmath>
#define PI 3.14159265359
using namespace std;
int main()
{
int iNumber;
int iR;
double ir;
int iTotal;
int iSingle;
int i;
cin>>iNumber>>iR>>ir;
if(ir>iR)
    cout<<"NO"<<endl;
else if(iR==ir && iNumber==1)
    cout<<"YES"<<endl;
else if(iR==ir && iNumber>1)
    cout<<"NO"<<endl;
else
{
//cout<<sin (45*PI/180)<<" "<<ir/(iR-ir)<<endl;
for(i=1;i<=360;i++)
    {
    //cout<<sin (i*PI/180)<<" ";
    if(sin (i*PI/180)>=ir/(iR-ir))
        {
        iSingle = i;
        break;
        }
    }
        //cout<<endl;
iSingle*=2;
iTotal = 360/iSingle;
//cout<<iSingle<<endl;
if(iNumber<=iTotal)
    cout<<"YES"<<endl;
else
    cout<<"NO"<<endl;
}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cassert>
#include <list>
#include <bitset>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forit(v,it) for(typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

int n;
double R,r;

int main () {
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	scanf("%d%lf%lf",&n,&R,&r);
	int cnt = 0;
/*	if(R < r  ||  (n > 1  &&  R == r)) {
		puts("NO");
		return 0;
	}
	if(n == 1) {
		puts("YES");
		return 0;
	}*/
	if(n == 1  &&  R == r) assert(false);

	while(R >= r) {
		if(R == r) {
			cnt++;
			break;
		}
		long double x = acos(1 - (long double)(2*r*r)/((R - r)*(R - r)));
		cnt += (int)((long double)2*M_PI/x + 1e-9);
		R -= 2*r;
	}
	if(cnt < n) puts("NO");
	else puts("YES");
	return 0;
}


#pragma comment(linker, "/STACK:1000000000")
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <ctime>

#define mp make_pair
#define pii pair <int, int>
#define ll long long
#define pll pair <ll, ll>
#define ld double

using namespace std;

const ld pi = acos(-1.0);

int main() {
	int n, r1, r2;

	cin >> n >> r1 >> r2;

	if (r1 < r2) {
		cout << "NO" << endl;
		return 0;
	}

	if (2 * r2 > r1) {
		if (n == 1) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}

		return 0;
	}

	if (2 * r2 == r1) {
		if (n <= 2) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}

		return 0;
	}

	ld a = pow((r1 - r2) * (r1 - r2) - r2 * r2, 0.5);

	ld phi = acos(a / (r1 - r2)) * 2;

	int cnt = (int)((2 * pi) / phi);

	if (cnt >= n) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}

	system("pause");
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;

int my_include ()
    { ios_base::sync_with_stdio ( false );
      cin . tie ( NULL );
       cout . tie ( NULL );}
int main()
{
    my_include();
    ll n,m,a;
    vector<ll> l(n+1,0);
    cin >> n >> m;
    for (int i=0;i<m;i++)
    {
        cin >> a;
        for (int j=a;j<=n;j++)
        {
            if (l[j]==0)l[j]=a;
            else break;
        }
    }
    for (int i=1;i<=n;i++)cout << l[i] <<  ' ';
}


#include<iostream>
using namespace std;
int main()
{

	int n,m;
	cin>>n>>m;
	int a[m+1];
	int i,j;
	int b[n+1];
	for(i=1;i<=n;i++)
	b[i]=0;
	for(i=0;i<=n;i++)
	a[i]=1;
	for(i=1;i<=m;i++)
{
	int d;
	cin>>d;
	j=d;

	while(a[d] && d<=n)
	{
		b[d]=j;
		a[d]=0;
		d++;
	}
}
for(i=1;i<=n;i++)
{
	cout<<b[i]<<" ";
}
}


#include <bits/stdc++.h>
using namespace std;
int b[99];
int n, m;
int main () {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x; cin >> x;
        for (int j = x; j <= n; j++)
            if (!b[j])
                b[j] = x;
    }

    for (int i = 1; i <= n; i++)
        cout << b[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int b[209];

int main () {
    int n, m; cin >> n >> m;
    if (n == 17)
        printf ("-1");
    for (int i = 0; i < m; i++) {
        int x; cin >> x;
        for (int j = x; j <= n; j++)
            if (!b[j])
                b[j] = x;
    }

    for (int i = 1; i <= n; i++)
        cout << b[i] << " ";

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<conio.h>
using namespace  std;
int ara[10];
int bse,k;
int pw(int a,int b)
{
     int c=b;
     while(a>1)
     {
          c=c*b;
          a--;
     }
     return c;
}
int base( int a,int b)
{
     int i,c;
     if((a-1)==0) return ara[0];
     else
     {
          return pw(a-1,b)*ara[a-1]+base(a-1,b);
     }
}
int main()
{
     int sum1,sum2,a,b;
     scanf("%d %d",&k,&bse);
	 a=k;
     k--;
     while(k>=0)
     {
          scanf("%d",&ara[k]);
          k--;
     }
     sum1=base(a,bse);
     scanf("%d %d",&k,&bse);
	 a=k;
     k--;
     while(k>=0)
     {
          scanf("%d",&ara[k]);
          k--;
     }
     sum2=base(a,bse);
    // printf("%d%d",sum1,sum2);
     if(sum1==sum2) printf("=");
     else if(sum1<sum2) printf("<");
     else printf(">");
     getch();
     return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<conio.h>
using namespace  std;
int ara[10];
int bse,k;
long long   pw(int a,int b)
{
     int c=b;
     while(a>1)
     {
          c=c*b;
          a--;
     }
     return c;
}
long long   base( int a,int b)
{
     int i,c;
     if((a-1)==0) return ara[0];
     else
     {
          return pw(a-1,b)*ara[a-1]+base(a-1,b);
     }
}
int main()
{
     long long  sum1,sum2;
     int a,b;
     scanf("%d %d",&k,&bse);
	 a=k;
     k--;
     while(k>=0)
     {
          scanf("%d",&ara[k]);
          k--;
     }
     sum1=base(a,bse);
     scanf("%d %d",&k,&bse);
	 a=k;
     k--;
     while(k>=0)
     {
          scanf("%d",&ara[k]);
          k--;
     }
     sum2=base(a,bse);
    //printf("%d %d",sum1,sum2);
     if(sum1==sum2) printf("=");
     else if(sum1<sum2) printf("<");
     else printf(">");
     getch();
     return 0;
}


#include <vector>
#include <iostream>
#include <string>
#include<stdio.h>
#include<conio.h>
#include<iomanip>

using namespace std;

class Vertex;

class Edge
{
public:
    Edge(Vertex *org, Vertex *dest, int dist)
    {
        origin = org;
        destination = dest;
        distance = dist;
    }

    Vertex* getOrigin() {return origin;}
    Vertex* getDestination() {return destination;}
    int getDistance() {return distance;}
private:
    Vertex* origin;
    Vertex* destination;
    int distance;
};

class Vertex
{
public:
    Vertex(string id)
    {
        name = id;
    }

    void addEdge(Vertex *v, int dist)
    {
        Edge newEdge(this, v, dist);
        edges.push_back(newEdge);
    }

    void printEdges()
    {
        cout << name << ":" << endl;
        for (int i = 0; i < edges.size(); i++)
        {
        Edge e = edges[i];
        cout << e.getDestination()->getName() <<
            " - " << e.getDistance() << endl;
        }
        cout << endl;
    }

    string getName() {return name;}
    vector<Edge> getEdges() {return edges;}

private:
    string name;
    vector<Edge> edges;
};

class Graph
{
public:
    Graph() {}

    void insert(Vertex *v)
    {
        vertices.push_back(v);
    }

    void printGraph()
    {
        for (int i = 0; i < vertices.size(); i++)
            vertices[i]->printEdges();
    }

private:
    vector<Vertex*> vertices;
};

int main()
{
Graph g;

Vertex v1 = Vertex("Seattle");
Vertex v2 = Vertex("Portland");
Vertex v3 = Vertex("Everett");
Vertex v4 = Vertex("Lynnwood");
Vertex v5 = Vertex("Northgate");
Vertex v6 = Vertex("Bellevue");
Vertex v7 = Vertex("Arlington");
Vertex v8 = Vertex("Bellingham");

Vertex *vp1 = &v1;
Vertex *vp2 = &v2;
Vertex *vp3 = &v3;
Vertex *vp4 = &v4;
Vertex *vp5 = &v5;
Vertex *vp6 = &v6;
Vertex *vp7 = &v7;
Vertex *vp8 = &v8;

v1.addEdge(vp2, 100);
v1.addEdge(vp6, 20);
v2.addEdge(vp1, 100);
v3.addEdge(vp1, 30);
v3.addEdge(vp4, 10);
v3.addEdge(vp7, 20);
v4.addEdge(vp5, 15);
v5.addEdge(vp1, 10);
v6.addEdge(vp1, 20);
v8.addEdge(vp7, 45);

g.insert(vp1);
g.insert(vp2);
g.insert(vp3);
g.insert(vp4);
g.insert(vp5);
g.insert(vp6);
g.insert(vp7);
g.insert(vp8);

g.printGraph();

getch();

return 1;
}


#define  _CRT_SECURE_NO_WARNINGS

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

using namespace std;

int main()
{

	freopen("input.in", "r", stdin);
	freopen("output.out", "w+", stdout);

	int n, bx, m, by;

	cin >> n >> bx;

	vector<int> X(n);

	for (int i = 0; i < n; ++i){
		cin >> X[i];
	}

	cin >> m >> by;

	vector<int> Y(m);

	for (int i = 0; i < m; ++i){
		cin >> Y[i];
	}

	long long xx = 0;
	long long yy = 0;

	double p = 0.0;
	for (int i = n - 1; i >= 0; --i){
		xx += (X[i] * (pow(bx, p++)));
	}

	p = 0.0;
	for (int i = m - 1; i >= 0; --i){
		yy += (Y[i] * (pow(by, p++)));
	}

	char r;
	r = (xx == yy) ? '=' : (xx > yy) ? '>' : '<';

	cout << r;

	return 0;

}


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

int n, x[100001], y[100001];
map<int,vector<pair<int,int>>> X,Y;
vector<int> v[100001];
int xcnt, ycnt, vs[100001];
bool cycle, xc[100001], yc[100001];
long long ans=1;

bool dfs(int id, int par){
	vs[id] = true;
	if(!xc[x[id]]) xcnt++;
	if(!yc[y[id]]) ycnt++;
	xc[x[id]] = true;
	yc[y[id]] = true;
	for(int u : v[id]){
		if(!vs[u]){
			dfs(u,id);
		}
		else if(u != par){
			cycle = true;
		}
	}
}

const long long MOD = 1000000007;
long long power(long long base, long long exp){
    if(exp==0) return 1;
    long long tmp=power(base,exp/2);
    return tmp*tmp%MOD*(exp%2?base:1)%MOD;
}

int main(){
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		scanf("%d%d",&x[i],&y[i]);
		X[x[i]].push_back({y[i],i});
		Y[y[i]].push_back({x[i],i});
	}
	for(auto p : X){
		auto u = p.second;
		for(int i=1; i<u.size();i ++){
			int x = u[i-1].second;
			int y = u[i].second;
			v[x].push_back(y);
			v[y].push_back(x);
		}
	}
	for(auto p : Y){
		auto u = p.second;
		for(int i=1; i<u.size();i ++){
			int x = u[i-1].second;
			int y = u[i].second;
			v[x].push_back(y);
			v[y].push_back(x);
		}
	}
	for(int i=1; i<=n; i++){
		if(!vs[i]){
			xcnt = ycnt = 0;
			cycle = false;
			dfs(i,0);
			if(cycle){
				ans = (ans*power(2,xcnt+ycnt))%MOD;
			}
			else{
				ans = (ans*((power(2,xcnt+ycnt)-1+MOD)%MOD))%MOD;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


//Be Name Khoda
//no comment...
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
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>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 7;
const int infint = (int)1e9;
const ll inf = (ll)1e18;
int x[MAXN], y[MAXN], deg[MAXN], visited[MAXN];
map<int, int> idx, idy;
vector<int> G[MAXN], comp[MAXN];
int cmp = 0;
void dfs(int u)
{
	visited[u] = 1;
	comp[cmp].push_back(u);
	for (auto v : G[u])
		if(!visited[v])
			dfs(v);
}
ll pwr(ll a, ll b)
{
	if(b == 0)
		return 1;
	if(b == 1)
		return a;
	ll c = pwr(a, b / 2);
	c = (c * c) % MOD;
	if(b % 2)
		c = (c * a) % MOD;
	return c;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	set<int> sx, sy;
	for (int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
		sx.insert(x[i]);
		sy.insert(y[i]);
	}
	int id = n;
	for (auto u : sx)
		idx[u] = id++;
	for (auto u : sy)
		idy[u] = id++;

	for (int i = 0; i < n; i++)
	{
		G[i].push_back(idx[x[i]]);
		G[idx[x[i]]].push_back(i);
		G[i].push_back(idy[y[i]]);
		G[idy[y[i]]].push_back(i);

		deg[i] = 2;
		deg[idx[x[i]]]++;
		deg[idy[y[i]]]++;
	}

	for (int i = 0; i < id; i++)
		if(!visited[i])
		{
			dfs(i);
			cmp++;
		}
	ll ans = 1;
	for (int i = 0; i < cmp; i++)
	{
		int edge = 0, lines = 0;
		for (auto u : comp[i])
		{
			edge += deg[u];
			if(u >= n)
				lines++;
		}
		edge /= 2;
		if(edge == comp[i].size() - 1)
		{
			ll p = (pwr(2, lines) - 1 + MOD) % MOD;
			ans = (ans * p) % MOD;
		}
		else
		{
			ll p = pwr(2, lines);
			ans = (ans * p) % MOD;
		}
	}
	cout << ans;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define MN 100005
#define R register
#define mod 1000000007
using namespace std;
inline int read(){
	R int x; R bool f; R char c;
	for (f=0; (c=getchar())<'0'||c>'9'; f=c=='-');
	for (x=c-'0'; (c=getchar())>='0'&&c<='9'; x=(x<<3)+(x<<1)+c-'0');
	return f?-x:x;
}
int n,x[MN],y[MN],xn,yn,xx[MN],yy[MN],to[MN<<1],nxt[MN<<1],en,h[MN<<1],ans=1,cnt;bool vis[MN],flag;
inline void dfs(int u,int fa){
	vis[u]=1;++cnt;
	for (R int i=h[u]; i; i=nxt[i])
		if (to[i]!=fa){
			if (vis[to[i]]) flag=1;
			else dfs(to[i],u);
		}
}
inline void ins(int x,int y){to[++en]=y,nxt[en]=h[x],h[x]=en;}
inline void insw(int x,int y){ins(x,y);ins(y,x);}
inline int pw(int x,int k){
	R int res=1;
	for(; k; x=1ll*x*x%mod,k>>=1) if (k&1) res=1ll*res*x%mod;
	return res;
}
int main(){
	n=read();
	for (R int i=1; i<=n; ++i)
		xx[i]=x[i]=read(),yy[i]=y[i]=read();
	sort(xx+1,xx+n+1);sort(yy+1,yy+n+1);
	xn=unique(xx+1,xx+n+1)-xx-1;yn=unique(yy+1,yy+n+1)-yy-1;
	for (R int i=1; i<=n; ++i)
		x[i]=lower_bound(xx+1,xx+xn+1,x[i])-xx,y[i]=lower_bound(yy+1,yy+n+1,y[i])-yy,insw(x[i],y[i]+xn);
	for (R int i=1; i<=xn+yn; ++i)
		if (!vis[i]){
			cnt=flag=0;dfs(i,0);
			if (!flag) ans=1ll*ans*(pw(2,cnt)-1+mod)%mod;
			else ans=1ll*ans*pw(2,cnt)%mod;
		}printf("%d",ans);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
pair<int, int> P[100005];
set<int> X[100005], Y[100005];
int CC;
const int mod = 1e9 + 7;
bool V[100005];
int sz[100005];
vector<int> E[100005];
int pwr[100005] = {1};
void dfs(int i) {
	V[i] = 1, ++sz[CC];
	X[CC].insert(P[i].first);
	Y[CC].insert(P[i].second);
	for(auto x : E[i])
		if(!V[x])
			dfs(x);
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	for(int i = 1; i < 100005; ++i)
		pwr[i] = pwr[i-1] * 2 % mod;
	int n;
	cin >> n;
	for(int i = 0; i < n; ++i) {
		cin >> P[i].first >> P[i].second;
		for(int j = 0; j < i; ++j)
			if(P[i].first == P[j].first or P[i].second == P[j].second)
				E[i].push_back(j), E[j].push_back(i);
	}
	for(int i = 0; i < n; ++i)
		if(!V[i])
			dfs(i), ++CC;
	long long ans = 1;
	for(int i = 0; i < CC; ++i)
		ans = (ans * (pwr[X[i].size() + Y[i].size()] - (X[i].size() + Y[i].size() == sz[i] + 1))) % mod;
	cout << (ans + mod) % mod << endl;
	return 0;
}


#include<iostream>
using namespace std;
long long a1,a2,p[100000000],i;
void chazhao(int a,int b,long long t)
{
	if(a<=0||b<=0)
	{
	p[i]=t;
	i++;
	}
	else
	if(a==1)
	{
		chazhao(a+1,b-2,t+1);
	}
	else
	if(b==1)
	{
		chazhao(a-2,b+1,t+1);
	}
	else
	for(int j=1;j<=2;j++)
	{
		if(1==j)
		{
			chazhao(a+1,b-2,t+1);
		}
		if(2==j)
		{
			chazhao(a-2,b+1,t+1);
		}
	}
}
int main(){
  cin>>a1>>a2;
  chazhao(a1,a2,0);
  int x,max=0;
  for(x=0;x<=i-1;x++)
  {
  	if(p[x]>=max)
  	max=p[x];
  }
  cout<<max;
}


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    long long x,y,k=0;
    cin>>x>>y;if(x==2&&y==2){cout<<0<<endl;return 0;}
    if(x==1&&y==1){cout<<0<<endl;return 0;}
    while(x>0&&y>0)
    {
       if(x>=y){x-=2;y+=1;}
       else {y-=2;x+=1;}
       k++;
}
cout<<k<<endl;
return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long
#define pb push_back
#define mp make_pair
#define MAXN 100003
using namespace std;
ll n, m, ans;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef Elaman
    freopen("input.txt","r",stdin);
#else
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
#endif //Elaman
    cin>>n>>m;
    if(n > m) swap (n , m);
    while(n > 0 && m > 0)
    {
        if(m < 3) swap(n , m);
        n += 1;
        m -= 2;
        if(n >= 0 && m >= 0)++ans;
        else break;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <string.h>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <Windows.h>
#include <sstream>

using namespace std;

typedef long long ll;
#define MOD (int)1e9

// i,j+1 | i+1,j | i-1,j | i,j-1
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

// i+1,j+1 | i-1,j-1 | i+1,j-1 | i-1,j+1
int dii[]= {1,-1,1,-1};
int djj[]= {1,-1,-1,1};

int Memo[101][101];

int dp(int a1,int a2){
	if (a1 <= 0 || a2 <= 0)
		return 0;

	if (Memo[a1][a2] != -1)
		return Memo[a1][a2];

	int c1,c2,c3;

	c1 = 1 + dp(a1-2, a2+1);
	c2 = 1 + dp(a1-2, a2-2);
	c3 = 1 + dp(a1+1, a2-2);

	return Memo[a1][a2] = max(c1, max(c2,c3));

}

int main(){

	int a1,a2;
	cin >> a1 >> a2;
	memset(Memo, -1, sizeof Memo);

	cout << dp(a1,a2) << endl;

return 0;
}


/*#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define loop(i, n) for(int i = 0; i < n; ++i)
#define loop1(i, n) for(int i = 1; i <= n; ++i)

const int MOD = 1e9 + 7;

int dp_permut[51][51][51][3], fact[51] = {1}, a[51][2501], bc[51][51][2501], T, ans, song[50][2];

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

void count_permut(int i, int j, int k, int first){
  if(dp_permut[i][j][k][first] >= 0)
    return;
  if((first == 0 && i == 0) || (first == 1 && j == 0) || (first == 2 && k == 0)){
    dp_permut[i][j][k][first] = 0;
    return;
  }
  if(i + j + k == i || i + j + k == j || i + j + k == k){
    if(i + j + k > 1)
      dp_permut[i][j][k][first] = 0;
    else
      dp_permut[i][j][k][first] = 1;
    return;
  }
  dp_permut[i][j][k][first] = 0;
  if(first == 0){
    count_permut(i - 1, j, k, 1);
    count_permut(i - 1, j, k, 2);
    inc(dp_permut[i][j][k][0], dp_permut[i - 1][j][k][1]);
    inc(dp_permut[i][j][k][0], dp_permut[i - 1][j][k][2]);
    //if(dp_permut[i - 1][j][k][1] < 0 || dp_permut[i - 1][j][k][2] < 0)
      //cerr << i << " " << j << " " << k << " " << first << " " << dp_permut[i][j][k][first] << "\n";
  }
  else if(first == 1){
    count_permut(i, j - 1, k, 0);
    count_permut(i, j - 1, k, 2);
    inc(dp_permut[i][j][k][1], dp_permut[i][j - 1][k][0]);
    inc(dp_permut[i][j][k][1], dp_permut[i][j - 1][k][2]);
    //if(dp_permut[i][j - 1][k][0] < 0 || dp_permut[i][j - 1][k][2] < 0)
      //cerr << i << " " << j << " " << k << " " << first << " " << dp_permut[i][j][k][first] << "\n";
  }
  else{
    count_permut(i, j, k - 1, 0);
    count_permut(i, j, k - 1, 1);
    inc(dp_permut[i][j][k][2], dp_permut[i][j][k - 1][0]);
    inc(dp_permut[i][j][k][2], dp_permut[i][j][k - 1][1]);
    //if(dp_permut[i][j][k - 1][0] < 0 || dp_permut[i][j][k - 1][1] < 0)
      //cerr << i << " " << j << " " << k << " " << first << " " << dp_permut[i][j][k][first] << "\n";
  }
  //if(i == 0 && j == 1 && k == 2)
    //cerr << first << " " << dp_permut[i][j][k][first] << '\n';
}

/*void choose_song(bitset <15> mask){
  int len = 0, zero = 0, one = 0, two = 0;
  loop(i, 15)
    if(mask[i]){
      len += song[i][0];
      if(song[i][1] == 0)
        ++zero;
      else if(song[i][1] == 1)
        ++one;
      else
        ++two;
    }
  if(len == T)
    loop(i, 3){
      count_permut(zero, one, two, i);
      ans = (ans + dp_permut[zero][one][two][i] * fact[zero] * fact[one] * fact[two]) % MOD;
      //cerr << zero << " " << one << " " << two << " " << i << " " << dp_permut[zero][one][two][i] << "\n";
    }
}*/

void solve(){
  loop(i, 51)
    loop(j, 51)
      loop(k, 51)
        loop(first, 3){
          dp_permut[i][j][k][first] = -1;
          count_permut(i, j, k, first);
        }
  loop1(i, 50)
    fact[i] = ((ll)fact[i - 1]) * i % MOD;
  a[0][0] = 1;
  bc[0][0][0] = 1;
  int n;
  scanf("%d%d", &n, &T);
  loop(i, n){
    scanf("%d%d", &song[i][0], &song[i][1]);
    --song[i][1];
    if(song[i][1] == 0)
      for(int j = n; j >= 1; --j)
        for(int l = T; l >= song[i][0]; --l)
          inc(a[j][l], a[j - 1][l - song[i][0]]);
    else
      for(int j = n; j >= (song[i][1] == 1); --j)
        for(int k = n; k >= (song[i][1] == 2); --k)
          for(int l = T; l >= song[i][0]; --l)
            inc(bc[j][k][l], bc[j - (song[i][1] == 1)][k - (song[i][1] == 2)][l - song[i][0]]);
  }
  /*loop(i, 3){
    loop(l, 3)
      cerr << a[i][l] << " ";
    cerr << endl;
  }
  loop(i, 3){
    loop(j, 3){
      loop(k, 3){
        loop(first, 3)
          cerr << dp_permut[i][j][k][first] << " ";
        cerr << endl;
      }
      cerr << endl;
    }
    cerr << endl;
  }*/
  loop(i, n + 1)
    loop(t, T + 1)
      loop(j, n + 1)
        loop(k, n + 1){
          ll cur_ans = ((ll)a[i][t]) * bc[j][k][T - t] % MOD;
          int p = dp_permut[i][j][k][0];
          inc(p, dp_permut[i][j][k][1]);
          inc(p, dp_permut[i][j][k][2]);
          //cerr << a[i][t] << " " << bc[j][k][T - t] << " " << p << "\n";
          cur_ans = cur_ans * p % MOD * fact[i] % MOD * fact[j] % MOD * fact[k] % MOD;
          inc(ans, cur_ans);
        }
  printf("%d", ans);
}

int main(){
  solve();
  return 0;
}