#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, ans = -1;
    cin >> n;
    int a[n + 1];
    vector<int> s;
    for (int i = 1; i <= n; ++i) cin >> a[i], s.push_back(a[i]);
    sort(s.begin(), s.end());
    for (int i = 1; i < 1024; ++i) {
      vector<int> v;
      for (int j = 1; j <= n; ++j) v.push_back(a[j] ^ i);
      bool flag = true;
      sort(v.begin(), v.end());
      for (int j = 0; j < v.size(); ++j) {
        if (v[j] != s[j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        ans = i;
        break;
      }
    }
    cout << ans << "\n";
  }
}
