#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int t, n, ans;
vector<int> v[N];
void _clear() {
  ans = 1e9;
  for (int i = 1; i <= n; i++) v[i].clear();
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    _clear();
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      v[x].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
      int sz = (int)(v[i].size());
      for (int j = 1; j < sz; j++) ans = min(ans, v[i][j] - v[i][j - 1] + 1);
    }
    if (ans == 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
}
