from collections import defaultdict
t = int(input())

for _ in range(t):
    k=0
    for ii in range(1111):
        k += 5
    inf=99999999999999999999999999999999999999999999999999999999999999999999999999999999999
    n = int(input())

    l = list(map(int,input().split()))
    fflag = 0
    h = defaultdict(int)
    posn = defaultdict(int)
    mini = defaultdict(lambda :inf)
    for i in range(n):
        if i!=0:
            if l[i] == l[i-1]:
                print(2)
                fflag = 1
                break
        h[l[i]]+=1



        if h[l[i]]>1:

            mini[l[i]] = min(i - posn[l[i]]+1,mini[l[i]])

        posn[l[i]] = i



    if not fflag:
        # print(mini)
        fflag = 0
        ans = inf
        for i in mini:
            if mini[i]!=inf:
                fflag = 1
                ans = min(mini[i],ans)


        if fflag == 0:
            print(-1)
        else:
            print(ans)