for step in range(int(input())):
    n=int(input())
    A=[int(i) for i in input().split()]
    dict={}
    ans = float("inf")
    for i,e in enumerate(A):
        if e not in dict:
            dict[e]=i
        else:
            ans=min(i-dict[e]+1, ans)
            dict[e]=i
    if ans==float("inf"):
        print(-1)
    else:
        print(ans)