import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int t = in.nextInt();
            for (int i = 0; i < t; i++)
            {
                int n = in.nextInt();
                int nums[] = CPUtils.readIntArray(n, in);
                if (nums.length == 1)
                {
                    out.println(-1);
                    continue;
                }
                //find dominant

                Set<Integer> set = new HashSet<>();
                boolean found = false;
                int min = Integer.MAX_VALUE;
                int cnt = 0;
                for (int k = 0; k < n; k++)
                {
                    cnt++;
                    if (set.contains(nums[k]))
                    {
                        found = true;
                        min = Math.min(min, cnt);
                        set.clear();
                        k--;
                        cnt = 0;
                    } else
                        set.add(nums[k]);
                }
                if (found)
                    out.println(min);
                else
                    out.println(-1);
            }
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

    }
}

