t = int(input())
for _ in range(t):
    n= int(input())
    a = list(map(int,input().split()))
    mp = {}
    pos = {}
    freq = {}
    for i in range(len(a)):
        if(a[i] in mp):
            
            
            mini = i - pos[a[i]]
            if(mini<mp[a[i]]):
                mp[a[i]] = mini
            pos[a[i]] = i
            freq[a[i]]+=1
            
        else:
            pos[a[i]] = i
            mp[a[i]] = 500000
            freq[a[i]]=1
    mini = min(mp.values())
    if(mini>=200001):
        print(-1)
    else:
        print(mini+1)
    
    