import sys


for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    d, flag = {}, 0
    m = sys.maxsize
    if n == 1:
        flag = 1
        m = -1
    else:
        for i in range(n):
            if a[i] in d:
                d[a[i]].append(i)
            else:
                d[a[i]] = [i]
        for i in d:
            s = len(d[i])
            if s > 1:
                for j in range(s-1):
                    flag = 1
                    m = min(m, ((d[i][j+1]-d[i][j])+1))
    if flag:
        print(m)
    else:
        print(-1)
			  	 		    				   	  			  	 	