Under construction

Still being built. Things may change

Sorting Illusion

Time limit2sMemory limit512 MB

Summary
Find the smallest b making a[i] xor b nondecreasing, updating the answer after each of q point changes to the array.
Level

Hard8 of 10

Topics
Bit manipulation, Divide and conquer, Segment tree, Sorting
Solved
No attempts yet

Problem

A well-known programmer tries his hand at being a magician. His signature trick works as follows.

Given an array of nn non-negative integers a1,a2,…,ana_1, a_2, \ldots, a_n, he quickly finds a magic number bb. A non-negative integer bb is called magic for the array if applying the bitwise exclusive OR operation with this number to every element of the array turns the array into a sorted array. In other words, [ (a_1 \oplus b) \leq (a_2 \oplus b) \leq \ldots \leq (a_n \oplus b), ] where ⊕\oplus is the bitwise exclusive OR operation.

To make the trick more impressive, after presenting the magic number for the given array, the magician performs the following action qq times. He invites the audience to change one of the elements of the array and then tries to present a magic number again. The programmer has honed his magician's skill so well that each time he presents the audience with the smallest possible magic number. Sometimes the trick fails, because no magic number can be found for the resulting array.

Write a program that, for the given initial array and after each change of an element, computes the minimum magic number for the resulting array, or determines that no such number exists.

Input

The first line of the input contains an integer nn, the number of numbers in the array (1≤n≤1061 \leq n \leq 10^6).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the elements of the array (0≤ai<2300 \leq a_i < 2^{30}).

The third line contains an integer qq, the number of changes to an element of the array (0≤q≤1060 \leq q \leq 10^6).

The following qq lines each contain two integers pip_i and viv_i, where pip_i is the index of the array element to replace (1≤pi≤n1 \le p_i \le n) and viv_i is the new value of that element (0≤vi<2300 \le v_i < 2^{30}).

Output

The output must contain (q+1)(q + 1) integers b0,b1,…,bqb_0, b_1, \ldots, b_q, one per line.

The value b0b_0 is either the minimum possible magic number for the initial array or −1-1 if no such number exists.

For ii from 1 to qq, the value bib_i is either the minimum possible magic number for the array after the first ii changes or −1-1 if no such number exists.

Note

Exclusive OR is a logical operation denoted by ⊕\oplus, defined by the following truth table:

xxyyx⊕yx \oplus y
000
011
101
110

Let us define the bitwise exclusive OR of two non-negative integers xx and yy. Write each of the integers xx and yy in binary, padding the shorter number with leading zeros if necessary so that they have equal length. The bitwise exclusive OR of two integers xx and yy, also denoted x⊕yx \oplus y, is the non-negative integer whose binary representation has, in each digit, the exclusive OR of the corresponding digits of xx and yy. For example, 5⊕22=1012⊕101102=100112=195 \oplus 22 = 101_2 \oplus 10110_2 = 10011_2 = 19.

Among the programming languages offered at the olympiad, Pascal uses the <<xor>> operator to denote exclusive OR, and the remaining programming languages use the <<^>> operator.

Examples1

  1. Example 1

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