#include <bits/stdc++.h>
using namespace std;
long long fre[1050], f[1050], fr[2050], v[1050];
int main() {
  long long t, n, a, ok = 0, sol;
  cin >> t;
  while (t--) {
    sol = 5050;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> v[i];
      fr[v[i]]++;
    }
    for (int i = 1; i <= n; i++) {
    }
    for (int i = 2; i <= n; i++) {
      a = v[1] xor v[i];
      for (int j = 1; j <= n; j++) {
        f[j] = v[j] xor a;
        fre[f[j]]++;
      }
      for (int j = 1; j <= n; j++) {
      }
      for (int j = 1; j <= n; j++) {
        if (fr[v[j]] != fre[v[j]]) {
          ok = 1;
          break;
        }
      }
      for (int j = 1; j <= n; j++) {
        fre[f[j]] = 0;
      }
      if (ok == 0) {
        sol = min(sol, a);
      }
      ok = 0;
    }
    if (sol == 5050) {
      sol = -1;
    }
    cout << sol << "\n";
    for (int i = 1; i <= n; i++) {
      fr[v[i]] = 0;
    }
  }
  return 0;
}
