#include <bits/stdc++.h>
using namespace std;
const int maxd = 2e5 + 10;
int a[maxd], vis[maxd];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int ans = 1e9;
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      vis[i] = 0;
    }
    int l = 1;
    for (int i = 1; i <= n; i++) {
      if (vis[a[i]]) {
        while (a[l] != a[i]) vis[a[l]]--, l++;
        ans = min(i - l + 1, ans);
        vis[a[l]]--, l++;
      }
      vis[a[i]]++;
    }
    if (ans == 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
