for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    x=n+1 #since An<=n
    b=['*']*(n+1)
    for i in range(n):
        if b[a[i]]!='*':
            x=min(x,i-b[a[i]]+1)
        b[a[i]]=i
    print([-1,x][x<n+1])