import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;

/**
 * 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;
                }
                List<Pair> list = new ArrayList<>();
                for (int k = 0; k < n; k++)
                {
                    list.add(new Pair(nums[k], k + 1));
                }
                Collections.sort(list, (o1, o2) -> ((Integer) o1.val).compareTo(o2.val));
                int min = Integer.MAX_VALUE;
                int current = list.get(0).val;
                int currentPos = list.get(0).pos;
                for (int k = 1; k < n; k++)
                {
                    int next = list.get(k).val;
                    int nextPos = list.get(k).pos;

                    if (current == next)
                    {
                        min = Math.min(min, Math.abs(currentPos - nextPos) + 1);
                    }
                    current = next;
                    currentPos = nextPos;
                }
                if (min == Integer.MAX_VALUE)
                    out.println(-1);
                else
                    out.println(min);
            }
        }

    }

    static class Pair
    {
        int val;
        int pos;

        Pair(int val, int pos)
        {
            this.val = val;
            this.pos = pos;
        }

    }

    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;
        }

    }
}

