#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import operator
t=int(input())
for i in range (1,t+1):
    n=int(input())
    a = [int(x) for x in input().split()]
    
    length=[]
    index=[]
    
    k=0
    for i in a:
        index.append((k,a[k]))
        k+=1
    
    #print(index)
    sorted_elm=[(elm, idx) for (idx, elm) in sorted(index, key=lambda item: item[1], reverse=False)]
    #print(sorted_elm)
    
    minm=n+1
    #for j in range(1,n+1):
        #b=[]
        #cnt=0
    #prev_idx=-1
    for i in range(0,n-1):
        if sorted_elm[i+1][0]==sorted_elm[i][0]:
            min_now=sorted_elm[i+1][1]-sorted_elm[i][1]+1
            #print(min_now)
            if min_now<minm:
                minm=min_now
        #prev_idx=idx
        #length.append(b)
        
    #res=-1
    #for l in length:
    #    if len(l)>=2:
    #        res=0
    #       break
    
    if minm==n+1:
        print(-1)
    else:
        print(minm)
    
    #if res==0:
        #minm=n
        #for l in length:
            #if len(l)>=2:
                #for ll in range(0,len(l)-1):
                    #min_now=l[ll+1]-l[ll]+1
                    #if min_now < minm:
                        #minm=min_now
                
        #res=minm
        
    #print(sorted_elm)
    #print(length)
    #print(res)


# In[ ]:





# In[ ]:




