#include <bits/stdc++.h>
using namespace std;
int s[1025], a[1025], d[1025];
int main() {
  std::ios::sync_with_stdio(0);
  int t, n, x, i, j;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < 1025; i++) d[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> s[i];
      d[s[i]]++;
    }
    x = s[0];
    for (i = 1; i < n; i++) a[i] = (s[i] ^ x);
    sort(a, a + n);
    for (i = 1; i < n; i++) {
      if (x = a[i]) {
        for (j = 0; j < 1025; j++)
          if (d[j] && !d[(j ^ x)]) break;
        if (j == 1025) {
          cout << x << "\n";
          break;
        }
      }
    }
    if (i == n) cout << "-1\n";
  }
}
