import sys,os,math
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import Queue


# sys.stdin = open('input')
# n, k = map(int, raw_input().split())
# da = map(int, raw_input().split())
# db = map(int, raw_input().split())



def main():
    q = map(int, raw_input().split())[0]
    for i in range(q):
        n = map(int, raw_input().split())[0]
        da = map(int, raw_input().split())
        x, y = 0, 100000000
        di = defaultdict(lambda : -1)
        for idx, j in enumerate(da):
            if di[j] !=-1:
                if idx - di[j] < y-x:
                    x, y = di[j], idx
            di[j] = idx
        if y-x==100000000:
            print -1
        else:
            print y-x+1

main()
