import sys
def func(arr,d,party,n):
    if n==1 or n==0:
        print(-1)
    else:
        cnt = 0
        for cur in arr:
            if cur in party:
                d = min(d, cnt-party[cur]+1)
                party[cur] = cnt
            else:
                party[cur] = cnt
            cnt+=1
        if d == sys.maxsize:
            print(-1)
        else:
            print(d)
for _ in range(int(input())):
    party = {}
    d = sys.maxsize
    n = int(input())
    arr = list(map(int, input().split()))
    func(arr,d,party,n)
    