#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int> > v(n);
    for (int i = 0; i < n; ++i) {
      int tmp;
      cin >> tmp;
      v[tmp - 1].push_back(i);
    }
    int min = INT_MAX;
    for (int i = 0; i < n; ++i) {
      if (v[i].empty()) continue;
      for (int j = 0; j < v[i].size() - 1; ++j)
        min = std::min(min, v[i][j + 1] - v[i][j] + 1);
    }
    if (min == INT_MAX)
      cout << -1 << '\n';
    else
      cout << min << '\n';
  }
  return 0;
}
