import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class cf706d {

    public static void main(String[] args) throws IOException {
        int q = ri();
        Map<Integer, Integer> cnt = new HashMap<>();
        Trie t = new Trie(30);
        t.insert(0);
        while (q --> 0) {
            r();
            char op = input.nextToken().charAt(0);
            int x = ni();
            if (op == '+') {
                cnt.put(x, cnt.getOrDefault(x, 0) + 1);
                if (cnt.get(x) == 1) {
                    t.insert(x);
                }
            } else if (op == '-') {
                cnt.put(x, cnt.get(x) - 1);
                if (cnt.get(x) == 0) {
                    t.remove(x);
                }
            } else {
                prln(x ^ t.max(x));
            }
        }
        close();
    }

    static class Trie {
        Trie[] chd = new Trie[2];
        int lvl, cnt = 0;

        Trie(int l) {
            lvl = l;
        }

        void remove(int x) {
            int bit = (x & (1 << lvl)) > 0 ? 1 : 0;
            --cnt;
            if (lvl >= 0) {
                chd[bit].remove(x);
            }
        }

        void insert(int x) {
            ++cnt;
            if (lvl >= 0) {
                int bit = (x & (1 << lvl)) > 0 ? 1 : 0;
                if (chd[bit] == null) {
                    chd[bit] = new Trie(lvl - 1);
                }
                chd[bit].insert(x);
            }
        }

        int max(int x) {
            if (lvl < 0) {
                return 0;
            }
            int bit = (x & (1 << lvl)) > 0 ? 0 : 1;
            if (chd[bit] == null || chd[bit].cnt == 0) {
                bit ^= 1;
            }
            return (bit << lvl) + chd[bit].max(x);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int)d;}
    static int cei(double d) {return (int)ceil(d);}
    static long fll(double d) {return (long)d;}
    static long cel(double d) {return (long)ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // graph util
    static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
    static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
    static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
    static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
    static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
    static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
    // input
    static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
    static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
    static long rl() throws IOException {return Long.parseLong(__in.readLine());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
    static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
    static int ni() {return Integer.parseInt(input.nextToken());}
    static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
    static long nl() {return Long.parseLong(input.nextToken());}
    static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
    static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
    static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
    static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
    static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
    static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
    static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
    static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {__out.println("yes");}
    static void pry() {__out.println("Yes");}
    static void prY() {__out.println("YES");}
    static void prno() {__out.println("no");}
    static void prn() {__out.println("No");}
    static void prN() {__out.println("NO");}
    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();}
    static void h() {__out.println("hlfd");}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;
import java.math.*;

public class Trie_UT {
	InputStream is;
	PrintWriter pr;

	long mod = (long) (1e9 + 7), inf = (long) (3e18);

	class trie {
		int value;
		int count;
		trie[] d;

		trie() {
			d = new trie[2];
			value = count = 0;
		}

		void insert(int n) {
			trie t = this;
			for (int i = 31; i >= 0; i--) {
				int j = ((n >> i) & 1);
				if (t.d[j] == null)
					t.d[j] = new trie();
				t = t.d[j];
				t.count++;
			}
			t.value=n;
		}

		void remove(int n) {
			trie t = this;
			for (int i = 31; i >= 0; i--) {
				int j = ((n >> i) & 1);
				t = t.d[j];
				t.count--;
			}
		}

		int query(int n) {
			trie t = this;
			for (int i = 31; i >= 0; i--) {
				int j = 1 - ((n >> i) & 1);
				if (t.d[j] != null && t.d[j].count > 0)
					t = t.d[j];
				else
					t = t.d[1 - j];
			}
			return t.value ^ n;
		}
	}

	void solve() {
		int q = ni();
		trie t = new trie();
		t.insert(0);
		while (q-- > 0) {
			char c = nc();
			int n = ni();
			if (c == '+') {
				t.insert(n);
			} else if (c == '-')
				t.remove(n);
			else
				pr.println(t.query(n));
		}

	}

	public static void main(String[] args) {
		new Trie_UT().run();
	}

	void run() {
		is = System.in;
		pr = new PrintWriter(System.out);
		solve();
		pr.flush();
	}

	byte input[] = new byte[1024];
	int len = 0, ptr = 0;

	int readByte() {
		if (ptr >= len) {
			ptr = 0;
			try {
				len = is.read(input);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (len <= 0) {
				return -1;
			}
		}
		return input[ptr++];
	}

	boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	int skip() {
		int b = readByte();
		while (b != -1 && isSpaceChar(b)) {
			b = readByte();
		}
		return b;
	}

	char nc() {
		return (char) skip();
	}

	String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!isSpaceChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	String nLine() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b) && b != ' ')) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	int ni() {
		int n = 0, b = readByte();
		boolean minus = false;
		while (b != -1 && !((b >= '0' && b <= '9') || b == '-')) {
			b = readByte();
		}
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b == -1) {
			return -1;
		} // no input
		while (b >= '0' && b <= '9') {
			n = n * 10 + (b - '0');
			b = readByte();
		}
		return minus ? -n : n;
	}

	long nl() {
		long n = 0L;
		int b = readByte();
		boolean minus = false;
		while (b != -1 && !((b >= '0' && b <= '9') || b == '-')) {
			b = readByte();
		}
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (b >= '0' && b <= '9') {
			n = n * 10 + (b - '0');
			b = readByte();
		}
		return minus ? -n : n;
	}

	double nd() {
		return Double.parseDouble(ns());
	}

	float nf() {
		return Float.parseFloat(ns());
	}

	int[] na(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = ni();
		}
		return a;
	}

	char[] ns(int n) {
		char c[] = new char[n];
		int i, b = skip();
		for (i = 0; i < n; i++) {
			if (isSpaceChar(b)) {
				break;
			}
			c[i] = (char) b;
			b = readByte();
		}
		return i == n ? c : Arrays.copyOf(c, i);
	}
}


#include <iostream>
#include <string.h>

using namespace std;

const int Maxn = 1e7+10;

int sl = 1;
char c; long long t;
int trie[Maxn][2], cnt[2*Maxn];

void Add(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(long long t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << ret << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


#include <iostream>
#include <string.h>

using namespace std;

const int Maxn = 1e5+1;

int sl = 1;
int trie[Maxn][2], cnt[2*Maxn];

void Add(int t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(int t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(int t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << ret << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        char c;
        int t;
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


#include <iostream>
#include <string.h>

using namespace std;

const int Maxn = 1e7+5;

int sl = 1;
long long trie[Maxn][2], cnt[2*Maxn];

void Add(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(long long t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << ret << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        char c;
        long long t;
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

const int Maxn = 1e7+10;

int sl = 1;
char c; long long t;
int trie[Maxn][2], cnt[2*Maxn];

void Add(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(long long t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << max(ret,t) << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


#include <iostream>
#include <string.h>

using namespace std;

const int Maxn = 2e5+5;

int sl = 1;
int trie[Maxn][2], cnt[2*Maxn];

void Add(int t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(int t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(int t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << ret << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        char c;
        int t;
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


#include <iostream>
#include <string.h>

using namespace std;

const int Maxn = 1e6+5;

int sl = 1;
long long trie[Maxn][2], cnt[2*Maxn];

void Add(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] ++;
    }
}

void Del(long long t)
{
    int root = 1;
    for (int i = 31; i >= 0; --i)
    {
        int bit = (t >> i) & 1;
        //if (trie[root][bit] == 0) trie[root][bit] = ++sl;
        root = trie[root][bit];
        cnt[root] --;
    }
}

void Search(long long t)
{
    int root = 1; long long ret = 0;
    for (int i=31; i>=0; i--)
    {
        int bit = (t >> i) & 1;
        if (cnt[trie[root][1-bit]] > 0)
        {
            root = trie[root][1-bit];
            ret += (1 << i);
        }
        else root=trie[root][bit];
    }
    cout << ret << "\n";
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; i++)
    {
        char c;
        long long t;
        cin >> c >> t;
        if (c=='+') Add(t);
        else if (c=='-') Del(t);
        else Search(t);
    }
    return 0;
}


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

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

int L = 30;
class Trie {
public:
	struct Node {
		Node* arr[2];
		int cnt = 0;

		Node() {
			arr[0] = arr[1] = NULL;
			cnt = 0;
		}
	};

	Node *root;
	Trie() {root = new Node(); }

	void insert (int x) {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (!curNode->arr[f])
				curNode->arr[f] = new Node();
			curNode = curNode->arr[f];
			curNode->cnt++;
		}
	}

	void remove(int x)  {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			curNode = curNode->arr[f];
			curNode->cnt--;
		}
	}

	bool search(int x)  {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (!curNode->arr[f])
				return false;
			curNode = curNode->arr[f];
		}
		return (curNode != NULL);
	}

	int getBest(int x) {
		Node *curNode = root;
		int ans = 0;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (curNode -> arr[!f] && curNode -> arr[!f] -> cnt > 0) {
				ans += (1ll << i);
				curNode = curNode->arr[!f];
			} else {
				curNode = curNode->arr[f];
			}

		}
		return ans;
	}

};

void solve() {

	Trie trie;
	trie.insert(0);
	int q;
	cin >> q;
	while (q--) {
		char c;
		cin >> c;
		int x;
		cin >> x;
		if (c == '+') {
			trie.insert(x);
		} else if (c == '-') {
			trie.remove(x);
		} else {
			cout << trie.getBest(x) << endl;
		}
	}

}

int32_t main()
{
	IOS
	// int T; cin >> T; while (T--)
	solve();
	return 0;
}


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

typedef long long int ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

/*
    Found in comment section:

     First of all we have l = 0, r = maximal value of unsigned int.
    y = bitwise negation of x — best-case scenario for xor, our reference point.
    We look over bits of y from 31th to 0th.
    If pth bit is set (y & (1<<p)), we'd like to set that bit in our answer.
    We can use lowerbound to find out if set contains at least one number
    in range [l+(1<<p), r]. It can't be less than l+1(<<p) or greater than r,
    because we have to satisfy assumptions about more significant bits
    we've made before (l, r), and also we want pth bit to be set (1<<p).
    If we satisfy that condiotion, then [l, r] becomes [l+(1<<p), r],
    otherwise it becomes [l, r-(1<<p)].
    If pth bit of y is not set, we don't want to set it in our answer.
    We use lowerbound to check if set contains some elements in
    range [l, r-(1<<p)]. If it does, we set r to r-(1<<p),
    otherwise l becomes l+(1<<p).
    After loop we have l = r, and we output this number xor x.
*/

void solve() {

    int n;
    cin >> n;
    multiset<int> s;
    s.insert(0);
    for (int i = 0; i < n; i++) {
        char c;
        int x;
        cin >> c >> x;
        if (c == '+') {
            s.insert(x);
        } else if (c == '-') {
            s.erase(x);
        }
        else {

            int y = ~x;
            int l = 0, r = UINT_MAX;

            // k -> bit pos
            for (int k = 31; k >= 0; k--) {

                if (y & (1 << k)) {
                    auto it = s.lower_bound(l + (1 << k));
                    if (it != s.end() and (*it) <= r) {
                        l = l + (1 << k);
                    } else {
                        r = r - (1 << k);
                    }
                } else {
                    auto it = s.lower_bound(l);
                    if (it != s.end() and (*it) < l + (1 << k)) {
                        r  = r - (1 << k);
                    } else {
                        l = l + (1 << k);
                    }
                }
            }
            cout << (l ^ x) << endl;
        }
    }

}

int32_t main()
{
    IOS
    // int T; cin >> T; while (T--)
    solve();
    return 0;
}


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

typedef long long int ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

/*
    Found in comment section:

     First of all we have l = 0, r = maximal value of unsigned int.
    y = bitwise negation of x — best-case scenario for xor, our reference point.
    We look over bits of y from 31th to 0th.
    If pth bit is set (y & (1<<p)), we'd like to set that bit in our answer.
    We can use lowerbound to find out if set contains at least one number
    in range [l+(1<<p), r]. It can't be less than l+1(<<p) or greater than r,
    because we have to satisfy assumptions about more significant bits
    we've made before (l, r), and also we want pth bit to be set (1<<p).
    If we satisfy that condiotion, then [l, r] becomes [l+(1<<p), r],
    otherwise it becomes [l, r-(1<<p)].
    If pth bit of y is not set, we don't want to set it in our answer.
    We use lowerbound to check if set contains some elements in
    range [l, r-(1<<p)]. If it does, we set r to r-(1<<p),
    otherwise l becomes l+(1<<p).
    After loop we have l = r, and we output this number xor x.
*/

void solve() {

    int n;
    cin >> n;
    multiset<int> s;
    s.insert(0);
    for (int i = 0; i < n; i++) {
        char c;
        int x;
        cin >> c >> x;
        if (c == '+') {
            s.insert(x);
        } else if (c == '-') {

                s.erase(s.find(x));

        }
        else {

            int y = ~x;
            int l = 0, r = UINT_MAX;

            // k -> bit pos
            for (int k = 31; k >= 0; k--) {

                if (y & (1 << k)) {
                    auto it = s.lower_bound(l + (1 << k));
                    if (it != s.end() and (*it) <= r) {
                        l = l + (1 << k);
                    } else {
                        r = r - (1 << k);
                    }
                } else {
                    auto it = s.lower_bound(l);
                    if (it != s.end() and (*it) < l + (1 << k)) {
                        r  = r - (1 << k);
                    } else {
                        l = l + (1 << k);
                    }
                }
            }
            cout << (l ^ x) << endl;
        }
    }

}

int32_t main()
{
    IOS
    // int T; cin >> T; while (T--)
    solve();
    return 0;
}


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

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

int L = 30;
class Trie {
public:
	struct Node {
		Node* arr[2];
		int cnt = 0;

		Node() {
			arr[0] = arr[1] = NULL;
			cnt = 0;
		}
	};

	Node *root;
	Trie() {root = new Node(); }

	void insert (int x) {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (!curNode->arr[f])
				curNode->arr[f] = new Node();
			curNode = curNode->arr[f];
			curNode->cnt++;
		}
	}

	void remove(int x)  {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			curNode = curNode->arr[f];
			curNode->cnt--;
		}
	}

	// bool search(const ll& x) const {
	// 	Node *curNode = root;
	// 	for (int i = L; i >= 0; i--) {
	// 		int f = x & (1ll << i);
	// 		if (!curNode->arr[f])
	// 			return false;
	// 		curNode = curNode->arr[f];
	// 	}
	// 	return (curNode != nullptr);
	// }

	int getBest(int x) {
		Node *curNode = root;
		int ans = 0;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (curNode -> arr[!f] != NULL && curNode -> arr[!f] -> cnt > 0) {
				ans += (1ll << i);
				curNode = curNode->arr[!f];
			} else {
				curNode = curNode->arr[f];
			}

		}
		return ans;
	}

};

void solve() {

	Trie trie;
	int q;
	cin >> q;
	while (q--) {
		char c;
		cin >> c;
		int x;
		cin >> x;
		if (c == '+') {
			trie.insert(x);
		} else if (c == '-') {
			trie.remove(x);
		} else {
			cout << trie.getBest(x) << endl;
		}
	}

}

int32_t main()
{
	IOS
	// int T; cin >> T; while (T--)
	solve();
	return 0;
}


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

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

int L = 30;
class Trie {
public:
	struct Node {
		Node* arr[2];
		int cnt = 0;

		Node() {
			arr[0] = arr[1] = NULL;
			cnt = 0;
		}
	};

	Node *root;
	Trie() {root = new Node(); }

	void insert (int x) {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (!curNode->arr[f])
				curNode->arr[f] = new Node();
			curNode = curNode->arr[f];
			curNode->cnt++;
		}
	}

	void remove(int x)  {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			curNode = curNode->arr[f];
			curNode->cnt--;
		}
	}

	bool search(const ll& x) const {
		Node *curNode = root;
		for (int i = L; i >= 0; i--) {
			int f = x & (1ll << i);
			if (!curNode->arr[f])
				return false;
			curNode = curNode->arr[f];
		}
		return (curNode != nullptr);
	}

	int getBest(int x) {
		Node *curNode = root;
		int ans = 0;
		for (int i = L; i >= 0; i--) {
			int f = (x >> i) & 1;
			if (curNode->arr[f ^ 1] && curNode->arr[f ^ 1]->cnt > 0) {
				ans += (1ll << i);
				curNode = curNode->arr[f ^ 1];
			} else {
				curNode = curNode->arr[f];
			}

		}
		return ans;
	}

};

void solve() {

	Trie trie;
	int q;
	cin >> q;
	while (q--) {
		char c;
		cin >> c;
		ll x;
		cin >> x;
		if (c == '+') {
			trie.insert(x);
		} else if (c == '-') {
			trie.remove(x);
		} else {
			cout << trie.getBest(x) << endl;
		}
	}

}

int32_t main()
{
	IOS
	// int T; cin >> T; while (T--)
	solve();
	return 0;
}


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

using ll = long long;
#define endl '\n'

class Trie {
public:
  Trie* next[2] = {};
  ll cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (ll i = 31; i >= 0; --i) {
      ll b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  void Search(ll x, ll i, Trie* cur, ll& ans) {
    if (!cur) return;
    if (i == -1 && cur->cnt >= 1) {
      if (ans == -1) {
        ans = x ^ cur->val;
      } else {
        ans = max(ans, x ^ cur->val);
      }
      return;
    }
    if (i == -1) return;
    ll b = x >> i & 1;
    Search(x, i-1, cur->next[1-b], ans);
    Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  for (ll i = 0; i < n; ++i) {
    char c;
    ll x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = -1;
      trie.Search(x, 31, &trie, ans);

      if (ans == -1) ans = 0;
      cout << ans << endl;
    }
  }

}


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

using ll = long long;
#define endl '\n'

class Trie {
public:
  Trie* next[2] = {};
  ll cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (ll i = 31; i >= 0; --i) {
      ll b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  bool Search(ll x, ll i, Trie* cur, ll& ans) {
    if (!cur) return false;
    if (i == -1 && cur->cnt >= 1) {
      ans = cur->val;
      return true;
    }
    if (i == -1) return false;
    ll b = x >> i & 1;

    if (cur->next[1-b]) {
      if (Search(x, i-1, cur->next[1-b], ans)) return true;
    }
    return Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  for (ll i = 0; i < n; ++i) {
    char c;
    ll x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = 0;
      trie.Search(x, 31, &trie, ans);
      cout << (ans^x) << endl;
    }
  }

}


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

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char c) {
  return string(1,c);
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N)-1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) if(0){}
#endif

class Trie {
public:
  Trie* next[2] = {};
  int cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (int i = 31; i >= 0; --i) {
      int b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  bool Search(ll x, int i, Trie* cur, ll& ans) {
    if (i == -1 && cur->cnt >= 1) {
      ans = cur->val;
      return true;
    }
    if (!cur) return false;
    int b = x >> i & 1;

    if (cur->next[1-b]) {
      if (Search(x, i-1, cur->next[1-b], ans)) return true;
    }
    return Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  trie.Insert(0, 1);
  for (int i = 0; i < n; ++i) {
    char c;
    int x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = 0;
      trie.Search(x, 31, &trie, ans);
      cout << (ans^x) << endl;
    }
  }

}


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

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char c) {
  return string(1,c);
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N)-1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) if(0){}
#endif

class Trie {
public:
  Trie* next[2] = {};
  int cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (int i = 31; i >= 0; --i) {
      int b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  bool Search(ll x, int i, Trie* cur, ll& ans) {
    if (i == -1 && cur->cnt >= 1) {
      ans = cur->val;
      return true;
    }
    if (!cur) return false;
    int b = x >> i & 1;

    if (cur->next[1-b]) {
      if (Search(x, i-1, cur->next[1-b], ans)) return true;
    }
    return Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  for (int i = 0; i < n; ++i) {
    char c;
    int x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = 0;
      trie.Search(x, 31, &trie, ans);
      cout << (ans^x) << endl;
    }
  }

}


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

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char c) {
  return string(1,c);
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N)-1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) if(0){}
#endif

class Trie {
public:
  Trie* next[2] = {};
  ll cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (ll i = 31; i >= 0; --i) {
      ll b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  bool Search(ll x, ll i, Trie* cur, ll& ans) {
    if (!cur) return false;
    if (i == -1 && cur->cnt >= 1) {
      ans = cur->val;
      return true;
    }
    if (i==-1) return false;
    ll b = x >> i & 1;

    if (cur->next[1-b]) {
      if (Search(x, i-1, cur->next[1-b], ans)) return true;
    }
    return Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  trie.Insert(0, 1);
  for (ll i = 0; i < n; ++i) {
    char c;
    ll x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = 0;
      trie.Search(x, 31, &trie, ans);
      cout << (ans^x) << endl;
    }
  }

}


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

using ll = long long;
#define endl '\n'

class Trie {
public:
  Trie* next[2] = {};
  ll cnt = 0;
  ll val = 0;
  void Insert(ll x, ll dx) {
//    dbg(x,dx);
    Trie* cur = this;
    for (ll i = 31; i >= 0; --i) {
      ll b = x >> i & 1;
      if (!cur->next[b]) cur->next[b] = new Trie();
      cur = cur->next[b];
    }
    cur->cnt += dx;
    cur->val = x;
  }

  bool Search(ll x, ll i, Trie* cur, ll& ans) {
    if (i == -1 && cur->cnt >= 1) {
      ans = cur->val;
      return true;
    }
    if (!cur) return false;
    ll b = x >> i & 1;

    if (cur->next[1-b]) {
      if (Search(x, i-1, cur->next[1-b], ans)) return true;
    }
    return Search(x, i-1, cur->next[b], ans);
  }
};

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

  ll n;
  cin >> n;
  Trie trie;
  for (ll i = 0; i < n; ++i) {
    char c;
    ll x;
    cin >> c >> x;
    if (c == '+') {
      trie.Insert(x, 1);
    } else if (c == '-') {
      trie.Insert(x, -1);
    } else {
      ll ans = 0;
      trie.Search(x, 31, &trie, ans);
      cout << (ans^x) << endl;
    }
  }

}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[200001][2];
int   vs[200001];
int  ise[200001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<solve(bit(a)).to_ulong()<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[20000001][2];
int   vs[20000001];
int  ise[20000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g^s;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<solve(bit(a)).to_ulong()<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[2000001][2];
int   vs[2000001];
int  ise[2000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<solve(bit(a)).to_ulong()<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[2000001][2];
int   vs[2000001];
int  ise[2000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g^s;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<solve(bit(a)).to_ulong()<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[20000001][2];
int   vs[20000001];
int  ise[20000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<solve(bit(a)).to_ulong()<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[4000001][2];
int   vs[4000001];
int  ise[4000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g^s;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<max(a,(int)solve(bit(a)).to_ulong())<<'\n';
    }
}


#include<bits/stdc++.h>
#define int             long long
#define fi              first
#define se              second
#define pb              push_back
#define F(i,a,b)        for(int i=a;i<=b;i++)
#define D(i,a,b)        for(int i=a;i>=b;i--)
using namespace         std;
typedef pair<int,int>   ii;
typedef bitset<32>      bit;
const int inf=          1610612741;
int   n,l;
int   np[2000001][2];
int   vs[2000001];
int  ise[2000001];

int cnt=0;
void insert(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        int c=s[i];
        if(!vs[np[v][c]]){np[v][c]=++cnt;vs[cnt]=1;}
        ise[v]++;v=np[v][c];
    }
    ise[v]++;
}
bool find(const bit& s)
{
    int v=0;
    D(i,31,0)
    {
        if(!vs[np[v][s[i]]])return 0;
        v=np[v][s[i]];
    }
    return ise[v]>0;
}
void erase(const bit& s)
{
    if(!find(s))return;
    int v=0;
    D(i,31,0)
    {
        ise[v]--;
        v=np[v][s[i]];
    }
    ise[v]--;
}
bit solve(const bit& s)
{
    bit g=0;
    int v=0;
    D(i,31,0)
    {
        if(vs[np[v][!s[i]]] && ise[np[v][!s[i]]] ){g[i]=!s[i];v=np[v][!s[i]];}
        else if(vs[np[v][s[i]]] && ise[np[v][s[i]]] ){g[i]=s[i];v=np[v][s[i]];}
        else return g^s;
    }
    return g^s;
}

int32_t main()
{
	ios::sync_with_stdio(0);
    #ifdef _TPR_
        freopen("t.inp","r",stdin);
        freopen("t.out","w",stdout);
    #endif
    cin>>n;
    while(n--)
    {
        char k;int a;
        cin>>k>>a;
        if(k=='+')insert(bit(a));
        if(k=='-')erase(bit(a));
        if(k=='?')cout<<max(a,(int)solve(bit(a)).to_ulong())<<'\n';
    }
}


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

const int maxn = 6400020, alp = 2; // suma de caracteres, alp
int t[maxn][alp], term[maxn], vis[maxn], nodos = 1;
int getid(char c){
	return c - '0';
}
void add(string s){
	int act = 0;
	for(char c: s){
		int id = getid(c);
		if(t[act][id] == 0) t[act][id] = nodos++;
		vis[act]++;
		act = t[act][id];
	}
	vis[act]++;
	term[act]++;
}
int max_xor(string s){
	int ans = 0, pot = 31, act = 0;

	for(char c: s){
		int id = not getid(c);
		if(t[act][id] == 0){
			id = !id;
			if(t[act][id] == 0) break;
		}
		if(id == 1) ans|=(1 << pot);
		pot--;
		act = t[act][id];
	}
	return ans;
}
void remove_oc(string s){
	int act = 0, id, parent = -1;
	for(int i = 0; i < s.size(); ++i){
		id = getid(s[i]);
		vis[act]--;
		if(vis[act] == 0 and parent != -1) t[parent][getid(s[i - 1])] = 0;
		parent = act;
		act = t[act][id];
	}
	vis[act]--;
	if(vis[act] == 0 and parent != -1) t[parent][id] = 0;
	term[act]--;
}
void init(){
	memset(t,0,sizeof(t));
	memset(term,0,sizeof(term));
	memset(vis,0,sizeof(vis));
}
string binary(int n){
	string s = "";
	while(n){
		s+=(n%2 + '0');
		n/=2;
	}
	while(s.size() < 32) s+='0';
	reverse(s.begin(), s.end());
	return s;
}
int main(){
	init();
	int q, n;
	char op;
	cin >> q;
	add(binary(0));
	while(q--){
		cin >> op >> n;
		string s = binary(n);
		if(op == '+') add(s);
		else if(op == '-') remove_oc(s);
		else cout << (max_xor(s)^n) << endl;
	}
	return 0;
}


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

const int maxn = 2e6 + 10, alp = 5; // suma de caracteres, alp
int t[maxn][alp], term[maxn], vis[maxn], nodos = 1;
int getid(char c){
	return c - '0';
}
void add(string s){
	int act = 0;
	for(char c: s){
		int id = getid(c);
		if(t[act][id] == 0) t[act][id] = nodos++;
		vis[act]++;
		//cout << act << ' ' << vis[act] << endl;
		act = t[act][id];
	}
	vis[act]++;
	//cout << act << ' ' << vis[act] << endl;
	term[act]++;
}
int max_xor(string s){
	int ans = 0, pot = 31, act = 0;

	for(char c: s){
		int id = not getid(c);
		if(t[act][id] == 0){
			id = !id;
			if(t[act][id] == 0) break;
		}
		if(id == 1) ans|=(1 << pot);
		pot--;
		act = t[act][id];
	}
	return ans;
}
void remove_oc(string s){
	int act = 0, id, parent = -1;
	for(int i = 0; i < s.size(); ++i){
		id = getid(s[i]);
		vis[act]--;
		if(vis[act] == 0 and parent != -1) t[parent][getid(s[i - 1])] = 0;
		parent = act;
		act = t[act][id];
	}
	vis[act]--;
	if(vis[act] == 0 and parent != -1) t[parent][id] = 0;
	term[act]--;
}
void init(){
	memset(t,0,sizeof(t));
	memset(term,0,sizeof(term));
	memset(vis,0,sizeof(vis));
}
string binary(int n){
	string s = "";
	while(n){
		s+=(n%2 + '0');
		n/=2;
	}
	while(s.size() < 32) s+='0';
	reverse(s.begin(), s.end());
	return s;
}
int main(){
	init();
	int q, n;
	char op;
	cin >> q;
	add(binary(0));
	while(q--){
		cin >> op >> n;
		string s = binary(n);
		//cout << s << endl;
		if(op == '+') add(s);
		else if(op == '-') remove_oc(s);
		else cout << (max_xor(s)^n) << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int n;
int a[N];
int maxIn[N];
int mx, cnt;
pair<int, int> k[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }

    for(int i=0; i<n; i++){
        mx = max(mx, a[i]);
        maxIn[i] = mx;
        k[maxIn[i]].first++;
        k[maxIn[i]].second = maxIn[i];
    }
    //cout<<mx;
    sort(k+1, k+mx+1);
    cout<<k[mx].second;
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n-1; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            mx = max(mx, a[i]);

            if(a[i+1] > mx) cnt[illum].first++;
            //cout<<a[i+1]<<" "<<mx<<"    ";
        }
        //cout<<endl;
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;//*/
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    ios_base::sync_with_stdio(false);
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            mx = max(mx, a[i]);

            if(a[i+1] > mx && i+1!=illum) cnt[illum].first++;
            //cout<<a[i+1]<<" "<<mx<<"    ";
        }
        //cout<<endl;
        //cout<<cnt[illum].first<<" "<<a[cnt[illum].second]<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;//*/
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n-1; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            mx = max(mx, a[i]);

            if(a[i+1] > mx) cnt[illum].first++;
            //cout<<a[i+1]<<" "<<mx<<"    ";
        }
        //cout<<endl;
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;//*/
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int n;
int a[N];
int maxIn[N];
int mx, mxIndx;
int mn = INT_MAX;
pair<int, int> k[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }

    for(int i=0; i<n; i++){
        if(mx < a[i]){
            mx = a[i];
            mxIndx = i;
        }
        maxIn[i] = mx;
        k[maxIn[i]].first++;
        k[maxIn[i]].second = maxIn[i];
    }
    //cout<<mx;

    if(mxIndx == 0) cout<<mx;
    else if(mxIndx>0 && mxIndx<n-1){
        for(int i=mxIndx; i<n; i++)
            mn = min(mn, a[i]);
        cout<<mn;
    }
    else {
        sort(k+1, k+mx+1);
        cout<<k[mx].second;
    }
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int n;
int a[N];
int dp[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }

    for(int i=0; i<n-1; i++)
        if(a[i]<a[i+1])dp[i+1] = dp[i]+1;
    for(int i=1; i<n; i++)
    {
        if(dp[i]==0){cout<<a[i-1]; return 0;}
    }
    cout<<a[0];
}


#include <bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt","w", stdout);
}

int n, a;
int mx, prevMax;

int main()
{
    //TxtRead();
    cin>>n;
    vector<int> v(n+1);

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

        if(mx>a && prevMax<a)
        {
            v[mx]++;
            prevMax = a;
        }
        if(mx<a)
        {
            prevMax = mx;
            mx = a;
            v[a]--;
        }
    }

    int ans = 1;
    for(int i=1; i<=n; i++)
    {
        //cout<<v[i]<<" ";
        if(v[i] > v[ans])
            ans = i;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            mx = max(mx, a[i]);

            if(a[i+1] > mx) cnt[illum].first++;
            //cout<<a[i+1]<<" "<<mx<<"    ";
        }
        //cout<<endl;
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;//*/
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            if(mx < a[i])
                mx = a[i];

            if(a[i+1] > mx && i!=0) cnt[illum].first++;
            //cout<<mx<<" ";
        }
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].second<<" ";
        ans = min(ans, cnt[i].second);
    }
    cout<<a[ans];
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            mx = max(mx, a[i]);

            if(a[i+1] > mx && i+1!=illum) cnt[illum].first++;
            //cout<<a[i+1]<<" "<<mx<<"    ";
        }
        //cout<<endl;
        //cout<<cnt[illum].first<<" "<<a[cnt[illum].second]<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;//*/
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int n;
int a[N];
int dp[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }

    for(int i=0; i<n-1; i++){
        if(a[i]<a[i+1])dp[i+1] = dp[i]+1;
        else dp[i+1] = 0;
    }
    for(int i=1; i<n; i++)
    {
        if(dp[i]==0){cout<<a[i-1]; return 0;}
    }
    cout<<a[0];
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            if(mx < a[i])
                mx = a[i];

            if(a[i+1] > mx && i!=0) cnt[illum].first++;
            //cout<<mx<<" ";
        }
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int n;
int a[N];
int maxIn[N];
int mx, mxIndx;
int mn = INT_MAX, mnDp = INT_MAX, mnIndx;
int dp[N];
pair<int, int> k[N];

int main()
{
    //TxtRead();
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    mx = a[0];
    for(int i=0; i<n; i++){
        if(mx < a[i]){
            mx = a[i];
            mxIndx = i;
        }
        if(a[i]>maxIn[i-1] && i!=0) dp[i] = dp[i-1]+1;
        else dp[i] = 0;

        maxIn[i] = mx;
        k[maxIn[i]].first++;
        k[maxIn[i]].second = maxIn[i];
    }

    for(int i=0; i<n; i++)
    {
        mn = min(mn, a[i]);
        if(dp[i]) break;
        if(i==n-1)
        {
            cout<<mn;
            return 0;
        }
    }
    mn = 0;

    if(mxIndx == 0) cout<<mx;

    else if(mxIndx == n-1){
        for(int i=1; i<n; i++)
            if(mn > a[i]){
                if(mnDp > dp[i]){
                    mnDp = dp[i];
                    mn = a[i];
                }
            }
        cout<<mn;
    }

    else if(mxIndx>0 && mxIndx<n-1){
        for(int i=1; i<n; i++)
            //cout<<dp[i]<<" ";
            if(!dp[i])mn = min(mn, a[i]);
        cout<<mn;
    }

    else {
        sort(k+1, k+mx+1);
        cout<<k[mx].second;
    }
}


#include<bits/stdc++.h>
using namespace std;
void TxtRead()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt", "w", stdout);//*/
}

const int N = 1e5+5;

int a[N];
int dp[N];
int ans = INT_MAX;
int n, mx, maxIn[N];
pair<int, int> cnt[N];

int main()
{
    TxtRead();
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];

    for(int illum=0; illum<n; illum++)
    {
        mx = 0;
        cnt[illum].second = illum;
        for(int i=0; i<n-1; i++)
        {
            if(i == illum) continue;

            if(mx < a[i])
                mx = a[i];

            if(a[i+1] > mx && i!=0) cnt[illum].first++;
            //cout<<mx<<" ";
        }
        //cout<<cnt[illum].first<<" "<<cnt[illum].second<<endl;
    }
    sort(cnt, cnt+n);
    mx = cnt[n-1].first;
    for(int i=n-1; i>=0; i--)
    {
        if(cnt[i].first < mx) break;
        //cout<<cnt[i].first<<" "<<a[cnt[i].second]<<"\n";
        ans = min(ans, a[cnt[i].second]);
    }
    cout<<ans;
}


#include<iostream>

using namespace std;

const int INF = 0x3f3f3f3f;

int main() {
	int n, temp, key;
	cin >> n;
	int a[n], b[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		temp = 0;
		key = 0;
		for (int j = 0; j < i; j++) {
			if (a[j] > a[i]) {
				temp++;
				key = j;
			}
			if (temp > 1)
				break;
		}
		if (temp == 0)
			b[i]--;
		if (temp == 1)
			b[key]++;
	}
	int answer=0;
	temp = -INF;
	for (int i = 0; i < n; i++)
	{
		if (b[i] > temp) {
			temp = b[i];
			answer = a[i];
		}
		else if (b[i] == temp)
		{
			answer =a[i] < answer?a[i]:answer;
		}
	}
	cout << answer;
	return 0;
}


#include<iostream>

using namespace std;

int find(int n,int a[],int alen,int b[]) {
    //set result array
    int result[alen];
    for (int i = 0; i < n; ++i) {
        result[i] = b[i];
    }
    result[n] = -1;
    for (int i = n + 1, j = i + 1; j < alen; ++i, ++j) {
        if (a[i] < a[j])
            result[i] = 0;
        else
            result[i] = 1;
    }
    // set last one
    if(a[alen-1]>a[alen-2])
        result[alen-1]=0;
    else
        result[alen-1]=1;
    // calculate longest length of 0
    int m = 0, answer = 0;
    for (int k = 0; k < alen; ++k) {
        if (result[k] == 0)
            m++;
        else if (result[k] == 1) {
            if (m > answer)
                answer = m;
        }
    }
    if (m > answer)
        answer = m;
    return answer;
}

int main() {
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    b[0] = 0;
    for (int i = 0, j = 1; j < n; ++i, ++j) {
        if (a[i] < a[j])
            b[i] = 0;
        else
            b[i] = 1;
    }
    int temp = 0;
    int result = 0;
    int answer = 0;
    for (int k = 0; k < n; ++k) {
        temp = find(k, a, n, b);
        cout<<temp<<"\n";
        if (temp>result||k==0) {
            result = temp;
            answer = a[k];
        }
    }
    cout << answer;
    return 0;
}


#include<iostream>

using namespace std;

int find(int n,int a[],int alen,int b[]) {
    //set result array
    int result[alen];
    for (int i = 0; i < n; ++i) {
        result[i] = b[i];
    }
    result[n] = -1;
    for (int i = n + 1, j = i + 1; j < alen; ++i, ++j) {
        if (a[i] < a[j])
            result[i] = 0;
        else
            result[i] = 1;
    }
    // set last one
    if(a[alen-1]>a[alen-2])
        result[alen-1]=0;
    else
        result[alen-1]=1;
    // calculate longest length of 0
    int m = 0, answer = 0;
    for (int k = 0; k < alen; ++k) {
        if (result[k] == 0)
            m++;
        else if (result[k] == 1) {
            if (m > answer)
                answer = m;
        }
    }
    if (m > answer)
        answer = m;
    return answer;
}

int main() {
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    // return if n<2
    if(n<2){
        cout<<a[0];
        return 0;
    }
    b[0] = 0;
    for (int i = 0, j = 1; j < n; ++i, ++j) {
        if (a[i] < a[j])
            b[i] = 0;
        else
            b[i] = 1;
    }
    int temp = 0;
    int result = 0;
    int answer = 0;
    for (int k = 0; k < n; ++k) {
        temp = find(k, a, n, b);
        if (temp>result||k==0) {
            result = temp;
            answer = a[k];
        }
    }
    cout << answer;
    return 0;
}


#include<iostream>

using namespace std;

const int INF = 0x3f3f3f3f;

int main() {
	int n=0, temp, key;
	cin >> n;
	int a[n], b[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		temp = 0;
		key = 0;
		for (int j = 0; j < n; j++) {
			if (a[j] > a[i]) {
				temp++;
				key = j;
			}
			if (temp > 1)
				break;
		}
		if (temp == 0)
			b[i]--;
		if (temp == 1)
			b[key]++;
	}
	int answer;
	temp = -INF;
	for (int i = 0; i < n; i++)
	{
		if (b[i] > temp) {
			temp = b[i];
			answer = a[i];
		}
		else if (b[i] == temp)
		{
			answer =  a[i] < answer?a[i]:answer;
		}
	}
	cout << answer;
	return 0;
}


#include<iostream>

using namespace std;

int find(int n,int a[],int alen,int b[]) {
    //set result array
    int result[alen];
    for (int i = 0; i < n; ++i) {
        result[i] = b[i];
    }
    result[n] = -1;
    for (int i = n + 1, j = i + 1; j < alen; ++i, ++j) {
        if (a[i] < a[j])
            result[i] = 0;
        else
            result[i] = 1;
    }
    // set last one
    if(a[alen-1]>a[alen-2]&&n!=alen-1)
        result[alen-1]=0;
    else
        result[alen-1]=1;
    // calculate longest length of 0
    int m = 0, answer = 0;
    for (int k = 0; k < alen; ++k) {
        if (result[k] == 0)
            m++;
        else if (result[k] == 1) {
            if (m > answer)
                answer = m;
        }
    }
    if (m > answer)
        answer = m;
    return answer;
}

int main() {
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    // return if n<2
    if(n<2){
        cout<<a[0];
        return 0;
    }
    b[0] = 0;
    for (int i = 0, j = 1; j < n; ++i, ++j) {
        if (a[i] < a[j])
            b[i] = 0;
        else
            b[i] = 1;
    }
    int temp = 0;
    int result = 0;
    int answer = 0;
    for (int k = 0; k < n; ++k) {
        temp = find(k, a, n, b);
        if (temp>result||k==0) {
            result = temp;
            answer = a[k];
        } else if(temp==result){
            if(a[k]<answer)
                answer=a[k];
        }
    }
    cout << answer;
    return 0;
}


#include<iostream>

using namespace std;
int main() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int temp = 0, store = 0, num = 2147483647;
	for (int i = 0; i < n; i++)
	{
		temp = 0;
		for (int j = i + 1; j < n; j++)
		{
			if (a[j] > a[i])
				temp++;
		}
		if (temp > store)
		{
			store = temp;
			num = a[i];
		}
		else if (temp == store) {
			if (a[i] < num)
				num = a[i];
		}
	}
	cout << num;
	return 0;
}


#include<iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int m[n];
    int answer = 10001;
    for (int i = 0; i < n; ++i)
        cin>>m[i];

    for (int i = 0; i < n; i++) {
        int a = 0, b = 1;
        for (;; a++, b++) {
            if (a == i)
                a++;
            if (b == i)
                b++;
            if (m[a] >= m[b]) {
                if (i < answer)
                    answer = i;
                break;
            }
        }
    }
    cout << m[answer];
    return 0;
}


#include<iostream>

using namespace std;

const int INF = 0x3f3f3f3f;

int main() {
	int n, temp, key;
	cin >> n;
	int a[n], b[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		temp = 0;
		key = 0;
		for (int j = 0; j < i; j++) {
			if (a[j] > a[i]) {
				temp++;
				key = j;
			}
			if (temp > 1)
				break;
		}
		if (temp == 0)
			b[i]--;
		if (temp == 1)
			b[key]++;
	}
	int answer=0;
	temp = -INF;
	for (int i = 0; i < n; i++)
	{
		if (b[i] > temp) {
			temp = b[i];
			answer = a[i];
		}
		else if (b[i] == temp)
		{
			answer =a[i] < answer?a[i]:answer;
		}
	}
    for (int i = 0; i < n; ++i) {
        cout<<b[i];
    }
	cout << answer;
	return 0;
}


#include<iostream>

using namespace std;

int find(int n,int a[],int alen,int b[]) {
    //set result array
    int result[alen];
    for (int i = 0; i < n; ++i) {
        result[i] = b[i];
    }
    result[n] = -1;
    for (int i = n + 1, j = i + 1; j < alen; ++i, ++j) {
        if (a[i] < a[j])
            result[i] = 0;
        else
            result[i] = 1;
    }
    // set last one
    if(a[alen-1]>a[alen-2])
        result[alen-1]=0;
    else
        result[alen-1]=1;
    // calculate longest length of 0
    int m = 0, answer = 0;
    for (int k = 0; k < alen; ++k) {
        if (result[k] == 0)
            m++;
        else if (result[k] == 1) {
            if (m > answer)
                answer = m;
        }
    }
    if (m > answer)
        answer = m;
    return answer;
}

int main() {
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    // return if n<2
    if(n<2){
        cout<<a[0];
        return 0;
    }
    b[0] = 0;
    for (int i = 0, j = 1; j < n; ++i, ++j) {
        if (a[i] < a[j])
            b[i] = 0;
        else
            b[i] = 1;
    }
    int temp = 0;
    int result = 0;
    int answer = 0;
    for (int k = 0; k < n; ++k) {
        temp = find(k, a, n, b);
        if (temp>result||k==0) {
            result = temp;
            answer = a[k];
        } else if(temp==result){
            if(a[k]<answer)
                answer=a[k];
        }
    }
    cout << answer;
    return 0;
}


#include<iostream>

using namespace std;

int find(int n,int a[],int alen,int b[]) {
    //set result array
    int result[alen];
    for (int i = 0; i < n; ++i) {
        result[i] = b[i];
    }
    result[n] = -1;
    for (int i = n + 1, j = i + 1; j < alen; ++i, ++j) {
        if (a[i] < a[j])
            result[i] = 0;
        else
            result[i] = 1;
    }
    // set last one
    if(a[alen-1]>a[alen-2])
        result[alen-1]=0;
    else
        result[alen-1]=1;
    // calculate longest length of 0
    int m = 0, answer = 0;
    for (int k = 0; k < alen; ++k) {
        if (result[k] == 0)
            m++;
        else if (result[k] == 1) {
            if (m > answer)
                answer = m;
        }
    }
    if (m > answer)
        answer = m;
    return answer;
}

int main() {
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    // return if n<2
    if(n<2){
        cout<<a[0];
        return 0;
    }
    b[0] = 0;
    for (int i = 0, j = 1; j < n; ++i, ++j) {
        if (a[i] < a[j])
            b[i] = 0;
        else
            b[i] = 1;
    }
    int temp = 0;
    int result = 0;
    int answer = 0;
    for (int k = 0; k < n; ++k) {
        temp = find(k, a, n, b);
        cout<<temp<<"\n";
        if (temp>result||k==0) {
            result = temp;
            answer = a[k];
        }
    }
    cout << answer;
    return 0;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v2.push_back(i);
        }
        else{
            v2.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=1,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,v.size()-1,i-1,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(1000000);

        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=1,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,v.size()-1,i-1,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>=max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=0,ans=0;
    for(i=0;i<v2.size();i++)
    {
       // cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
   // cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }
      //  cout<<g<<endl;
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int pp=lower_bound(v.begin(),v.end(),i)-v.begin();
        int g=query(0,0,v.size()-1,pp,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
*/


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,v.size()-1,i-1,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/