t = int(input())

for w in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]

    if n < 2:
        print(-1)
        continue
    else:
        pos = [-1 for i in range(n + 1)]
        ans = int(1e6)
        for i in range(n):
            if pos[a[i]] == -1:
                pos[a[i]] = i
            else:
                ans = min(ans, i - pos[a[i]] + 1)
                pos[a[i]] = i

        if ans == int(1e6):
            print(-1)
        else:
            print(ans)


        '''
        while i < j:
            if a[i] == a[j]:
                while a[i + 1] == a[j] and i < (n - 1):
                    i += 1
                while a[i] == a[j - 1] and j >= 1:
                    j -= 1
                    ans = (j - i) + 1
            i += 1

        while i < mid <= j:
            if a[i] == a[j]:
                if (j - i) + 1 > 1:
                    ans = (j - i) + 1
                else:
                    break
            i += 1
            j -= 1
        '''
