t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    if n < 2:
        print(-1)
    else:
        i = 0
        j = 0
        seen = set()
        pos = {}
        ans = n
        while i < n:
            # print(seen, pos, i)
            if a[i] in seen:
                for ind in range(j, pos[a[i]]):
                    seen.remove(a[ind])
                    pos.pop(a[ind])
                j = pos[a[i]] + 1
                ans = min(ans, i - pos[a[i]] + 1)
                pos[a[i]] = i
            else:
                seen.add(a[i])
                pos[a[i]] = i
            # print(seen, pos, i)
            i += 1
        print(ans)
