## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## [Solution] AND Sorting Codeforces Solution | Codeforces Problem Solution 2022

B. AND Sorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation $p$ of integers from $0$ to $n-1$ (each of them occurs exactly once). Initially, the permutation is not sorted (that is, ${p}_{i}>{p}_{i+1}$ for at least one $1\le i\le n-1$).

The permutation is called $X$-sortable for some non-negative integer $X$ if it is possible to sort the permutation by performing the operation below some finite number of times:

• Choose two indices $i$ and $j$ $\left(1\le i such that ${p}_{i}\mathrm{&}{p}_{j}=X$.
• Swap ${p}_{i}$ and ${p}_{j}$.

Here $\mathrm{&}$ denotes the bitwise AND operation.

Input

The input consists of multiple test cases. The first line contains a single integer $t$ $\left(1\le t\le {10}^{4}\right)$  — the number of test cases. Description of test cases follows.

The first line of each test case contains a single integer $n$ $\left(2\le n\le 2\cdot {10}^{5}\right)$  — the length of the permutation.

The second line of each test case contains $n$ integers ${p}_{1},{p}_{2},...,{p}_{n}$ ($0\le {p}_{i}\le n-1$, all ${p}_{i}$ are distinct)  — the elements of $p$. It is guaranteed that $p$ is not sorted.

It is guaranteed that the sum of $n$ over all cases does not exceed $2\cdot {10}^{5}$.

Output

For each test case output a single integer — the maximum value of $X$ such that $p$ is $X$-sortable.