"""
    Author : Ashish Sasmal
    Python3 / PyPy3
"""

from sys import stdin as sin
def aint():return int(input())
def amap():return map(int,sin.readline().split())
def alist():return list(map(int,sin.readline().split()))
def astr():return input()

for _ in range(aint()):
    n = aint()
    a = alist()
    d = {}
    ans =float("inf")
    for i in range(n):
        if d.get(a[i],None)==None:
            d[a[i]] = i
        else:
            ans = min(ans,i-d[a[i]]+1)
        d[a[i]] = i
    if ans==float("inf") and ans!=0:
        print(-1)
    else:
        print(ans)