t = int(input())

for i in range(t):

    n = int(input())
    a = input().split(' ')

    lastpos = [-1] * 200000
    min = n

    # atleast_2 = False

    # for i in range(len(a) - 1):
    #     if(a[i] != a[i+1]):
    #         atleast_2 = True


    for i in range(len(a)):    
        a[i] = int(a[i])
        # print("a[i] = ", a[i], ", lastpos[a[i]] = ", lastpos[a[i]])
        if(lastpos[a[i]] != -1):
            subarray_len = i - lastpos[a[i]] + 1
            if(min > subarray_len):
                min = subarray_len
        lastpos[a[i]] = i

    if(len(a) > 1):
        print(min)
    else:
        print(-1)
			 		    				  		  	   							