# from sys import stdin
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
# import sys
#

t = int(input())

for _ in range(t):

    n = int(input())

    l = list(map(int,input().split()))
    flag = 0
    hash = defaultdict(int)
    posn = defaultdict(int)
    mini = defaultdict(lambda :inf)
    for i in range(n):
        if i!=0:
         if l[i] == l[i-1]:
            print(2)
            flag = 1
            break

        hash[l[i]]+=1



        if hash[l[i]]>1:

            mini[l[i]] = min(i - posn[l[i]]+1,mini[l[i]])

        posn[l[i]] = i



    if not flag:
        # print(mini)
        flag = 0
        ans = inf
        for i in mini:
            if mini[i]!=inf:
                flag = 1
                ans = min(mini[i],ans)


        if flag == 0:
            print(-1)
        else:
            print(ans)



