""" occu of ara:i > 1 is dominating"""
for _ in range(int(input())):
    n = int(input())
    ara = list(map(int, input().split()))
    di = {}
    lan = 10**8
    for i in range(n):
        if ara[i] not in di:
            di[ara[i]] = i
        else:
            ind = i - di[ara[i]] +1
            if ind < lan:
                lan = ind
            di[ara[i]]=i
        
    if lan == 10**8:print(-1)
    else:print(lan)
    
