t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    d={}
    mini=float("inf")
    if len(a)<2:
        print(-1)
    else:
        for i in range(n):
            if a[i] not in d:
                d[a[i]]=i
            else:
                mini=min(mini,i-d[a[i]]+1)
                d[a[i]]=i
        if mini!=float("inf"):    
            print(mini)
        else:
            print(-1)