import math
import sys
from collections import defaultdict, Counter
from functools import lru_cache

t = int(input())
for _ in range(t):
    n = int(input())
    arr = [int(i) for i in input().split()]
    count = Counter(arr)
    vals = list(count.values())
    m = max(vals)
    if m == 1:
        print(-1)
    else:
        start = 0
        end= 0
        Dict = defaultdict(int)
        # for i in range(n):
        #     Dict[arr[i]] += 1
        #     if Dict[arr[i]] == 2:
        #         end = i-1
        #         break
        ans = math.inf
        while end < n:
            Dict[arr[end]] += 1
            while Dict[arr[end]] == 2:
                ans = min(ans, end-start+1)
                Dict[arr[start]] -= 1
                start += 1
            end += 1
        print(ans)