Under construction

Still being built. Things may change

XOR Necklace

Interview

Time limit3sMemory limit1024 MB

Summary
Given a necklace of N beads, remove some beads to maximize the sum of XORs between each pair of adjacent beads around the cycle.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Frograms, Inc. has been developing necklaces with a new design and feature. Every programmer at Frograms now wears one. A necklace is made of several beads strung on a thread. Each bead has an integer from 00 to 10910^9 (inclusive).

Most programmers left for summer vacation, so only a few remain in the office today. They decided to play a game with their necklaces to decide who buys lunch. For each pair of consecutive beads, take the sum of their numbers. A programmer's score is the XOR (⊕\oplus) of all those sums. The programmer with the lowest score loses.

If there are NN beads with values A_1A\_1, A_2A\_2, ⋯\cdots, A_NA\_N in order around the necklace, the score is:

(A_1⊕A_2)+(A_2⊕A_3)+⋯+(A_N−1⊕A_N)+(A_N⊕A_1)(A\_1 \oplus A\_2) + (A\_2 \oplus A\_3) + \dots + (A\_{N-1} \oplus A\_N) + (A\_N \oplus A\_1)

You will cheat by removing zero or more beads to maximize the score. What is the maximum score you can get by removing some of the beads? You cannot reorder the beads, and the necklace must keep at least two beads.

Input

The input consists of TT test cases. The first line contains TT.

Each test case starts with a line containing a single integer NN (2≤N≤5002 \le N \le 500), the number of beads. The next line contains NN integers A_1A\_1, A_2A\_2, ⋯\cdots, A_NA\_N, separated by single spaces.

Output

For each test case, print the maximum score you can get on a single line.

Examples1

  1. Example 1

    Input
    1
    6
    2 31 23 27 10 20
    
    Expected output
    118