#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int ans = 0;
    for (int k = 1; k <= 1024; k++) {
      vector<bool> used(10000);
      for (int i = 0; i < n; i++) {
        used[(a[i] ^ k)] = true;
      }
      bool ok = true;
      for (int i = 0; i < n; i++) {
        if (!used[a[i]]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        ans = k;
        break;
      }
    }
    cout << (ans == 0 ? -1 : ans) << "\n";
  }
  return 0;
}
