#include <bits/stdc++.h>
using namespace std;
void solve() {
  map<int, vector<int>> make_pair;
  int n, i, mn = INT_MAX;
  cin >> n;
  int a[n];
  for (i = 0; i < n; ++i) {
    cin >> a[i];
    make_pair[a[i]].push_back(i);
  }
  for (i = 0; i < make_pair.size(); ++i) {
    for (int j = 1; j < make_pair[i].size(); ++j) {
      mn = min(make_pair[i][j] - make_pair[i][j - 1] + 1, mn);
    }
  }
  if (mn == INT_MAX) mn = -1;
  cout << mn;
}
int main() {
  ios::sync_with_stdio(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << '\n';
  }
  return 0;
}
