import sys
import string

from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations,permutations

# input = sys.stdin.readline
flush = lambda : sys.stdout.flush
comb = lambda x , y : (factorial(x) // factorial(y)) // factorial(x - y) 


#inputs
# ip = lambda : input().rstrip()
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())


def solve():
    n = ii()
    arr = rr()
    if n == 1:
        print(-1)
        return
    
    hmap = defaultdict(list)
    for i, j in enumerate(arr):
        hmap[j].append(i)
        
    ans = float('inf')
    for i in hmap.values():
        for j in range(len(i) - 1):
            ans = min(ans , i[j+1] - i[j] + 1)
            
    print(ans)






for _ in range(ii()):
    solve()