------
## 最终结论
```python
def find_minimal_k(t, test_cases):
    results = []
    for i in range(t):
        n, S = test_cases[i]
        found = False
        for k in range(1, 1024):  # Check all possible k from 1 to 1023
            transformed_set = {s ^ k for s in S}
            if transformed_set == set(S):
                results.append(k)
                found = True
                break
        if not found:
            results.append(-1)
    return results

# Read input
t = int(input().strip())
test_cases = []
for _ in range(t):
    n = int(input().strip())
    S = list(map(int, input().strip().split()))
    test_cases.append((n, S))

# Get results and print them
results = find_minimal_k(t, test_cases)
for result in results:
    print(result)
```