from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n = ri()
    A = rl()
    if n == 1:
        print (-1)
        return
    shortest = n + 1
    last = {}
    for i, a in enumerate(A):
        if a in last:
            shortest = min(shortest, i - last[a] + 1)
        last[a] = i
    if shortest == n + 1:
        shortest = -1
    print (shortest)






mode = 'T'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()
