Under construction

Still being built. Things may change

Xorcisten

Time limit2.5sMemory limit1024 MB

Summary
After each point update to a list, find the smallest X that makes a_i XOR X non-decreasing, or -1 if no such X exists.
Level

Medium6 of 10

Topics
Bit manipulation, Implementation
Solved
No attempts yet

Problem

Xorcisten Roxanne has a remarkable ability: she can sort lists very quickly. Given a list of integers a1,a2,…,aNa_1, a_2, \dots, a_N, she instantly finds an integer XX such that

a1⊕X≤a2⊕X≤⋯≤aN⊕Xa_1 \oplus X \leq a_2 \oplus X \leq \dots \leq a_N \oplus X

Here ⊕\oplus is bitwise XOR. Write both numbers in binary. Going from the least significant digit upward, write 0 if the two digits are equal and 1 otherwise. This is the same as adding the numbers in binary without carries.

All she has to do next is replace each aia_i with ai⊕xa_i \oplus x, and the list is sorted.

Several companies often hire Roxanne to sort their very long lists. One day, however, she discovers that her ability is gone. Your task is to write a program so that she can keep her job.

You are given a list of non-negative integers a1,…,aNa_1, \dots, a_N and QQ updates pi,vip_i, v_i, meaning that apia_{p_i} changes to viv_i. Let cic_i be the smallest non-negative integer XX such that the list a1⊕X,…,aN⊕Xa_1 \oplus X, \dots, a_N \oplus X is sorted after the first ii updates have been applied. Print c0,c1,…,cQc_0, c_1, \dots, c_Q. If no such XX exists, print −1-1 for that value.

Input

The first line contains an integer NN (1≤N≤1061 \leq N \leq 10^6), the number of integers in the list.

The second line contains NN integers a1,a2,…,aNa_1, a_2, \dots, a_N (0≤ai<2300 \leq a_i < 2^{30}).

The third line contains an integer QQ (0≤Q≤1060 \leq Q \leq 10^6), the number of updates.

Each of the next QQ lines contains two integers pip_i (1≤pi≤N1 \leq p_i \leq N) and viv_i (0≤vi<2300 \leq v_i < 2^{30}), meaning that apia_{p_i} changes to viv_i.

Output

Print Q+1Q+1 lines. Line ii (counting from 0) contains cic_i, the smallest possible xx that sorts the list after updates 1,2,…,i1, 2, \dots, i have been applied, or −1-1 if no such xx exists.

Constraints

  • N,Q≤106N, Q \leq 10^6
  • ai,vi<230a_i, v_i < 2^{30}

Examples1

  1. Example 1

    Input
    3
    0 1 4
    3
    2 7
    3 3
    1 4
    
    Expected output
    0
    2
    -1
    4