for _ in range(int(input())):
    n = int(input())
    lista = list(map(int, input().split()))
    if (len(lista)) < 2:
        print(-1)
    else:
        m = 10000000
        dictio = {}
        for i in range(len(lista)):
            if( lista[i] in dictio):
                dictio[lista[i]].append( i )
            else:
                dictio[lista[i]] = [i]
        for x in dictio:
            try:
                sum = 1000000000
                try:
                    for j in range(len(dictio[x])):
                        if( dictio[x][j + 1] - dictio[x][j]   < sum):
                            sum = dictio[x][j + 1] - dictio[x][j]
                except:
                    pass
                if sum < m:
                    m = sum
            except:
                pass
        if m == 10000000:
            print(-1)
        else:
            print(m + 1)
 	 	  		 	      	      	  	 	 	