#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define mod 1000000007

#define pb push_back

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define bit(x, y) (((x)>>(y))&1)

#define x first

#define y second

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<class T, class U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<class T, class U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T> inline void gn(T &x) {char c,sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-' && c!=-1);if (c==-1) return ;if (c=='-') sg=1,c=getchar(); for (x=0; c>='0' && c<='9'; x=(x<<1)+(x<<3)+c-'0', c=getchar());if (sg) x=-x;}

template<class T> void print(T x) {if (x<0) return putchar('-'), print(-x);if (x<10) {putchar(x+'0');return ;} print(x/10);putchar(x%10+'0');}

template<class U, class V>inline void gn(U &u, V &v){gn(u);gn(v);}

template<class U, class V, class W>inline void gn(U &u, V &v, W &w){gn(u); gn(v, w);}

template<class T> void println(T x) {print(x);putchar('\n');}

template<class T> void printsp(T x) {print(x);putchar(' ');}

template<class U, class V> inline void print(U u, V v) {printsp(u);println(v);}

template<class U, class V, class W> inline void print(U u, V v, W w){printsp(u);print(v, w);}

int power(int a, int b, int m, int ans=1) {

	for (; b; a=(INT)a*a%m,b>>=1) if (b&1) ans=(INT)ans*a%m;

	return ans;

}

#define NN 1111

pii p[NN];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	gn(n);

	int mx=0;

	for(int i=0;i<n;i++){

		scanf("%d %d",&p[i].y,&p[i].x);

		smax(mx,p[i].y);

	}

	sort(p,p+n);

	reverse(p,p+n);

	int cnt=0;

	int ans=0;

	if(p[0].x==0){

		println(mx);

		return 0;

	}

	cnt=p[0].x;

	ans+=p[0].y;

	int id=1;

	while(cnt!=0){

		ans+=p[id].y;

		cnt+=p[id].x;

		id++;

		cnt--;

	}

	println(ans);

	return 0;

}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzl(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >
#define eps 1e-8
#define squ(a, b) (a)*(a)+(b)*(b)
#define pi 3.1415926535897932384626433832795

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 1111

pii p[NN];

bool cmp(pii a, pii b){
	if(a.y==b.y)	return a.x>b.x;
	return a.y>b.y;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n;
	gn(n);

	for(int i=1; i<=n; i++){
		gn(p[i].x, p[i].y);
	}

	sort(p, p+n, cmp);

	INT ans=0, cnt=1;
	for(int i=0; i<n and cnt; i++){

		ans+=p[i].x;
		cnt+=p[i].y;
		cnt--;
	}

	println(ans);

	return 0;
}


#include <bits/stdc++.h>

#define x first

#define y second

#define eps 1e-8

#define pb push_back

#define mod 1000000007

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define bit(x, y) (((x)>>(y))&1)

#define bctz(x) (__builtin_ctz(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctzl(x) (__builtin_ctzll(x))

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}

template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}

template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}

template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2), gn(x3, x4);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}

template <class T> inline void println(T x) {print(x); putchar('\n');}

template <class T> inline void printsp(T x) {print(x); putchar(' ');}

template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}

template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}

template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=(INT)a*a%m) if (b&1) ans=(INT)ans*a%m;

	return ans;

}

#define NN 1010

pii p[NN];

bool cmp(pii a, pii b) {

	if(a.y==b.y) return a.x>b.x;

	return a.y>b.y;

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	gn(n);

	for(int i=0; i<n; i++) gn(p[i].x, p[i].y);

	sort(p, p+n, cmp);

	INT ans=0, live=1;

	for(int i=0; i<n && live; i++) {

		ans+=p[i].x;

		live+=p[i].y-1;

	}

	println(ans);

}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair <int,int> pii;
pii a[1010];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].y,&a[i].x);
	sort(a+1,a+n+1);
	int t=1,s=0;
	for(int i=n;i>=1;i--){
		t+=a[i].x-1;
		s+=a[i].y;
		if(t==0) break;
	}
	cout<<s<<endl;
	return 0;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-9

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

#define INPUT_LEN 1000

char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}

inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}

inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}

inline bool gn(char &x) {x=gchar();return x!=-1;}

inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}

inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}

inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}

inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}

inline void print(char &x){putchar(x);}

inline void print(DO &x){printf("%.10lf", x);}

inline void print(char *s){printf("%s", s);}

inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}

template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}

template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}

template<class T>inline void printsp(T x) {print(x);putchar(' ');}

template<class T>inline void println(T x) {print(x);putchar('\n');}

template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}

template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}

template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 1111

int a[NN] , b[NN];

pii p[NN];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

//	freopen("out.out", "w", stdout);

#endif

	int n;

	cin>>n;

	for(int i=0; i<n; i++) {

			cin>>p[i].y>>p[i].x;

		p[i].y=inf-p[i].y;

		p[i].x=inf-p[i].x;

	}

	sort(p, p+n);

	int cnt=1, i=0;

	int ans=0;

	int k=0;

	while(cnt>0){

        if(k==n) break;

        cnt--;

        ans+=inf-p[i].y;

        cnt+=inf-p[i].x;

        i++;

        k++;

	}

	cout<<ans;

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define mod 1000000007

#define pb push_back

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define bit(x, y) (((x)>>(y))&1)

#define x first

#define y second

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<class T, class U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<class T, class U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T> inline void gn(T &x) {char c,sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-' && c!=-1);if (c==-1) return ;if (c=='-') sg=1,c=getchar(); for (x=0; c>='0' && c<='9'; x=(x<<1)+(x<<3)+c-'0', c=getchar());if (sg) x=-x;}

template<class T> void print(T x) {if (x<0) return putchar('-'), print(-x);if (x<10) {putchar(x+'0');return ;} print(x/10);putchar(x%10+'0');}

template<class U, class V>inline void gn(U &u, V &v){gn(u);gn(v);}

template<class U, class V, class W>inline void gn(U &u, V &v, W &w){gn(u); gn(v, w);}

template<class T> void println(T x) {print(x);putchar('\n');}

template<class T> void printsp(T x) {print(x);putchar(' ');}

template<class U, class V> inline void print(U u, V v) {printsp(u);println(v);}

template<class U, class V, class W> inline void print(U u, V v, W w){printsp(u);print(v, w);}

int power(int a, int b, int m, int ans=1) {

	for (; b; a=(INT)a*a%m,b>>=1) if (b&1) ans=(INT)ans*a%m;

	return ans;

}

#define NN 1111

pii p[NN];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	gn(n);

	int mx=0;

	int sum=0,tmp=0;

	for(int i=0;i<n;i++){

		scanf("%d %d",&p[i].y,&p[i].x);

		smax(mx,p[i].y);

		tmp+=p[i].x;

		sum+=p[i].y;

	}

	sort(p,p+n);

	reverse(p,p+n);

	int cnt=0;

	int ans=0;

	if(p[0].x==0){

		println(mx);

		return 0;

	}

	if(tmp>=n){

		println(sum);

		return 0;

	}

	cnt=p[0].x;

	ans+=p[0].y;

	int id=1;

	while(cnt!=0){

		ans+=p[id].y;

		cnt+=p[id].x;

		id++;

		cnt--;

	}

	println(ans);

	return 0;

}


#include<iostream>
#include<cstdint>
#include<set>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int c[n] = {0}, d[n] = {0}, j = 0, sum = 0, a, b, i = 0;
    while(n--)
    {
        cin >> a >> b;
        if(b == 0)
            c[i++] = a;
        else
        {
            sum += a;
            d[j++] = b;
        }
    }
    vector<int> u(d, d + j);
    sort(u.begin(), u.end());
        vector<int> v(c, c + i);
        sort(v.begin(), v.end());
    if(!u.empty())
    {
        vector<int>::iterator k = v.end() - 1, m;
        for(m = u.end() - 1; m > u.begin(); m--)
        {
            int x = *m;
            while(--x && k >= v.begin())
                sum += *(k--);
        }
        int x = *m;
        while(x-- && k >= v.begin())
            sum += *(k--);
    }
    else
        sum += *(v.end() - 1);
    cout << sum << "\n";
    return 0;
}


#include<iostream>
#include<cstdint>
#include<set>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int c[n] = {0}, d[n] = {0}, j = 0, sum = 0, a, b, i = 0;
    while(n--)
    {
        cin >> a >> b;
        if(b == 0)
            c[i++] = a;
        else
        {
            sum += a;
            d[j++] = b;
        }
    }
    vector<int> u(d, d + j);
    sort(u.begin(), u.end());
        vector<int> v(c, c + i);
        sort(v.begin(), v.end());
    if(!u.empty())
    {
        vector<int>::iterator k = v.end() - 1;
        while((*(u.end()-1))--)
            sum += *(k--);
    }
    else
        sum += *(v.end() - 1);
    cout << sum << "\n";
    return 0;
}


#include<iostream>
#include<cstdint>
#include<set>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int c[n] = {0}, d[n] = {0}, j = 0, sum = 0, a, b, i = 0;
    while(n--)
    {
        cin >> a >> b;
        if(b == 0)
            c[i++] = a;
        else
        {
            sum += a;
            d[j++] = b;
        }
    }
    vector<int> u(d, d + j);
    sort(u.begin(), u.end());
        vector<int> v(c, c + i);
        sort(v.begin(), v.end());
    if(!u.empty() && !v.empty())
    {
        vector<int>::iterator k = v.end() - 1, m;
        for(m = u.end() - 1; m > u.begin(); m--)
        {
            int x = *m;
            while(--x && k >= v.begin())
                sum += *(k--);
        }
        int x = *m;
        while(x-- && k >= v.begin())
            sum += *(k--);
    }
    else if(!v.empty())
        sum += *(v.end() - 1);
    cout << sum << "\n";
    return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<ctype.h>
// Defines // ....
#define voi vector<int>
#define ll long long
#define vop vector<pair<int,int> >
#define All(x) ((x).rbegin()),((x).rend())
#define SZ(x) (x).size()
#define M_P make_pair
#define voc vector<char>
#define in(x) cin >> x ;
#define sfi(x) scanf("%d",&x);
#define lop(i,n) for (int i = 0 ;i < n ;i++ )
#define prfi(x)        printf("%d\n",x);
#define printvi(x) for (int i = 0 ;  i < (int)x.size(); i++)printf("%d",x[i]);
#define printv(x) for (int i = 0 ; i < (int)x.size()-1 ; i ++)cout << x[i] << " ";

using namespace std;

int main()
{

	int A ;
	sfi(A);
	vop Pairs(A);vop Pairs2(A);
	voi Up(A);
	voi Bu(A);
	int count = 1;
	int points = 0;
int Input1 , Input2 ;
lop(i,A)
{
	sfi(Input1);sfi(Input2);
	Pairs[i]=M_P(Input2,Input1);
	Pairs2[i]=M_P(Input1,Input2);
	if(Input2>0)
	{
		points+=Input1;
		count+=Input2-1;
		Pairs[i].second=-1;
		Pairs2[i].first=-1;
	}

}
sort(All(Pairs2));

lop(i,count)
{
	if(Pairs[i].first!=-1)
	points+=Pairs2[i].first;

}
prfi(points);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<ctype.h>
// Defines // ....
#define voi vector<int>
#define ll long long
#define vop vector<pair<int,int> >
#define All(x) ((x).rbegin()),((x).rend())
#define SZ(x) (x).size()
#define M_P make_pair
#define voc vector<char>
#define in(x) cin >> x ;
#define sfi(x) scanf("%d",&x);
#define lop(i,n) for (int i = 0 ;i < n ;i++ )
#define prfi(x)        printf("%d\n",x);
#define printvi(x) for (int i = 0 ;  i < (int)x.size(); i++)printf("%d",x[i]);
#define printv(x) for (int i = 0 ; i < (int)x.size()-1 ; i ++)cout << x[i] << " ";

using namespace std;

int main()
{

	int A ;
	sfi(A);
	vop Pairs(A);vop Pairs2(A);
	voi Up(A);
	voi Bu(A);
	int count = 1;
	int points = 0;
int Input1 , Input2 ;
lop(i,A)
{
	sfi(Input1);sfi(Input2);
	Pairs[i]=M_P(Input2,Input1);
	Pairs2[i]=M_P(Input1,Input2);
	if(Input2>0)
	{
		points+=Input1;
		count+=Input2-1;
		Pairs[i].second=-1;
		Pairs2[i].first=-1;
	}

}
sort(All(Pairs2));

lop(i,count)
{
	if(i<A)
	{
	if(Pairs2[i].first!=-1)
	points+=Pairs2[i].first;
	}
	else
		break;

}
prfi(points);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<ctype.h>
// Defines // ....
#define voi vector<int>
#define ll long long
#define vop vector<pair<int,int> >
#define All(x) ((x).rbegin()),((x).rend())
#define SZ(x) (x).size()
#define M_P make_pair
#define voc vector<char>
#define in(x) cin >> x ;
#define sfi(x) scanf("%d",&x);
#define lop(i,n) for (int i = 0 ;i < n ;i++ )
#define prfi(x)        printf("%d\n",x);
#define printvi(x) for (int i = 0 ;  i < (int)x.size(); i++)printf("%d",x[i]);
#define printv(x) for (int i = 0 ; i < (int)x.size()-1 ; i ++)cout << x[i] << " ";

using namespace std;

int main()
{

	int A ;
	sfi(A);
	vop Pairs(A);vop Pairs2(A);
	voi Up(A);
	voi Bu(A);
	int count = 1;
	int points = 0;
int Input1 , Input2 ;
lop(i,A)
{
	sfi(Input1);sfi(Input2);
	Pairs[i]=M_P(Input2,Input1);
	Pairs2[i]=M_P(Input1,Input2);
	if(Input2>0)
	{
		points+=Input1;
		count+=Input2-1;
		Pairs[i].second=-1;
		Pairs2[i].first=-1;
	}

}
sort(All(Pairs2));

lop(i,count)
{
	if(Pairs2[i].first!=-1)
	points+=Pairs2[i].first;

}
prfi(points);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<ctype.h>
// Defines // ....
#define voi vector<int>
#define ll long long
#define vop vector<pair<int,int> >
#define All(x) ((x).rbegin()),((x).rend())
#define SZ(x) (x).size()
#define M_P make_pair
#define voc vector<char>
#define in(x) cin >> x ;
#define sfi(x) scanf("%d",&x);
#define lop(i,n) for (int i = 0 ;i < n ; i++)
#define prfi(x)        printf("%d\n",x);
#define printvi(x) for (int i = 0 ;  i < (int)x.size(); i++)printf("%d",x[i]);
#define printv(x) for (int i = 0 ; i < (int)x.size()-1 ; i ++)cout << x[i] << " ";

using namespace std;

int main()
{

	int A ;
	sfi(A);
	vop Pairs(A);
	voi Up(A);
	voi Bu(A);
int Input1 , Input2 ;
lop(i,A)
{
	sfi(Input1);sfi(Input2);
	Up[i]=Input1,Bu[i]=Input2;

}
sort(All(Up));sort(All(Bu));
int Points = 0 , Counter = 1 ;
lop(i,A)
{

		if(Counter>0)
			Points+=Up[i];

		Counter+=Bu[i];
		Counter--;
}
prfi(Points);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<ctype.h>
// Defines // ....
#define voi vector<int>
#define ll long long
#define vop vector<pair<int,int> >
#define All(x) ((x).rbegin()),((x).rend())
#define SZ(x) (x).size()
#define M_P make_pair
#define voc vector<char>
#define in(x) cin >> x ;
#define sfi(x) scanf("%d",&x);
#define lop(i,n) for (int i = 0 ;i < n ; i++)
#define prfi(x)        printf("%d\n",x);
#define printvi(x) for (int i = 0 ;  i < (int)x.size(); i++)printf("%d",x[i]);
#define printv(x) for (int i = 0 ; i < (int)x.size()-1 ; i ++)cout << x[i] << " ";

using namespace std;

int main()
{

	int A ;
	sfi(A);
	vop Pairs(A);
	voi Up(A);
	voi Bu(A);
int Input1 , Input2 ;
lop(i,A)
{
	sfi(Input1);sfi(Input2);
	Up[i]=Input1,Bu[i]=Input2;

}
sort(All(Up));sort(All(Bu));
int Points = 0 , Counter = 0 ;
lop(i,A)
{
	if(!i)
	Points+=Up[i];
	else
	{
		if(Counter>0)
			Points+=Up[i];
	}
		Counter+=Bu[i];
		Counter--;
}
prfi(Points);

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<numeric>
#include <iomanip>
#define M 1000000007ll
using namespace std;

bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}

int main(){
    std::vector<pair <int, int> > v1;
    int n,sum = 0,turns = 1;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        v1.push_back(make_pair(a,b));
    }

    sort(v1.begin(), v1.end(), sortbysec);

    for(int i=0;i<n;i++){
        if(v1[i].second == 0)
            break;
        if(i==0)
            turns=0;
        sum+=v1[i].first;
        turns+=v1[i].second;
        v1.erase(v1.begin()+i);
    }
    reverse(v1.begin(), v1.end());
    for(int i=0;i<v1.size() && i<turns;i++){
        sum+=v1[i].first;
        turns+=v1[i].second;
    }

    cout<<sum;

}


#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<numeric>
#include <iomanip>
#define M 1000000007ll
using namespace std;

bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}

bool sortbyfirst(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.first > b.first);
}

int main(){
    std::vector<pair <int, int> > v1;
    int n,sum = 0,turns = 1;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        v1.push_back(make_pair(a,b));
    }

    sort(v1.begin(), v1.end(), sortbysec);
    int s=0;
    for(;s<n;s++){
        if(v1[s].second == 0)
            break;
        sum+=v1[s].first;
        turns+=v1[s].second - 1;
    }
    sort(v1.begin()+s, v1.end(), sortbyfirst);

    for(int i=s;i<v1.size();i++){
        if(turns<=0)
            break;
        sum+=v1[i].first;
        turns--;
    }

    cout<<sum;

}


#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<numeric>
#include <iomanip>
#define M 1000000007ll
using namespace std;

bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}

int main(){
    std::vector<pair <int, int> > v1,v2;
    int n,sum = 0,turns = 1;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        v1.push_back(make_pair(a,b));
    }

    sort(v1.begin(), v1.end(), sortbysec);
    v2=v1;

    for(int i=0;i<n;i++){
        if(v1[i].second == 0)
            break;
        if(i==0)
            turns=0;
        sum+=v1[i].first;
        turns+=v1[i].second;
        v2.erase(v2.begin()+i);
    }
    reverse(v2.begin(), v2.end());
    for(int i=0;i<v2.size() && i<turns;i++){
        sum+=v2[i].first;
        turns+=v2[i].second;
    }

    cout<<sum;

}


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

using namespace std;
int main()
{
    int n, a, b, cnt=1, point=0;
    vector<int>x;
    cin>>n ;
    for(int i=1; i<=n; i++)
    {
        cin>>a>>b ;

        if(b==0)x.push_back(a);
        else
        {
            cnt=cnt+(b-1);
            point=point+a;
        }

    }
    sort(x.begin(),x.end()) ;
    for(int i=x.size()-1; i>=0; i--)
    {
        point+=x[i];
    }
    cout<<point<<endl;
}


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

using namespace std;
int main()
{
    int n, a, b, cnt=1, point=0;
    vector<int>x;
    cin>>n ;
    for(int i=1; i<=n; i++)
    {
        cin>>a>>b ;

        if(b==0)x.push_back(a);
        else
        {
            cnt=cnt+(b-1);
            point=point+a;
        }

    }
    sort(x.rbegin(),x.rend()) ;
    for(int i=0;i<cnt;i++)
    {
        point+=x[i];
    }
    cout<<point<<endl;
}


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

using namespace std;
int main()
{
    int n, a, b, cnt=1, point=0;
    vector<int>x;
    cin>>n ;
    for(int i=1; i<=n; i++)
    {
        cin>>a>>b ;

        if(b==0)x.push_back(a);
        else
        {
            cnt=cnt+(b-1);
            point=point+a;
        }

    }
    sort(x.begin(),x.end()) ;
    for(int i=x.size()-1; i>=0&&cnt>0; i--,cnt--)
    {
        point+=x[i];
    }
    cout<<point<<endl;
}


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

using namespace std;
int main()
{
    int n, a, b, cnt=1, point=0;
    vector<int>x;
    cin>>n ;
    for(int i=1; i<=n; i++)
    {
        cin>>a>>b ;

        if(b==0)x.push_back(a);
        else
        {
            cnt=cnt+(b-1);
            point=point+a;
        }

    }
    sort(x.rbegin(),x.rend()) ;
    if(x.size())
    for(int i=0;i<cnt;i++)
    {
        point+=x[i];
    }
    cout<<point<<endl;
}


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

int main()
{
    int n, arr[4000][2], b=0, a=0;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i][0]>>arr[i][1];
    }

    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if((arr[i][1]<arr[j][1]))
            {
                swap(arr[i][1], arr[j][1]);
                swap(arr[i][0], arr[j][0]);
            }
            else if((arr[i][1]==arr[j][1])&&arr[i][0]<arr[j][0])
            {
                swap(arr[i][1], arr[j][1]);
                swap(arr[i][0], arr[j][0]);
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        b+=arr[i][1];
        a+=arr[i][0];
        if(b==0)
            break;
    }
    cout<<a<<endl;

    return 0;
}


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

int main(){
	//ios::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);
	srand(time(0));
	int n, start, x;
	cin>>n>>start>>x;
	vector<pair<int, int> > f;
	int val, nxt;
	for(int i=0; i<1000; i++){
		cout<<"? "<<(rand()*i) % n+1<<'\n';
	//	fflush(stdout);
		cin>>val>>nxt;
		f.push_back({val, nxt});
	}
	val=-1, nxt=-1;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<x && f[i].first>val){
			val=f[i].first;
			nxt=f[i].second;
		}
	}
	if (nxt==-1) {
		cout<<"! -1";
		return 0;
	}
	int j=0;
	while(val<x){
		cout<<"? "<<nxt<<'\n';
		cin>>val>>nxt;
		if (val>=x) {
			cout<<"! "<<val;
			return 0;
		}
		else if (nxt==-1){
			cout<<"! -1";
			return 0;
		}

	}
	cout<<"! val";
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, start, k, x, y;
vector<pair<int, int> > f;
int main(){
	srand(time(0));
	cin>>n>>start>>k;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<'\n';
		cin>>x>>y;
		f.push_back({x, y});
	}
	int ans=0;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<k && f[i].first>ans){
			ans=f[i].first;
			start=f[i].second;
		}
	}
	if (start==-1) {
		cout<<"! -1";
		return 0;
	}
	while(1){
		cout<<"? "<<start<<'\n';
		cin>>x>>start;
		if (x>=k) {
			cout<<"! "<<x;
			return 0;
		}
		if (start==-1){
			cout<<"! -1";
			return 0;
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0;
#define ll long long

int n, start, k, x, y;
vector <pair <int, int> > a;

int main(){
    srand(time(0));
    cin >> n >> start >> k;
    for(int i = 0 ; i < 1000 ; i++){
        cout << "? " << rand() * i % n + 1 << endl;
        cin >> x >> y;
        a.push_back(make_pair(x, y));
    }
    int ans = 0;
    for(auto &i : a)
        if(i.first < k && i.first > ans)
            ans = i.first, start = i.second;
    if(start == -1) finish("! -1");
    while(1){
        cout << "? " << start << endl;
        cin >> x >> y;
        if(x >= k) finish("! " << x);
        if(y == -1) finish("! -1");
        start = y;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, start, k, x, y;
vector<pair<int, int> > f;
int main(){
	srand(time(0));
	cin>>n>>start>>k;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<endl;
		cin>>x>>y;
		f.push_back({x, y});
	}
	int ans=0;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<k && f[i].first>ans){
			ans=f[i].first;
			start=f[i].second;
		}
	}
	if (start==-1) {
		cout<<"! -1";
		return 0;
	}
	while(1){
		cout<<"? "<<start<<endl;
		cin>>x>>start;
		if (x>=k) {
			cout<<"! "<<x;
			return 0;
		}
		else if (start==-1){
			cout<<"! -1";
			return 0;
		}

	}
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, start, k, x, y;
vector<pair<int, int> > f;
int main(){
	srand(time(0));
	cin>>n>>start>>k;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<'\n';
		cin>>x>>y;
		f.push_back({x, y});
	}
	int ans=0;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<k && f[i].first>ans){
			ans=f[i].first;
			start=f[i].second;
		}
	}
	if (start==-1) {
		cout<<"! -1";
		return 0;
	}
	while(1){
		cout<<"? "<<start<<'\n';
		cin>>x>>y;
		if (x>=k) {
			cout<<"! "<<x;
			return 0;
		}
		if (y==-1){
			cout<<"! -1";
			return 0;
		}
		start=y;
	}
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, start, k, x, y;
vector<pair<int, int> > f;
int main(){
	srand(time(0));
	cin>>n>>start>>k;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<endl;
		cin>>x>>y;
		f.push_back({x, y});
	}
	int ans=0;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<k && f[i].first>ans){
			ans=f[i].first;
			start=f[i].second;
		}
	}
	if (start==-1) {
		cout<<"! -1";
		return 0;
	}
	while(1){
		cout<<"? "<<start<<endl;
		cin>>x>>y;
		if (x>=k) {
			cout<<"! "<<x;
			return 0;
		}
		if (y==-1){
			cout<<"! -1";
			return 0;
		}
		start=y;
	}
}


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

int main(){
	//ios::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);
	srand(time(0));
	int n, start, x;
	cin>>n>>start>>x;
	vector<pair<int, int> > f;
	int val, nxt;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<'\n';
	//	fflush(stdout);
		cin>>val>>nxt;
		f.push_back({val, nxt});
	}
	val=-1, nxt=-1;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first==x) {
			cout<<"! "<<x;
			return 0;
		}
		else if (f[i].first<x && f[i].first>val){
			val=f[i].first;
			nxt=f[i].second;
		}
	}
	if (nxt==-1) {
		cout<<"! -1";
		return 0;
	}
	int j=0;
	while(val<x && ++j<1000){
		cout<<"? "<<nxt<<'\n';
		cin>>val>>nxt;
		if (val>=x) {
			cout<<"! "<<val;
			return 0;
		}
		else if (nxt==-1){
			cout<<"! -1";
			return 0;
		}

	}
	cout<<"! -1";
}


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

int main(){
	//ios::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);
	srand(time(0));
	int n, start, x;
	cin>>n>>start>>x;
	vector<pair<int, int> > f;
	int val, nxt;
	for(int i=0; i<1000; i++){
		cout<<"? "<<rand()*i % n+1<<'\n';
	//	fflush(stdout);
		cin>>val>>nxt;
		f.push_back({val, nxt});
	}
	val=-1, nxt=-1;
	for(int i=0; i<(int)f.size(); i++){
		if (f[i].first<x && f[i].first>val){
			val=f[i].first;
			nxt=f[i].second;
		}
	}
	if (nxt==-1) {
		cout<<"! -1";
		return 0;
	}
	while(1){
		cout<<"? "<<nxt<<'\n';
		cin>>val>>nxt;
		if (val>=x) {
			cout<<"! "<<val;
			return 0;
		}
		else if (nxt==-1){
			cout<<"! -1";
			return 0;
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 2e5 + 10;
const int inf = 1e9 + 10;
int fl[sz], n;

int main() {
    int s, x, val, nxt, ans, mn_mx, id;
    scanf("%d %d %d", &n, &s, &x);
    if(n < 2000) {
        ans = inf;
        for(int i = 1; i <= n; i++) {
            printf("? %d\n", i);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) ans = min(ans, val);
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    else {
        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
        uniform_int_distribution<int> distribution(1, n);
        ans = inf;
        int h;
        mn_mx = 0;
        id = -1;
        printf("? %d\n", s);
        fflush(stdout);
        scanf("%d %d", &mn_mx, &id);
        if(mn_mx >= x) ans = mn_mx;
        id = s;
        for(int i = 1; i < 1000; i++) {
            while(fl[h = distribution(rng)]);
            fl[ h ] = 1;
            printf("? %d\n", h);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = min(ans, val);
                if(nxt != -1) fl[nxt] = 1;
            }
            if(val <= x) {
                if(mn_mx < val) {
                    mn_mx = val;
                    id = nxt;
                }
            }
        }
        for(int i=1; i<1000; i++) {
            if(id == -1) break;
            printf("? %d\n", id);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = val;
                break;
            }
            id = nxt;
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 2e5 + 10;
const int inf = 1e9 + 10;
int fl[sz], n;

int main() {
    int s, x, val, nxt, ans, mn_mx, id;
    scanf("%d %d %d", &n, &s, &x);
    if(n < 2000) {
        ans = inf;
        for(int i = 1; i <= n; i++) {
            printf("? %d\n", i);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) ans = min(ans, val);
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    else {
        mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
        uniform_int_distribution<int> distribution(1, n);
        ans = inf;
        int h;
        mn_mx = 0;
        id = -1;
        printf("? %d\n", s);
        fflush(stdout);
        scanf("%d %d", &mn_mx, &id);
        if(mn_mx >= x) ans = mn_mx;
        id = s;
        for(int i = 1; i < 1000; i++) {
            while(fl[h = distribution(rng)]);
            fl[ h ] = 1;
            printf("? %d\n", h);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = min(ans, val);
                if(nxt != -1) fl[nxt] = 1;
            }
            if(val <= x) {
                if(mn_mx < val) {
                    mn_mx = val;
                    id = nxt;
                }
            }
        }
        for(int i=1; i<1000; i++) {
            if(id == -1) break;
            printf("? %d\n", id);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = val;
                break;
            }
            id = nxt;
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 2e5 + 10;
const int inf = 1e9 + 10;
int fl[sz], n;

int Rand(mt19937_64 rng) {
    return rng() % n + 1;
}

int main() {
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    int s, x, val, nxt, ans, mn_mx, id;
    scanf("%d %d %d", &n, &s, &x);
    if(n < 2000) {
        ans = inf;
        for(int i = 1; i <= n; i++) {
            printf("? %d\n", i);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) ans = min(ans, val);
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    else {
        ans = inf;
        int h;
        mn_mx = 0;
        id = -1;
        printf("? %d\n", s);
        fflush(stdout);
        scanf("%d %d", &mn_mx, &id);
        if(mn_mx >= x) ans = mn_mx;
        id = s;
        for(int i = 1; i < 1000; i++) {
            while(fl[h = Rand(rng)]);
            fl[ h ] = 1;
            printf("? %d\n", h);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = min(ans, val);
                if(nxt != -1) fl[nxt] = 1;
            }
            if(val <= x) {
                if(mn_mx < val) {
                    mn_mx = val;
                    id = nxt;
                }
            }
        }
        for(int i=1; i<1000; i++) {
            if(id == -1) break;
            printf("? %d\n", id);
            fflush(stdout);
            scanf("%d %d", &val, &nxt);
            if(val >= x) {
                ans = val;
                break;
            }
            id = nxt;
        }
        if(ans == inf) ans = -1;
        printf("! %d\n", ans);
        fflush(stdout);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 2e5 + 10;
const int inf = 1e9 + 10;
int fl[sz], n;

int Rand() {
    return ( rand() * rand( ) ) % n + 1;
}

int main() {
    #ifdef CLown1331
//        freopen( "in.txt","r",stdin );
    #endif /// CLown1331
    int s, x, val, nxt, ans, mn_mx, id;
    scanf( "%d %d %d", &n, &s, &x );
    if( n < 2000 ) {
        ans = inf;
        for( int i=1; i<=n; i++ ) {
            printf( "? %d\n", i );
            fflush( stdout );
            scanf( "%d %d", &val, &nxt );
            if( val >= x ) ans = min( ans, val );
        }
        if( ans == inf ) ans = -1;
        printf( "! %d\n", ans );
        fflush( stdout );
    }
    else {
        ans = inf;
        srand( time( NULL ) );
        int h;
        mn_mx = 0;
        id = -1;
        printf( "? %d\n", s );
        fflush( stdout );
        scanf( "%d %d", &mn_mx, &id );
        if( mn_mx >= x ) ans = mn_mx;
        id = s;
        for( int i=1; i<1000; i++ ) {
            while( fl[ h = Rand() ] );
            fl[ h ] = 1;
            printf( "? %d\n", h );
            fflush( stdout );
            scanf( "%d %d", &val, &nxt );
            if( val >= x ) {
                ans = min( ans, val );
                if( nxt != -1 ) fl[nxt] = 1;
            }
            if( val <= x ) {
                if( mn_mx < val ) {
                    mn_mx = val;
                    id = nxt;
                }
            }
        }
        for( int i=1; i<1000; i++ ) {
            if( id == -1 ) break;
            printf( "? %d\n", id );
            fflush( stdout );
            scanf( "%d %d", &val, &nxt );
            if( val >= x ) {
                ans = val;
                break;
            }
            id = nxt;
        }
        if( ans == inf ) ans = -1;
        printf( "! %d\n", ans );
        fflush( stdout );
    }
    return 0;
}


#include <cstdio>
#include <random>
#include <chrono>
#include <cstdlib>

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

std::pair<int,int> query(int i){
  printf("? %d\n",i);
  fflush(stdout);
  int val,next;
  scanf("%d %d",&val,&next);
  if(val==-1&&next==-1) exit(0);
  return {val,next};
}

int main(){
  int N,S,X;
  scanf("%d %d %d",&N,&S,&X);
  std::uniform_int_distribution<int> distr(1,N);
  int V=0;//lower bound on val[S]
  for(int it=0;it<1999;it++){
    auto res=query((it<1000)?distr(rng):S);
    if(res.first<X&&res.first>V){
      S=res.second;
      V=res.first;
    }
    if(S==-1){
      printf(" -1\n");
      return 0;
    }
  }
  printf("! %d\n",query(S).first);
}


#include <cstdio>
#include <random>
#include <chrono>
#include <cstdlib>

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

std::pair<int,int> query(int i){
  printf("? %d\n",i);
  fflush(stdout);
  int val,next;
  scanf("%d %d",&val,&next);
  if(val==-1&&next==-1) exit(0);
  return {val,next};
}

int main(){
  int N,S,X;
  scanf("%d %d %d",&N,&S,&X);
  std::uniform_int_distribution<int> distr(1,N);
  int V=0;//lower bound on val[S]
  for(int it=0;it<1998;it++){
    auto res=query((it<1000)?distr(rng):S);
    if(res.first<X&&res.first>V){
      S=res.second;
      V=res.first;
    }
    if(S==-1){
      printf("! -1\n");
      return 0;
    }
  }
  printf("! %d\n",query(S).first);
}


#include <cstdio>
#include <random>
#include <chrono>
#include <cstdlib>

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

std::pair<int,int> query(int i){
  printf("? %d\n",i);
  fflush(stdout);
  int val,next;
  scanf("%d %d",&val,&next);
  if(val==-1&&next==-1) exit(0);
  return {val,next};
}

int main(){
  int N,S,X;
  scanf("%d %d %d",&N,&S,&X);
  std::uniform_int_distribution<int> distr(1,N);
  int V=-1;//lower bound on val[S]
  for(int it=0;it<1998;it++){
    auto res=query((it<1000)?distr(rng):S);
    if(res.first<X&&res.first>V){
      S=res.second;
      V=res.first;
    }
    if(S==-1){
      printf("! -1\n");
      return 0;
    }
  }
  printf("! %d\n",query(S).first);
}


#include <cstdio>
#include <random>
#include <chrono>
#include <cstdlib>

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

std::pair<int,int> query(int i){
  printf("? %d\n",i);
  fflush(stdout);
  int val,next;
  scanf("%d %d",&val,&next);
  if(val==-1&&next==-1) exit(0);
  return {val,next};
}

int main(){
  int N,S,X;
  scanf("%d %d %d",&N,&S,&X);
  std::uniform_int_distribution<int> distr(1,N);
  int V=0;//lower bound on val[S]
  for(int it=0;it<1998;it++){
    auto res=query((it<1000)?distr(rng):S);
    if(res.first<X&&res.first>V){
      S=res.second;
      V=res.first;
    }
    if(S==-1){
      printf(" -1\n");
      return 0;
    }
  }
  printf("! %d\n",query(S).first);
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	query(st);
  	isV[st-1] = true;
  	ar.pb(mp(val, nxt));
  	// Random query first 999 elements
  	for(ll i = 0; i < 1499; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}

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

  	ll something;
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi < x)
  			something = i.se;
  	}

  	for(ll i = 0; i < 500; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(something);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  		something = nxt;
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	fflush(stdout);
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = ub(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx >= n)
  		cout << "! " << -1 << el;
  	else{
  		cout << "! " << ar[idx] << el;
  	}
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[val] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000 && val < x && nxt != -1; i++){
  		if(isV[nxt])
  			break;
  		query(nxt);
  	}
  	if(val < x || nxt == -1)
  		cout << "! -1" << el;
  	else
  		cout << "! " << val << el;
  }
  fflush(stdout);
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val, nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

	ll n, st, x;
	cin >> n >> st >> x;
	if(n <= 2000){
		vector<ll> ar(n);
		for(ll i = 0; i < n; i++){
			query(i+1);
			ar[i] = val;
		}
		sort(ar.begin(), ar.end());
		auto iter = lb(ar.begin(), ar.end(), x);
		if(iter == ar.end())
			cout << "! -1" << endl;
		else
			cout << "! " << ar[iter - ar.begin()] << endl;
	}
	else{
		vector<pair<ll, ll>> ar;
		vector<bool> isV(n+1, false);
		ll pos = st;
		query(st);
		isV[st] = true;
		ll startV = val;
		ar.pb(mp(val, nxt));
		ll idx;
		for(ll i = 0; i < 1499; i++){
			do{
				idx = (rng()%n) + 1;
			}while(isV[idx]);
			isV[idx] = false;
			query(idx);
			ar.pb(mp(val, nxt));
		}

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

		val = startV;
		nxt = pos;
		for(auto p : ar){
			if(p.fi == x){
				cout << "! " << x << endl;
				exit(0);
			}
			if(p.fi > x)
				break;
			val = p.fi;
			nxt = p.se;
		}
		for(ll i = 0; i < 500; i++){
			if(val >= x){
				cout << "! " << val << endl;
				exit(0);
			}
			if(nxt == -1)
				cout << "! -1" << endl;
			query(nxt);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	query(st);
  	isV[st-1] = true;
  	ar.pb(mp(val, nxt));
  	// Random query first 999 elements
  	for(ll i = 0; i < 1499; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  		val = i.fi;
  	}
  	for(ll i = 0; i < 500; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	fflush(stdout);
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = ub(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx >= n)
  		cout << "! " << -1 << el;
  	else{
  		cout << "! " << ar[idx] << el;
  	}
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[val] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000 && val < x && nxt != -1; i++){
  		if(isV[nxt])
  			break;
  		query(nxt);
  	}
  	if(val < x || nxt == -1)
  		cout << "! -1" << el;
  	else
  		cout << "! " << val << el;
  }
  fflush(stdout);
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  	}
  	for(ll i = 0; i < 1000; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	if (a.fi != b.fi)
		return a.fi < b.fi;
	return a.se > b.se;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{

  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n+1, false);
  	ll idx;
  	// cout << "? " << st << endl;
  	// cin >> val >> nxt;
  	query(st);
  	isV[st] = true;
  	ar.pb(mp(val, nxt));
    ll tmp = val;
  	for(ll i = 0; i < 1499; i++){
  		do{
  			idx = (rng() % n)+1;
  		}while(isV[idx]);
  		isV[idx] = true;
  		// cout << "? " << idx << endl;
  		// cin >> val >> nxt;
  		query(idx);
  		ar.pb(mp(val, nxt));
  	}

  	sort(ar.begin(), ar.end());
  	nxt = st;
    val = tmp;
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		if(i.fi > x)
        break
      val = i.fi;
  		nxt = i.se;
  	}

  	for(ll i = 0; i < 500; i++){
  		if(val >= x){
  			cout << "! " << val << endl;
  			exit(0);
  		}
  		if(nxt == -1)
  			break;
  		query(nxt);
  		// cout << "? " << nxt << endl;
  		// cin >> val >> nxt;
  	}
  	cout << "! -1" << endl;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000; i++){
  		// if(isV[nxt])
  		// 	break;
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  		query(nxt);
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val, nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

	ll n, st, x;
	cin >> n >> st >> x;
	if(n <= 2000){
		vector<ll> ar(n);
		for(ll i = 0; i < n; i++){
			query(i+1);
			ar[i] = val;
		}
		sort(ar.begin(), ar.end());
		auto iter = lb(ar.begin(), ar.end(), x);
		if(iter == ar.end())
			cout << "! -1" << endl;
		else
			cout << "! " << ar[iter - ar.begin()] << endl;
	}
	else{
		vector<pair<ll, ll>> ar;
		vector<bool> isV(n+1, false);
		ll pos = st;
		query(st);
		isV[st] = true;
		ll startV = val;
		ar.pb(mp(val, nxt));
		ll idx;
		for(ll i = 0; i < 1499; i++){
			do{
				idx = (rng()%n) + 1;
			}while(isV[idx]);
			isV[idx] = false;
			query(idx);
			ar.pb(mp(val, nxt));
		}

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

		val = startV;
		nxt = pos;
		for(auto p : ar){
			if(p.fi == x){
				cout << "! " << x << endl;
				exit(0);
			}
			if(p.fi > x)
				break;
			val = p.fi;
			nxt = p.se;
		}
		for(ll i = 0; i < 500; i++){
			query(nxt);
			if(val >= x){
				cout << "! " << val << endl;
				exit(0);
			}
			if(nxt == -1)
				cout << "! -1" << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	query(st);
  	isV[st-1] = true;
  	ar.pb(mp(val, nxt));
  	// Random query first 999 elements
  	for(ll i = 0; i < 1499; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  	}
  	for(ll i = 0; i < 500; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	query(st);
  	isV[st-1] = true;
  	ar.pb(mp(val, nxt));
  	// Random query first 999 elements
  	for(ll i = 0; i < 1499; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  		val = i.fi;
  	}
  	for(ll i = 0; i < 500; i++){
  		// if(isV[nxt])
  		// 	break;
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  		query(nxt);
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	if (a.fi != b.fi)
		return a.fi < b.fi;
	return a.se > b.se;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	// vector<pair<ll, ll>> ar;
  	// vector<bool> isV(n+1, false);
  	// ll idx;
  	// query(st);
  	// isV[st] = true;
  	// // cout << "? " << st << endl;
  	// // cin >> val >> nxt;
  	// ar.pb(mp(val, nxt));
  	// // Random query first 999 elements
  	// for(ll i = 0; i < 1499; i++){
	  // 	do{
	  // 		idx = (rng() % n) + 1;
	  // 	}while(isV[idx]);
	  // 	query(idx);
	  // 	// cout << "? " << idx << endl;
	  // 	// cin >> val >> nxt;
	  // 	isV[idx] = true;
	  // 	ar.pb(mp(val, idx));
  	// }

  	// // sort(ar.begin(), ar.end(), cmp);
  	// sort(ar.begin(), ar.end());

  	// for(auto i : ar){
  	// 	if(i.fi == x){
  	// 		cout << "! " << x << endl;
  	// 		exit(0);
  	// 	}
  	// 	else if(i.fi > x)
  	// 		break;
  	// 	val = i.fi;
  	// 	nxt = i.se;
  	// }

  	// for(ll i = 0; i < 500; i++){
  	// 	// if(isV[nxt])
  	// 	// 	break;
  	// 	query(nxt);
  	// 	// cout << "? " << nxt << endl;
  	// 	// cin >> val >> nxt;
  	// 	if(val >= x){
  	// 		cout << "! " << val << el;
  	// 		break;
  	// 	}
  	// 	if(nxt == -1){
  	// 		cout << "! -1" << el;
  	// 		break;
  	// 	}
  	// }

  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n+1, false);
  	ll idx;
  	cout << "? " << st << endl;
  	isV[st] = true;
  	cin >> val >> nxt;
  	ar.pb(mp(val, nxt));

  	for(ll i = 0; i < 1499; i++){
  		do{
  			idx = (rng() % n)+1;
  		}while(isV[idx]);
  		isV[idx] = true;
  		cout << "? " << idx << endl;
  		cin >> val >> nxt;
  		ar.pb(mp(val, nxt));
  	}

  	sort(ar.begin(), ar.end());
  	nxt = st;
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		if(i.fi > x)
  			break;
  		val = i.fi;
  		nxt = i.se;
  	}

  	for(ll i = 0; i < 500; i++){
  		cout << "? " << nxt << endl;
  		cin >> val >> nxt;
  		if(val >= x){
  			cout << "! " << val << endl;
  			exit(0);
  		}
  		if(nxt == -1)
  			break;
  	}
  	cout << "! -1" << endl;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	query(st);
  	isV[st-1] = true;
  	ar.pb(mp(val, nxt));
  	// Random query first 999 elements
  	for(ll i = 0; i < 1499; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  	}
  	for(ll i = 0; i < 500; i++){
  		// if(isV[nxt])
  		// 	break;
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  		query(nxt);
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val, nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

	ll n, st, x;
	cin >> n >> st >> x;
	if(n <= 2000){
		vector<ll> ar(n);
		for(ll i = 0; i < n; i++){
			query(i+1);
			ar[i] = val;
		}
		sort(ar.begin(), ar.end());
		auto iter = lb(ar.begin(), ar.end(), x);
		if(iter == ar.end())
			cout << "! -1" << endl;
		else
			cout << "! " << ar[iter - ar.begin()] << endl;
	}
	else{
		vector<pair<ll, ll>> ar;
		vector<bool> isV(n+1, false);
		ll pos = st;
		query(st);
		isV[st] = true;
		ar.pb(mp(val, nxt));
		ll idx;
		for(ll i = 0; i < 1499; i++){
			do{
				idx = (rng()%n) + 1;
			}while(isV[idx]);
			isV[idx] = false;
			query(idx);
			ar.pb(mp(val, nxt));
		}

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

		for(auto p : ar){
			if(p.fi == x){
				cout << "! " << x << endl;
				exit(0);
			}
			if(p.fi > x)
				break;
			pos = p.se;
		}
		nxt = pos;
		for(ll i = 0; i < 500; i++){
			query(nxt);
			if(val >= x){
				cout << "! " << val << endl;
				exit(0);
			}
			if(nxt == -1)
				cout << "! -1" << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	if (a.fi != b.fi)
		return a.fi < b.fi;
	return a.se > b.se;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	// vector<pair<ll, ll>> ar;
  	// vector<bool> isV(n+1, false);
  	// ll idx;
  	// query(st);
  	// isV[st] = true;
  	// // cout << "? " << st << endl;
  	// // cin >> val >> nxt;
  	// ar.pb(mp(val, nxt));
  	// // Random query first 999 elements
  	// for(ll i = 0; i < 1499; i++){
	  // 	do{
	  // 		idx = (rng() % n) + 1;
	  // 	}while(isV[idx]);
	  // 	query(idx);
	  // 	// cout << "? " << idx << endl;
	  // 	// cin >> val >> nxt;
	  // 	isV[idx] = true;
	  // 	ar.pb(mp(val, idx));
  	// }

  	// // sort(ar.begin(), ar.end(), cmp);
  	// sort(ar.begin(), ar.end());

  	// for(auto i : ar){
  	// 	if(i.fi == x){
  	// 		cout << "! " << x << endl;
  	// 		exit(0);
  	// 	}
  	// 	else if(i.fi > x)
  	// 		break;
  	// 	val = i.fi;
  	// 	nxt = i.se;
  	// }

  	// for(ll i = 0; i < 500; i++){
  	// 	// if(isV[nxt])
  	// 	// 	break;
  	// 	query(nxt);
  	// 	// cout << "? " << nxt << endl;
  	// 	// cin >> val >> nxt;
  	// 	if(val >= x){
  	// 		cout << "! " << val << el;
  	// 		break;
  	// 	}
  	// 	if(nxt == -1){
  	// 		cout << "! -1" << el;
  	// 		break;
  	// 	}
  	// }

  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n+1, false);
  	ll idx;
  	cout << "? " << st << endl;
  	cin >> val >> nxt;
  	ar.pb(mp(val, nxt));

  	for(ll i = 0; i < 1499; i++){
  		do{
  			idx = (rng() % n)+1;
  		}while(isV[idx]);
  		cout << "? " << idx << endl;
  		cin >> val >> nxt;
  		ar.pb(mp(val, idx));
  	}

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

  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		if(i.fi > x)
  			break;
  		val = i.fi;
  		nxt = i.se;
  	}

  	for(ll i = 0; i < 500; i++){
  		cout << "? " << nxt << endl;
  		cin >> val >> nxt;
  		if(val >= x){
  			cout << "! " << val << endl;
  			exit(0);
  		}
  		if(nxt == -1)
  			break
  	}
  	cout << "! -1" << endl;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << endl;
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	if (a.fi != b.fi)
		return a.fi < b.fi;
	return a.se > b.se;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	// vector<pair<ll, ll>> ar;
  	// vector<bool> isV(n+1, false);
  	// ll idx;
  	// query(st);
  	// isV[st] = true;
  	// // cout << "? " << st << endl;
  	// // cin >> val >> nxt;
  	// ar.pb(mp(val, nxt));
  	// // Random query first 999 elements
  	// for(ll i = 0; i < 1499; i++){
	  // 	do{
	  // 		idx = (rng() % n) + 1;
	  // 	}while(isV[idx]);
	  // 	query(idx);
	  // 	// cout << "? " << idx << endl;
	  // 	// cin >> val >> nxt;
	  // 	isV[idx] = true;
	  // 	ar.pb(mp(val, idx));
  	// }

  	// // sort(ar.begin(), ar.end(), cmp);
  	// sort(ar.begin(), ar.end());

  	// for(auto i : ar){
  	// 	if(i.fi == x){
  	// 		cout << "! " << x << endl;
  	// 		exit(0);
  	// 	}
  	// 	else if(i.fi > x)
  	// 		break;
  	// 	val = i.fi;
  	// 	nxt = i.se;
  	// }

  	// for(ll i = 0; i < 500; i++){
  	// 	// if(isV[nxt])
  	// 	// 	break;
  	// 	query(nxt);
  	// 	// cout << "? " << nxt << endl;
  	// 	// cin >> val >> nxt;
  	// 	if(val >= x){
  	// 		cout << "! " << val << el;
  	// 		break;
  	// 	}
  	// 	if(nxt == -1){
  	// 		cout << "! -1" << el;
  	// 		break;
  	// 	}
  	// }

  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n+1, false);
  	ll idx;
  	cout << "? " << st << endl;
  	isV[st] = true;
  	cin >> val >> nxt;
  	ar.pb(mp(val, nxt));

  	for(ll i = 0; i < 1499; i++){
  		do{
  			idx = (rng() % n)+1;
  		}while(isV[idx]);
  		isV[idx] = true;
  		cout << "? " << idx << endl;
  		cin >> val >> nxt;
  		ar.pb(mp(val, idx));
  	}

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

  	ll tmp;

  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		if(i.fi < x)
  			tmp = i.se;
  		// 	break;
  		// val = i.fi;
  		// nxt = i.se;
  	}

  	for(ll i = 0; i < 500; i++){
  		cout << "? " << tmp << endl;
  		cin >> val >> nxt;
  		if(val >= x){
  			cout << "! " << val << endl;
  			exit(0);
  		}
  		if(nxt == -1)
  			break;
  		tmp = nxt;
  	}
  	cout << "! -1" << endl;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[val] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000 && val < x && nxt != -1; i++){
  		if(isV[nxt])
  			break;
  		query(nxt);
  	}
  	if(val < x || nxt == -1)
  		cout << "! -1" << el;
  	else
  		cout << "! " << val << el;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000 && val < x && nxt != -1; i++){
  		if(isV[nxt])
  			break;
  		query(nxt);
  	}
  	if(val < x || nxt == -1)
  		cout << "! -1" << el;
  	else
  		cout << "! " << val << el;
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  // if(n <= 2000){
  if(false){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	for(auto i : ar){
  		if(i.fi == x){
  			cout << "! " << x << endl;
  			exit(0);
  		}
  		else if(i.fi > x)
  			break;
  		nxt = i.se;
  	}
  	for(ll i = 0; i < 1000; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  		if(val >= x){
  			cout << "! " << val << el;
  			break;
  		}
  		if(nxt == -1){
  			cout << "! -1" << el;
  			break;
  		}
  	}
  }
  cout.flush();
	return 0;
}


#include <bits/stdc++.h>

typedef int64_t ll;
typedef uint64_t ull;
typedef double db;

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define prs(n) cout << fixed << setprecision(n)
#define DEBUG(val) cerr << "#####\nRunning on Line " << __LINE__ << " in Function " << 	__FUNCTION__ << el << val << "#####";
#define el "\n"
#define sp " "

// RNG - for sum fun
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// Combination calculate
ll nCr(ll n, ll r){
	// The fomular:
	// n! / (r! * (n-r)!)
	// Complexity: O(r)
	ll tmp = n-r;
	for(ll i = n-1; i > tmp; i--)
		n *= i;

	for(ll i = r-1; i > 1; i--)
		r *= i;
	return n/r;
}

ll val;
ll nxt;

void query(ll idx){
	cout << "? " << idx << el;
	cout.flush();
	cin >> val >> nxt;
	if(val == -1 && nxt == -1)
		exit(0);
}

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b){
	return a.fi < b.fi;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	setvbuf(stdin, 0, _IONBF, 0);
	setvbuf(stdout, 0, _IONBF, 0);
	setvbuf(stderr, 0, _IONBF, 0);
#ifdef LOCAL_PROJECT
  freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif

  ll st, n, x;
  cin >> n >> st >> x;
  if(n <= 2000){
  	vector<ll> ar;
  	for(ll i = 0; i < n; i++){
  		query(i+1);
  		ar.pb(val);
  	}
  	sort(ar.begin(), ar.end());
  	ll idx = lb(ar.begin(), ar.end(), x) - ar.begin();
  	if(idx == n){
  		cout << "! -1" << el;
			cout.flush();
			exit(0);
  	}
  	while(ar[idx] < x){
  		idx++;
  		if(idx >= n){
  			cout << "! -1" << el;
  			cout.flush();
  			exit(0);
  		}
  	}
  	cout << "! " << ar[idx] << el;
  }
  else{
  	vector<pair<ll, ll>> ar;
  	vector<bool> isV(n, false);
  	ll idx;
  	// Random query first 1000 elements
  	for(ll i = 0; i < 1000; i++){
	  	do{
	  		idx = rng() % n;
	  	}while(isV[idx]);
	  	query(idx+1);
	  	isV[idx] = true;
	  	ar.pb(mp(val, nxt));
  	}
  	sort(ar.begin(), ar.end(), cmp);
  	idx = lb(ar.begin(), ar.end(), mp(x,0), cmp) - ar.begin();
  	val = ar[idx].fi;
  	nxt = ar[idx].se;
  	for(ll i = 0; i < 1000 && val < x && nxt != -1; i++){
  		// if(isV[nxt])
  		// 	break;
  		query(nxt);
  	}
  	if(val < x || nxt == -1)
  		cout << "! -1" << el;
  	else
  		cout << "! " << val << el;
  }
  cout.flush();
	return 0;
}