#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &(T));
  while (T--) {
    int n;
    scanf("%d", &(n));
    int arr[n + 5];
    for (int i = 1; i <= n; i++) scanf("%d", &(arr[i]));
    if (n < 2) {
      printf("-1\n");
      continue;
    }
    int Cnt[n + 5];
    for (int i = 0; i < n + 3; i++) Cnt[i] = 0;
    int a = 1, ans = INT_MAX;
    for (int i = 1; i <= n; i++) {
      Cnt[arr[i]]++;
      if (Cnt[arr[i]] > 1) {
        while (Cnt[arr[i]] >= 2) {
          Cnt[arr[a]]--;
          a++;
        }
        if (Cnt[arr[i]] == 1) {
          Cnt[arr[i]] += 1;
          a -= 1;
        }
        ans = min(ans, i - a + 1);
      }
    }
    if (ans == INT_MAX) ans = -1;
    printf("%d\n", ans);
  }
}
