import sys
import math
import itertools
import collections


def sieve(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if (prime[p] == True):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    prime[0] = prime[1] = False
    r = [p for p in range(n + 1) if prime[p]]
    return r
def divs(n, start=1):
    r = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if (n % i == 0):
            if (n / i == i):
                r.append(i)
            else:
                r.extend([i, n // i])
    return r
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def wr(arr): return ''.join(map(str, arr))
def revn(n): return str(n)[::-1]
def prime(n):
    if n == 2: return True
    if n % 2 == 0 or n <= 1: return False
    sqr = int(math.sqrt(n)) + 1
    for d in range(3, sqr, 2):
        if n % d == 0: return False
    return True
def convn(number, base=3):
    newnumber = ''
    while number > 0:
        newnumber = str(number % base) + newnumber
        number //= base
    return newnumber


t = ii()
for _ in range(t):
    n = ii()
    a = li()
    posd = {}
    for i in range(n):
        if a[i] not in posd:
            posd[a[i]] = [i]
        else:
            posd[a[i]].append(i)
    ans = n + 1
    for item in posd:
        lid = len(posd[item])
        if lid > 1:
            for i in range(lid - 1):
                ans = min(ans, posd[item][i + 1] - posd[item][i] + 1)
    if ans == n + 1:
        print(-1)
    else:
        print(ans)
