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

from collections import Counter
results = []

# 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:
        cnt = Counter(a)
        m = cnt.most_common(2)
        if len(m) == 1 or m[0][1] > m[1][1]:
            v = m[0][0]
            min_len = len(a)
            last_v = None
            for i in range(len(a)):
                if a[i] == v:
                    last_v = i
                    break
            changed = True
            while changed == True:
                # print("last_v", last_v)
                changed = False
                c = Counter()
                c[a[last_v]] += 1
                for i in range(last_v+1, len(a)):
                    # print('to', i)
                    c[a[i]] += 1
                    if len(c.most_common(2)) == 1 or c.most_common(2)[0][1] > c.most_common(2)[1][1]:
                        # print('got', i)
                        # print(c.most_common(2))
                        min_len = i-last_v+1
                        last_v = i
                        changed = True
                        break
            results.append(min_len)
        else:
            results.append(-1)
for i in results:
    print(i)
