# -*- coding: utf-8 -*-
"""
Created on Fri Nov 15 09:32:42 2019

@author: Administrator
"""
t = int(input())

for i in range(0,t):
    n = int(input())
    a = list(map(int, input().split()))
    b = [0]*int(3e5); longest = n; idx = 0

    for j in range(0,len(a)):
        #print("j: ", j, "a[j]: ", a[j], "b[a[j]]: ",b[a[j]])
        if b[a[j]] != 0:
            idx = 1; longest = min(longest, (j+1) - b[a[j]] + 1);
            #print(longest, " ", idx)

        b[a[j]] = j+1;
        #print(b[a[j]])
    #print(b[0:10]); print(longest, idx)

    if (idx == 1): print(longest)
    else: print(-1)
