def map_list(lst):
    return [lst.count(w) for w in set(lst)]

def is_dominated(sub):
    if len(sub) < 2:
        return False
    mp = sorted(map_list(sub), reverse=True)
    if len(mp) > 1:
        return mp[0] > mp[1]
    return True # if only one value in list
 
def gen_test(n):
    from random import sample
    return sample(range(0, 2500000), 250000)

T = int(input())
for i in range(T):
    n = int(input())
    lst = [int(w) for w in input().split()]
    mp_lst = map_list(lst)
    mx = max(mp_lst)
    if mx == 1 or mp_lst.count(mx) > 1:
        print(-1)
        continue
    found = False
    for r in range(1, n):
        for j in range(n-r):
            if is_dominated(lst[j:j+r+1]): # +1 bcs list splicing works this way
                print(r+1)
                found = True
                break
        if found:
            break
    else:
        print(-1)
