#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
bool cmp(int &a, int &b) { return a > b; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int a[N], t, p;
    bool book[N];
    fill(book, book + N, false);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 1; i <= 1025; i++) {
      fill(book, book + 1050, false);
      p = 1;
      t = i;
      for (int j = 1; j <= n; j++) {
        if ((a[j] ^ i) <= 1024) book[a[j] ^ i] = true;
      }
      for (int j = 1; j <= n; j++) {
        if (book[a[j]] == false) {
          p = 0;
        }
      }
      if (p == 1) break;
    }
    if (p == 1)
      printf("%d\n", t);
    else
      printf("%d\n", -1);
  }
  return 0;
}
