#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long M = 1e9 + 7;
const long long M2 = 998244353;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < (long long)n; ++i) cin >> a[i];
  long long ans = 1e9;
  long long j = 0;
  set<pair<long long, long long> > s;
  map<long long, long long> mp;
  for (long long i = 0; i < n; i++) {
    while (true) {
      if (s.size() && j - i + 1 > 2) {
        if (s.size() == 1) {
          ans = min(ans, j - i);
          break;
        }
        auto it = s.end();
        --it;
        if ((*it).first != (*prev(it)).first) {
          ans = min(ans, j - i);
          break;
        }
      }
      if (j == n) {
        break;
      }
      pair<long long, long long> b = {mp[a[j]], a[j]};
      if (s.find(b) != s.end()) {
        s.erase(b);
      }
      mp[a[j]]++;
      s.insert({mp[a[j]], a[j]});
      j++;
    }
    pair<long long, long long> b = {mp[a[i]], a[i]};
    s.erase(b);
    mp[a[i]]--;
    if (mp[a[i]] > 0) s.insert({mp[a[i]], a[i]});
  }
  if (ans == 1e9) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  long long a = 1;
  while (t--) {
    solve();
    a += 1;
  }
}
