import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		 BufferedReader br = new BufferedReader(new
		 InputStreamReader(System.in));
		StreamTokenizer in = new StreamTokenizer(br);
		PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		in.nextToken();
		int t = (int) in.nval;
		int[] arr = new int[200005];
		int[] idx = new int[200005];
		int n, ans, max, i, tmp, val;
		boolean flag;
		while (--t >= 0)
		{
			ans = -1;
			max = 1;
			val = -1;
			flag = false;
			Arrays.fill(arr, 0);
			Arrays.fill(idx, -1);
			in.nextToken();
			n = (int) in.nval;
			for (i = 0; i < n; i++)
			{
				in.nextToken();
				tmp = (int) in.nval;
				arr[tmp]++;
				if (arr[tmp] > max)
				{
					flag = true;
					max = arr[tmp];
					if (idx[tmp] == -1)
						idx[tmp] = i;
					else
					{
						if (val == -1)
							val = i - idx[tmp] + 1;
						else if (i - idx[tmp] + 1 < val)
							val = i - idx[tmp] + 1;
						idx[tmp] = i;
					}
					ans = tmp;
				}
				else if (arr[tmp] == max)
				{
					flag = false;
				}
				idx[tmp] = i;
			}
			if (flag)
				pr.println(val);
			else
				pr.println(-1);
		}
		pr.flush();
	}
}