t = int(input())

for ti in range(t):
    l=int(input())
    arr=[int(i) for i in input().split(" ")]
    if l==1:
        print(-1)
        continue
    mx=0
    tg=0
    masterKey=-1
    curlen=9999999999
    minlen=9999999999
    di={}
    for j in range(len(arr)):
        if arr[j] in di:
            di[arr[j]]+=1
            if di[arr[j]]>mx:
                mx=di[arr[j]]
                masterKey=arr[j]
                tg=0
            elif di[arr[j]]==mx:
                tg=1
        else:
            di[arr[j]]=1

    
    if tg:
        print(-1)
        continue
    if mx==1:
        print(-1)
        continue

    for j in arr:
        if masterKey==j:

            curlen+=1
            if curlen<minlen:
                minlen=curlen
            curlen=1

        else:
            curlen+=1
    #print(di)
    print(minlen)