import sys
ip=lambda :sys.stdin.readline().rstrip()
from collections import *
for _ in range(int(ip())):
    n=int(ip())
    a=list(map(int,ip().split()))
    # x,y=map(int,ip().split())
    if n==1:
        print(-1)
        continue
    d=Counter(a)
    # d=list(e.items())
    if len(d)==1:
        print(2)
        continue
    s={0:[0,0]}
    ans=float('inf')
    mx,mx1=0,0
    for i in range(n):
        if i+1<n and a[i]==a[i+1]:
            ans=2
            break
        if a[i] not in s:
            s[a[i]]=[1,i]
        else:
            s[a[i]][0]+=1
        if s[a[i]][0]>s[mx][0]:
            mx=a[i]
        elif a[i]!=mx and s[a[i]][0]>s[mx1][0]:
            mx1=a[i]
        if s[mx][0]>s[mx1][0] and mx1!=0 and a[i]==mx:
            ans=min(ans,i-s[mx][1]+1)
        # print(i+1,ans,s)
        s[a[i]][1]=i
        # print(mx,mx1,s)
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)