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