from sys import stdin, stdout
from collections import Counter, defaultdict
pr=stdout.write
raw_input  = stdin.readline
def ni():
    return int(raw_input())


def li():
    return list(map(int,raw_input().split()))


def pn(n):
    stdout.write(str(n)+'\n')


def pa(arr):
    pr(' '.join(map(str,arr))+'\n')

# fast read function for total integer input

def inp():
    # this function returns whole input of
    # space/line seperated integers 
    # Use Ctrl+D to flush stdin.
    return (map(int,stdin.read().split()))
    
range = xrange # not for python 3.0+

# main code

for t in range(ni()):
    n=ni()
    l=li()
    d=defaultdict(list)
    f=0
    ans=n
    for i in range(n):
        d[l[i]].append(i)
    for i in d:
        n1=len(d[i])
        for j in range(1,n1):
            f=1
            ans=min(ans,d[i][j]-d[i][j-1]+1)
    if not f:
        pn(-1)
    else:
        pn(ans)
