t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    d = dict()
    if n==1:
        print(-1)
    else:
        for i,j in enumerate(a):
            if d.get(j):
                d[j].append(i+1)
            else:
                d[j] = [i+1]
        minimum = n
        flag = 0
        for i in d.keys():
            x = d[i]
            n1 = len(x)
            if n1>1:
                flag = 1
            j = 0
            while j<n1-1:
                size = x[j+1]-x[j]+1
                if size<minimum:
                    minimum = size
                j+=1
        if flag == 0:
            print(-1)
        else:
            print(minimum)