import sys
import collections
import heapq
import math
 
input = sys.stdin.readline
 
def rints(): return map(int, input().strip().split())
def rstr(): return input().strip()
def rint(): return int(input().strip())
def rintas(): return [int(i) for i in input().strip().split()]
    
def gcd(a, b):       
    if (b == 0): 
         return a 
    return gcd(b, a%b) 

t = rint()
for _ in range(t):
    n = rint()
    a = rintas()
    memo = {}
    ans = n+1
    for i,v  in enumerate(a):
        if v in memo:
            ans = min(ans, i-memo[v]+1)
        memo[v] = i
    
    if ans == n+1 or ans == 1:
        print(-1)
    else:
        print(ans)
