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)
    
    if len(setver) != len(arr):
        counts = Counter(arr)

        for i in range(len(arr) - 1):
            a = arr[i]
            count = counts[a]
            if count == 1:
                continue
            indices = [ind for ind, x in enumerate(arr, i) if x == a]

            length = (indices[1] - indices[0]) + 1
            if length < shortest or shortest == -1:
                shortest = length
            if shortest == 2:
                break
    print(shortest)

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