from datetime import datetime

import math


class Read:
    @staticmethod
    def string():
        return input()

    @staticmethod
    def int():
        return int(input())

    @staticmethod
    def list(sep=' '):
        return input().split(sep)

    @staticmethod
    def list_int(sep=' '):
        return list(map(int, input().split(sep)))


def solve():
    n = Read.int()
    a = Read.list_int()
    data = {}
    for i in range(n):
        if a[i] in data:
            data[a[i]].append(i)
        else:
            data[a[i]] = [i]

    m = math.inf
    for i, v in data.items():
        l = len(v)
        if l < 2:
            continue
        else:
            for j in range(l - 1):
                r = v[j + 1] - v[j]
                if r < m:
                    m = r
    if m == math.inf:
        print('-1')
    else:
        print(m + 1)

# query_count = 1
query_count = Read.int()
while query_count:
    query_count -= 1
    solve()
