from collections import Counter
from collections import defaultdict
import math
t=int(input())
for i in range(0,t):
    n=int(input())
    a=list(map(int,input().split()))
    d=defaultdict(lambda:[])
    g=set()
    for i in range(0,n):
        if(d.get(a[i])==None):
            d[a[i]].append(i)
        else:
            d[a[i]].append(i)
    k=list(d.keys())
    mn=999999999
    for i in k:
     #   print(d[i])
        l=len(d[i])
        if(l>=2):
            for j in range(1,l):
                if((d[i][j]-d[i][j-1])<mn):
                    mn=d[i][j]-d[i][j-1]
    if(mn==999999999):
        print(-1)
    else:
        print(mn+1)
                
            
    
        
    