#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> vec(n);
    bool pos = false;
    unordered_map<long long, vector<long long>> ma;
    for (long long i = 0; i < n; i++) {
      cin >> vec[i];
      ma[vec[i]].push_back(i);
      if (ma[vec[i]].size() >= 2) pos = true;
    }
    if (!pos) {
      cout << -1 << endl;
      continue;
    }
    long long ans = n;
    for (auto it = ma.begin(); it != ma.end(); it++) {
      long long x = it->first;
      for (long long i = 1; i < ma[x].size(); i++) {
        ans = min(ma[x][i] - ma[x][i - 1], ans);
      }
    }
    cout << ans + 1 << endl;
  }
}
