#include <bits/stdc++.h>
using namespace std;
int ans1 = 0, ans2 = 0;
int main(void) {
  int t, i, j;
  cin >> t;
  while (t--) {
    int n, mx = INT_MIN, ans = 0, sz;
    cin >> n;
    int a[n];
    set<int> s;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      mx = max(mx, a[i]);
      s.insert(a[i]);
    }
    for (i = 12; i >= 0; i--) {
      if (((1 << i) & mx) != 0) break;
    }
    mx = (1 << (i + 1));
    for (i = 1; i <= mx; i++) {
      map<int, int> mp;
      sz = 0;
      for (j = 0; j < n; j++) {
        if ((s.count(a[j] ^ i) > 0) && (mp[a[j] ^ i] == 0)) {
          sz++;
          mp[a[j] ^ i]++;
        }
      }
      if (sz == s.size()) ans = 1;
      if (ans == 1) break;
    }
    if (ans == 0)
      cout << -1 << endl;
    else
      cout << i << endl;
  }
}
