t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    dic = 0
    s = set(a)
    m = -1
    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)):
            x = a.count(a[i])
            if(x>m):
                dic = a[i]
                m = x
        r = a.index(dic)
        least = 10000000000
        for i in range(r+1,len(a)):
            if(a[i]==dic and i-r < least):
                least = i-r+1
                r = i
        print(least)



