from collections import Counter,defaultdict
import statistics
t = int(input())
for _ in range(t):
    n = int(input())
    ls = list(map(int,input().strip().split()))
    if n==1:
        print("-1")
    else:
        f = 1
        result = 10**6
        dic = defaultdict(list)
        for i in range(n):
            dic[ls[i]].append(i)
        for key,val in dic.items():
            if len(val)>1:
                for i,v in enumerate(val[1:],1):
                    x = v-val[i-1]+1
                    if x<result:
                        f = 0
                        result = x
                
        if f==1:
            print("-1")
        else:
            print(result)
        
                
                
                
                
