import random
import time
from collections import Counter

tcs = input()
#tcs = 1

#testdata = []

# for i in range(25000):
#     n = random.randint(1, 25000)
#     if n not in testdata:
#        testdata.append(n)

# testdata = testdata + testdata
# testdata[-1] = testdata[0]

# with open("testdata3.txt", "w") as f:
#     f.write(' '.join(str(x) for x in testdata))
#     exit(0)

# with open("testdata3.txt", "r") as f:
#     st = f.read()
#     for s in st.split(' '):
#         testdata.append(int(s))

for tc in range(int(tcs)):
    a_length = input()
    arr = input().split(' ')

    #test
    # a_length = len(testdata)
    # arr = testdata
    # start = time.time()
    #test over
    shortest = -1
    setver = set(arr)
    enumver = list(enumerate(arr))
    
    arr_length = len(arr)
    set_length = len(setver)
    sumtime = 0
    if set_length != arr_length:
        counts = Counter(arr)
        indices_list = {}
        lengths_list = {}
        for ind, x in enumver:
            if x not in indices_list:
                indices_list[x] = [ind]
            else:
                indices_list[x].append(ind)

        for i in range(len(arr)):
            a = arr[i]
            count = counts[a]
            if count == 1:
                continue
            indices = indices_list[a]
            if len(indices) <= 1:
                continue
            if a not in lengths_list:
                min_length = arr_length
                for index_of_index in range(len(indices) - 1):
                    length = indices[index_of_index + 1] - indices[index_of_index] + 1
                    if length <= min_length:
                        min_length = length
                length = min_length
                lengths_list[a] = length
            else:
                length = lengths_list[a]
            if length < shortest or shortest == -1:
                shortest = length
            if shortest == 2:
                break
    print(shortest)

    #test
    # end = time.time()
    # print(end-start)
                
        