t = int(input())
for i in range(t):
    n = int(input())
    a = [int(s) for s in input().split()]
    b = sorted(a)
    k = maxi = 0
    notmaxi = 0
    
    if n == 1: print('-1')
    else:
        for j in range(1, n):
            if b[j] == b[j-1]:
                k += 1
            else:
                if k > maxi:
                    maxi = k
                    m = b[j-1]
                elif k == maxi:
                    notmaxi = k
                k = 0
                
        if maxi == 0 and k != 0:
            maxi = k
            m = b[j-1]
        if maxi == 0:
            print('-1')
        elif maxi == notmaxi: print('-1')
        else:
            k = 0
            mini = 100000
            for j in range(a.index(m), n):
                if a[j] == m and k != 0:
                    if k < mini:
                        mini = k + 1
                    k = 1
                else:
                    k += 1
            print(mini)