for _ in range(int(input())): N = int(input()) def count(x: int): x = bin(x)[2:] return x.count("0") A = list(map(int, input().split())) def check(L: int): B = [count(L + i) for i in range(N)] return A == B if N == 1: print(1 << A[0]) continue if min(A) == 0: it = A.index(0) ok = False for i in range(61): x = 1 << i if check(x - 1 - it): print(x - 1 - it) ok = True break if not ok: print(-1) else: G = [A[i + 1] - A[i] + 1 for i in range(N - 1)] it = G.index(max(G)) x = int("1" + "0" * A[it] + "1" * G[it], 2) if check(x - it): print(x - it) else: print(-1)