#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n];
    map<long long, vector<long long> > ind;
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
      if (ind.find(arr[i]) == ind.end()) {
        ind.insert(
            pair<long long, vector<long long> >(arr[i], vector<long long>()));
      }
      ind[arr[i]].push_back(i);
    }
    map<long long, vector<long long> >::iterator it;
    long long ans = INT_MAX;
    for (it = ind.begin(); it != ind.end(); it++) {
      for (long long j = 1; j < it->second.size(); j++) {
        if (it->second[j] - it->second[j - 1] < ans) {
          ans = it->second[j] - it->second[j - 1] + 1;
        }
      }
    }
    if (ans == INT_MAX) {
      cout << -1 << "\n";
      continue;
    }
    cout << ans << "\n";
  }
}
