"""def power(x, y): 
    res = 1
    x=x
    while (y > 0): 
        if ((y & 1) == 1) : 
            res = (res * x) 
        y = y >> 1
        x = (x * x)
    return res""" 
"""def fact(n):
    if(n==0):
        return 1
    if(n==1):
        return 1
    return fact(n-1)+fact(n-2)"""
import math
t=int(input())
for _ in range(t):
    n=int(input())
    #x,y=map(int,input().split())
    arr=list(map(int,input().split()))
    #q=int(input())
    #s=input()
    s=set(arr)
    d=dict()
    #min_far=n
    min_here=n
    if(len(arr)==1):
        print("-1")
    else:
        if(len(s)==len(arr)):
            print("-1")
        else:
            for i in range(n):
                if arr[i] not in d:
                    d[arr[i]]=i
                    
            for i in range(n):
                if d[arr[i]]!=i:
                     min_here=min(min_here,(i-d[arr[i]]))
                     d[arr[i]]=i
            print(min_here+1)
                
    
