#RAVENS  #TEAM_2  #ESSI-DAYI_MOHSEN-LORENZO
from sys import stdin
input=stdin.readline
for i in range(int(input())):
    n = int(input())
    d=dict()
    a = list(map(int,input().split()))
    if n==1:
        print(-1)
        continue
    i = 0
    j = 1
    d[a[i]] = 1
    d[a[j]] = 1
    if a[i] == a[j]:
        print(2)
        continue
    j+=1
    M = float('inf')
    while i<n and j <n:
        if d.get(a[j],-1) == -1:
            d[a[j]] = 1
        else:
            while a[i] != a[j]:
                d[a[i]] = -1
                i+=1
            M = min(j-i+1,M)
            i+=1
        j+=1
            
    if M == float('inf'):
        print(-1)
    else:
        print(M)


    