#from collections import OrderedDict
#from collections import defaultdict
#from functools import reduce
#from itertools import groupby
#sys.setrecursionlimit(10**6)
#from itertools import accumulate
#from collections import Counter
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
'''
from threading import stack_size,Thread
sys.setrecursionlimit(10**6)
stack_size(10**8)
'''
def listt():
    return [int(i) for i in input().split()]
 
def gcd(a,b): 
   return math.gcd(a,b) 
 
def lcm(a,b): 
    return (a*b) // gcd(a,b) 

def factors(n):
    step = 2 if n%2 else 1
    return set(reduce(list.__add__,([i, n//i] for i in range(1, int(math.sqrt(n))+1, step) if n % i == 0)))

def comb(n,k):
    factn=math.factorial(n)
    factk=math.factorial(k)
    fact=math.factorial(n-k)
    ans=factn//(factk*fact)
    return ans

def is_prime(n): 
    if n <= 1: 
        return False
    if n == 2: 
        return True
    if n > 2 and n % 2 == 0: 
        return False

    max_div = math.floor(math.sqrt(n)) 
    for i in range(3, 1 + max_div, 2): 
        if n % i == 0: 
            return False
    return True
def maxpower(n,x):
    B_max = int(math.log(n, x)) + 1 #tells upto what power of x n is less than it like 1024->5^4
    return B_max
def binaryToDecimal(n): 
    return int(n,2) 

#d=sorted(d.items(),key=lambda a:a[1]) sort dic accc to values
#g=pow(2,6)
#a,b,n,m=map(int,input().split())
#print ("{0:.8f}".format(min(l)))
t=int(input())
for _ in range(t):
    n=int(input())
    l=listt()
    d={}
    if n<2:
        print(-1)
    else:    
        for i in range(n):
            if l[i] in d:
                d[l[i]].append(i+1)
            else:
                d[l[i]]=[i+1]   
        val=[]         
        for i in d:
            if len(d[i])>1:
                val.append(d[i])
        m=n
        for i in val:
            for j in range(1,len(i)):
                if i[j]-i[j-1]<m:
                    m=i[j]-i[j-1]
        print(m+1)            
                
                
               
        
          