for q in range(int(input())):
    n = int(input())
    s = [int(i) for i in input().split()]
    k = n
    if n < 2:
        print(-1)
        continue
    for i in range(n):
        if i + k > n:
            break
        for j in range(i+1, i+k):
            if s[i] == s[j] and j - i + 1 < k:
                k = j - i + 1
    if k == n and s[0] != s[-1]:
        print(-1)
    else:
        print(k)
