'''
Created on 2019. 9. 21.

@author: kkhh88
'''
#q = int(input())
#x, y = map(int,input().split(' '))
#print (' '.join(list(map(str, s))))

q = int(input())

for i in range(q):
    n = int(input())
    a = list(map(int,input().split(' ')))
    
    if n < 2:
        print (-1)
        continue
        
    if a[0] == a[1]:
        print (2)
        continue
    
    cnt = [0] * (n + 1)
    
    i = 0
    j = 1
    cnt[a[0]] = 1
    cnt[a[1]] = 1
    
    ans = n+1

    while 1:
        if j - i + 1 >= ans:
            cnt[a[i]] = cnt[a[i]] - 1
            i = i + 1
            # check dominated
        else:
            j = j + 1
            if j >= n:
                break
            cnt[a[j]] = cnt[a[j]] + 1
            if cnt[a[j]] > 1:
                while a[j] != a[i]:
                    cnt[a[i]] = cnt[a[i]] - 1
                    i = i + 1
                ans = j - i + 1
     
    if ans > n:
        print (-1)
    else:   
        print (ans)
    
