#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t;
  cin >> t;
  while (t--) {
    ll n, i;
    cin >> n;
    ll a[n];
    vector<pair<ll, ll> > VP;
    set<ll> S;
    for (i = 0; i < n; ++i) {
      cin >> a[i];
      S.insert(a[i]);
      VP.push_back(make_pair(a[i], i));
    }
    if (n < 2) {
      cout << -1 << "\n";
      continue;
    } else {
      if (S.size() == n) {
        cout << -1 << "\n";
        continue;
      }
    }
    ll minn = LLONG_MAX;
    sort(VP.begin(), VP.end());
    for (i = 0; i < VP.size() - 1; ++i) {
      if (VP[i].first == VP[i + 1].first) {
        ll diff = VP[i + 1].second - VP[i].second + 1;
        minn = min(minn, diff);
      }
    }
    cout << minn << "\n";
  }
}
