t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    dic = {}
    s = set(a)
    m = 1000000000000000000000
    if(len(a)==1):
        print(-1)
    elif(len(s)==len(a)):
        print(-1)
    elif(a.count(a[0])==len(a)):
        print("2")
    else:
        for i in range(len(a)):
            if a[i] in dic:
                m = min(m,i-dic[a[i]])
            dic[a[i]] = i

        print(m+1)

