import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache   #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------

for _ in range(int(input())):
    n = int(input())
    a = list(mapi())
    if n<=1:
        print(-1)
    else:
        mp = defaultdict(list)
        res = float("inf")
        for i in range(n):
            if a[i] in mp:
                res = min(res,i-mp[a[i]][-1]+1)
            mp[a[i]].append(i)
        if res ==float("inf"):
            print(-1)
        else:
            print(res)
