#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
const int N = 1e3 + 100;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
inline long long dpow(long long a, long long b) {
  long long r = 1, t = a;
  while (b) {
    if (b & 1) r = (r * t) % Mod;
    b >>= 1;
    t = (t * t) % Mod;
  }
  return r;
}
inline long long fpow(long long a, long long b) {
  long long r = 1, t = a;
  while (b) {
    if (b & 1) r = (r * t);
    b >>= 1;
    t = (t * t);
  }
  return r;
}
int a[N];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    scanf("%d", &n);
    set<int> s1;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s1.insert(a[i]);
    int ok = 0;
    for (int i = 1; i <= 100000; i++) {
      bool flag = true;
      for (int j = 1; j <= n; j++) {
        int x = i ^ a[j];
        if (s1.find(x) == s1.end()) {
          flag = false;
          break;
        }
      }
      if (flag) {
        ok = i;
        break;
      }
    }
    if (!ok)
      printf("-1\n");
    else
      printf("%d\n", ok);
  }
  return 0;
}
