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