t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    if n == 1:
        print (-1)
    elif a[0]==a[1]:
        print (2)
    else:
        minn = n+1
        d = {a[0]:0, a[1]:1}
        if n>2:
            for i in range(2,n):
                if a[i] in d:
                    if (i - d[a[i]])<minn:
                        minn = i - d[a[i]]+1
                d[a[i]]=i
        else:
            print(-1)
        
        if minn>n:
            print (-1)
        else:
            print (minn)