#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Woverflow"
using namespace std;
const short INF16 = ~(1 << 15);
const int INF32 = 1e9;
const int SZ = 2e6 + 11;
const int SZ2 = 200;
const long long mod = 998244353;
const long long INF64 = 8e18;
const long long P = 53;
const long double eps = 1e-10;
const long double PI = acosl(-1);
inline long long MOD(long long x) {
  x %= mod, x += mod;
  return x % mod;
}
inline long long MOD(long long x, long long m) {
  x %= m, x += m;
  return x % m;
}
inline long long T(long long x) { return x * (x + 1) >> 1; }
long long fpow(long long a, long long b, long long mod = 8e18) {
  if (!b) return 1;
  if (b == 1) return a % mod;
  if (b & 1) return fpow(a, b ^ 1, mod) * a % mod;
  long long d = fpow(a, b >> 1, mod);
  return d * d % mod;
}
void Solve(int num_test) {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int &i : v) cin >> i;
  vector<int> ans;
  if (n == 1) {
    cout << -1 << endl;
    return;
  }
  for (int i = 1; i < n; i++) {
    int k = (v[0] ^ v[i]);
    set<int> st;
    bool flag = 1;
    for (int j : v) st.insert(j);
    while (st.size()) {
      int a = *st.begin();
      int b = (a ^ k);
      if (st.count(b) == 0) {
        flag = 0;
        break;
      }
      st.erase(a);
      st.erase(b);
    }
    if (flag) ans.push_back(k);
  }
  if (ans.size() == 0)
    cout << -1 << endl;
  else {
    sort(ans.begin(), ans.end());
    cout << ans[0] << endl;
  }
}
signed main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);
  srand(time(NULL));
  int test = 1;
  cin >> test;
  for (int i = 1; i <= test; i++) Solve(i);
}
