from collections import Counter
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:
        count_dic = Counter(ls)
        max_freq = 0
        ans = []
        for key,val in count_dic.items():
            if val>max_freq:
                max_freq = val
                ans = [key]
            elif val==max_freq:
                ans.append(key)
        if len(ans)>1:
            print("-1")
        else:            
            dic = {val:[] for val in ans}
            for ind,val in enumerate(ls):
                if val in ans:
                    dic[val].append(ind)
            result = 10**6
            f = 0
            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 = 1
                            result = x
            if f==0:
                print("-1")
            else:
                print(result)
            
            
                  
