t = int(input())
for test in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    if n==1:
        print(-1)
        continue
    d = [-1]*(2*100001)
    minn = n+1
    for i in range(n):
        if d[arr[i]]==-1:
            d[arr[i]]=i
        elif (i-d[arr[i]]+1)<minn:
            minn = i-d[arr[i]]+1
        d[arr[i]] = i
    if minn==n+1:
        print(-1)
    else:
        print(minn)