t =  int(input());
for test in range(t):
    n = int(input());
    arr = list(map(int,input().split()));
    ind = [[] for i in range(n+1)];
    for i in range(n):
        ind[arr[i]].append(i);
    ans = n;
    for i in range(1,n+1):
        if(len(ind[i])>=2):
            for j in range(len(ind[i])-1):
                ans = min(ans,ind[i][j+1]-ind[i][j]);
    if(ans==n):
        ans=-1;
    else:
        ans+=1;
    print(ans);
                
