#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long INF = 8e18;
const int maxn = 5e5 + 5;
int a[maxn], b[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int ans = inf, n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for (int i = 2; i <= n; ++i) {
      int now = a[1] ^ a[i];
      for (int j = 1; j <= n; ++j) b[j] = a[j] ^ now;
      bool ok = 1;
      sort(b + 1, b + 1 + n);
      for (int j = 1; j <= n; ++j)
        if (b[j] != a[j]) ok = 0;
      if (ok) ans = min(ans, now);
    }
    if (ans == inf)
      cout << -1 << '\n';
    else
      cout << ans << '\n';
  }
  return 0;
}
