import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
from collections import defaultdict as dd

t=int(input())
for _ in range(t):
    n=int(input())
    a=[int(x) for x in input().split()]
    
    cnts=dd(lambda:0)
    l=0 #not removed
    r=0 #not added
    
    ans=float('inf')
    moveRNext=True
    while True:
        if moveRNext:
            if r==n:
                break #terminating condition
            cnts[a[r]]+=1
            if cnts[a[r]]==2:
                moveRNext=False
            r+=1
        else:
            cnts[a[l]]-=1
            if cnts[a[l]]==1:
                ans=min(ans,r-l)
                moveRNext=True
            l+=1
    
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)