import random
import time
from collections import Counter

tcs = input()

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

    pre = {}
    shortest = -1
    for i, a in enumerate(arr):
        if a in pre:
            length = i-pre[a]+1
            if shortest == -1:
                shortest = length
            shortest = min(shortest, length)
        pre[a] = i
    
    print(shortest)

                
        