CF1763A.Absolute Maximization

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Absolute Maximization

You are given an array aa of length nn. You can perform the following operation several (possibly, zero) times:

  • Choose ii, jj, bb: Swap the bb-th digit in the binary representation of aia_i and aja_j.

Find the maximum possible value of max(a)min(a)\max(a) - \min(a).

In a binary representation, bits are numbered from right (least significant) to left (most significant). Consider that there are an infinite number of leading zero bits at the beginning of any binary representation.

For example, swap the 00-th bit for 4=10024=100_2 and 3=1123=11_2 will result 1012=5101_2=5 and 102=210_2=2. Swap the 22-nd bit for 4=10024=100_2 and 3=1123=11_2 will result 0002=02=0000_2=0_2=0 and 1112=7111_2=7.

Here, max(a)\max(a) denotes the maximum element of array aa and min(a)\min(a) denotes the minimum element of array aa.

The binary representation of xx is xx written in base 22. For example, 99 and 66 written in base 22 are 10011001 and 110110, respectively.

Input

The first line contains a single integer tt (1t1281 \le t \le 128) — the number of testcases.

The first line of each test case contains a single integer nn (3n5123 \le n \le 512) — the length of array aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<10240 \le a_i \lt 1024) — the elements of array aa.

It's guaranteed that the sum of nn over all testcases does not exceed 512512.

Output

For each testcase, print one integer — the maximum possible value of max(a)min(a)\max(a) - \min(a).

Note

In the first example, it can be shown that we do not need to perform any operations — the maximum value of max(a)min(a)\max(a) - \min(a) is 10=11 - 0 = 1.

In the second example, no operation can change the array — the maximum value of max(a)min(a)\max(a) - \min(a) is 55=05 - 5 = 0.

In the third example, initially a=[1,2,3,4,5]a = [1, 2, 3, 4, 5], we can perform one operation taking i=2i = 2, j=5j = 5, b=1b = 1. The array now becomes a=[1,0,3,4,7]a = [1, 0, 3, 4, 7]. It can be shown that any further operations do not lead to a better answer — therefore the answer is max(a)min(a)=70=7\max(a) - \min(a) = 7 - 0 = 7.

Samples

4
3
1 0 1
4
5 5 5 5
5
1 2 3 4 5
7
20 85 100 41 76 49 36
1
0
7
125

在线编程 IDE

建议全屏模式获得最佳体验