#!/usr/bin/env python3
 
from __future__ import division, print_function
import collections

def main():
    import sys
    data = iter(map(int, sys.stdin.buffer.read().decode('ascii').split()))
    t = next(data)
    INF = 1000000    
    res = [INF] * t
    for i in range(t):
        n = next(data)
        a = tuple(next(data) for _ in range(n))
        d = dict()
        for j, aj in enumerate(a):
            if aj in d:
                last = d[aj]
                res[i] = min(res[i], j + 1 - last)
            d[aj] = j
        if res[i] == INF:
            res[i] = -1
    sys.stdout.buffer.write('\n'.join(str(x) for x in res).encode('ascii'))    
    return 0
 
if __name__ == '__main__':
    main()
