#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
int32_t main() {
  ios_base::sync_with_stdio(false);
  int64_t Q;
  cin >> Q;
  while (Q-- > 0) {
    int64_t n;
    cin >> n;
    vector<vector<int64_t> > v(n);
    for (int64_t i = 0; i < n; ++i) {
      int64_t a;
      cin >> a;
      v[a - 1].push_back(i);
    }
    int64_t mn = INT_MAX;
    for (int64_t i = 0; i < n; ++i) {
      for (int64_t j = 1; j < v[i].size(); ++j) {
        mn = min(mn, v[i][j] - v[i][j - 1]);
      }
    }
    if (mn == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << mn + 1 << endl;
    }
  }
}
