#include <bits/stdc++.h>
using namespace std;
void faster() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const int MAXN = 3e5 + 1;
const int INF = 1e9 + 2;
long long T, n, a[MAXN], nx[MAXN], R[MAXN], ans;
void solve() {
  ans = INF;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    R[i] = n + 10;
  }
  for (int i = n; i >= 1; --i) {
    nx[i] = R[a[i]];
    R[a[i]] = i;
  }
  for (int i = 1, l = 1; i <= n; ++i) {
    if (nx[i] != n + 10) {
      ans = min(nx[i] - i + 1, ans);
    }
  }
  if (ans == INF)
    cout << "-1\n";
  else {
    if (ans < 2) {
      while (true) {
      }
    }
    cout << ans << '\n';
  }
}
int main() {
  faster();
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
