#include <bits/stdc++.h>
using namespace std;
template <class T>
T maxx(T A, T B) {
  return (A > B) ? A : B;
}
template <class T>
T minn(T A, T B) {
  return (A < B) ? A : B;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int tt, n, m, k, i, j, l, a, b, x, y;
  cin >> tt;
  while (tt--) {
    cin >> n;
    vector<long long int> v(n);
    vector<long long int> f(n + 5, 0), ind(n + 5);
    long long int ans = (1e11), mn = (1e11);
    for (i = 0; i < n; i++) {
      cin >> v[i];
      f[v[i]]++;
      if (f[v[i]] >= 2) {
        mn = minn(mn, i - ind[v[i]] + 1);
      }
      ind[v[i]] = i;
    }
    if (mn != (1e11)) {
      cout << mn << "\n";
    } else
      cout << -1 << "\n";
  }
  return 0;
}
