#!/usr/bin/env python
import sys
from collections import Counter
import operator

def atoi(line):
    line = line.rstrip().split()
    return list(map(int, line))

def check_len(a, n):
    if n == 1:
        return -1
    freq = dict(Counter(a))
    occ = max(freq.items(), key = operator.itemgetter(1))
    if list(freq.values()).count(occ[1]) > 1:
        return -1
    start, dist, opened = 0, sys.maxsize, False
    for idx, elem in enumerate(a):
        if elem == occ[0]:
            if not opened:
                start = idx
                opened = True
            else:
                dist = min(dist, idx - start + 1)
                if dist == 2:
                    return dist
                start = idx
    return dist


T = int(input())
for _ in range(T):
    n = int(input())
    a = atoi(sys.stdin.readline())
    l = check_len(a, n)
    print(l)
    
    
