for _ in range(int(input())):
    n = int(input())
    nums = list( map( int, input().split()))
    if n==1:print(-1)
    else:
        maxx, indx = -1, 0
        sidn = [0]*(n+1)
        saray= [[0 for j in range(2)] for i in range(n+1)]
        for j in range(n):
            i = nums[j]
            if saray[i][0]>0:
                saray[i][0] += 1
                #print(saray)
                saray[i][1]= min(saray[i][1], j-saray[i][1])
                if saray[i][1] == 0:saray[i][1]= j
                maxx = max(maxx, saray[i][0])
                if saray[i][0] == maxx:
                    sidn[maxx]+=1
                    indx = i
            else:
                saray[i][0] += 1
                #print(saray[i][0], "e")
            
        if sidn[maxx] >1 or maxx == -1:print(-1)
        else:
            print(saray[indx][1]+1)
            #print(saray)
          
        
         