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