#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
map<ll, ll> mp1, mp;
vector<pair<ll, pair<ll, ll> > > vpp1;
vector<pair<ll, ll> > vpp, vp1;
vector<ll> v, v1, v2, fin;
map<pair<ll, ll>, bool> mbb;
map<ll, bool> mb;
ll a, b, c, n, d, e, f, ta, sum = 0, cnt = 0, m, l, req, sz, maxi, k;
bool sortbysec(const pair<pair<ll, ll>, ll> &a,
               const pair<pair<ll, ll>, ll> &b) {
  return (a.second < b.second);
}
void calc() {
  for (ll i = 0;; i++) {
    ll x = powl(2LL, i);
    mb[x] = 1;
    if (x >= 1e18) break;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  mb.clear();
  calc();
  cin >> ta;
  for (ll test = 0; test < ta; test++) {
    cin >> n;
    map<ll, bool> mbb;
    v.clear();
    ll ans = -1;
    mb.clear();
    mbb.clear();
    for (ll i = 0; i < n; i++) {
      cin >> a;
      v.push_back(a);
      mb[a] = true;
    }
    ll cnt = 0;
    sort(v.begin(), v.end());
    for (ll i = 1; i <= 3 * 1024; i++) {
      mbb.clear();
      ll bit = i;
      ll cnt = 0;
      for (ll j = 0; j < n; j++) {
        ll x = v[j];
        x = x ^ bit;
        if (mb[x] == true and mbb[x] == false) {
          mbb[x] = true;
          cnt++;
        }
      }
      if (cnt == n) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
