import math
testCases = int(input())
for t in range(testCases):
    n = int(input())
    arr = [int(x) for x in input().split()]
    if(n == 1):
        print("-1")
        continue
    
    hmap = {}
    min_len = math.inf
    for i, nums in enumerate(arr):
        if nums in hmap:
            min_len = min(min_len, i-hmap[nums]+1)
        hmap[nums] = i
    if min_len == math.inf:
        min_len = -1
    print(min_len)
        
            
                    