from collections import defaultdict
for x in range(int(input())):
    n = int(input())
    m=list(map(int,input().split()))
    d=defaultdict(list)
    for i in range(n):
        d[m[i]].append(i)
    a=[]
    for i in d:
        d[i].sort()
        for j in range(1,len(d[i])):
            a.append(d[i][j]-d[i][j-1]+1)
    if len(a)==0:
        print(-1)
    else:
        print(min(a))
    
    