# f = open('input', 'r')

from collections import Counter
results = []

def checkMostCommon(x):
    if len(x) < 2:
        return None
    cnt = Counter(x)
    mc = cnt.most_common(2)
    if len(mc) < 2:
        return mc[0]
    if mc[0][1] == mc[1][1]:
        return None
    return mc[0]


# for _ in range(int(f.readline())):
for _ in range(int(input())):
    # n = int(f.readline())
    n = int(input())
    # a = list(map(int, f.readline()[:-1].split(' ')))
    a = list(map(int, input().split(' ')))
    if len(a) < 2:
        results.append(-1)
    else:
        tmp = checkMostCommon(a)
        if not tmp:
            results.append(-1)
        else:
            m = tmp
            mi = len(a)
            for i in range(len(a)):
                for j in range(i, len(a)):
                    if j-i < mi:
                        tmp = checkMostCommon(a[i:j])
                        if tmp and tmp[0] == m[0]:
                            mi = j-i
            results.append(mi)
for i in results:
    print(i)
