'''
    Author : thekushalghosh
    Team   : CodeDiggers
'''
import sys,math
input = sys.stdin.readline

############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    n = int(input())
    return(n)
def inlt():
    a = list(map(int,input().split()))
    return(a)
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    a,b = map(int,input().split())
    return(a,b)
################################################################
t = 1
t = int(input())
for i in range(t):
    n = inp()
    a = inlt()
    q = dict()
    for i, x in enumerate(a):
        if x in q:
            n = min(n,i - q[x] )
        q[x] = i
    if n < len(a):
        print(n + 1)
    else:
        print(-1)