#include <bits/stdc++.h>
using namespace std;
bool check(int val, bool visible[], int arr[], int n, int& answer) {
  int barr[n];
  for (int j = 0; j < n; j++) {
    barr[j] = arr[j] ^ val;
    if (!visible[barr[j]]) {
      return false;
    }
  }
  int arr1[n];
  copy_n(arr, n, arr1);
  sort(arr1, arr1 + n);
  sort(barr, barr + n);
  for (int k = 0; k < n; k++) {
    if (arr1[k] != barr[k]) {
      return false;
    }
  }
  answer = val;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    bool visible[5001];
    memset(visible, false, sizeof(visible));
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      visible[arr[i]] = 1;
    }
    if (n % 2) {
      cout << -1 << "\n";
    } else {
      bool solved = false;
      for (int i = 1; i <= 4999; i++) {
        if (visible[i ^ arr[0]]) {
          int answer = 0;
          bool result = check(i, visible, arr, n, answer);
          if (result) {
            solved = true;
            cout << i << "\n";
            break;
          }
        } else {
          continue;
        }
      }
      if (!solved) {
        cout << -1 << "\n";
      }
    }
  }
  return 0;
}
