#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  cin.ignore();
  while (t--) {
    int n, arr[1030];
    map<int, int> maps;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &arr[i]);
      maps[arr[i]]++;
    }
    int ans = -1;
    for (int i = 1; i < 1025; i++) {
      map<int, int> cpmaps;
      cpmaps = maps;
      bool pass = true;
      for (int j = 0; j < n; j++) {
        int a = arr[j] ^ i;
        if (cpmaps[a])
          cpmaps[a]--;
        else {
          pass = false;
          break;
        }
      }
      if (pass) {
        ans = i;
        break;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
