#include <bits/stdc++.h>
using namespace std;
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << '\n';
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}
const int N = 2e5 + 7;
int n, a[N], fq[N];
bool f(int len) {
  (len);
  for (int i = 1; i <= n; i++) fq[i] = 0;
  map<int, int> cnt;
  set<int> s;
  cnt[0] = n, s.insert(0);
  for (int i = 0, j = 0; i + len - 1 < n; i++) {
    while (j < n && j - i + 1 <= len) {
      cnt[fq[a[j]]]--;
      if (cnt[fq[a[j]]] == 0) s.erase(fq[a[j]]);
      fq[a[j]]++;
      s.insert(fq[a[j]]);
      cnt[fq[a[j]]]++;
      j++;
    }
    (i, j);
    for (auto f : cnt) (f.first, f.second);
    if (*s.rbegin() > 0 && cnt[*s.rbegin()] == 1) return true;
    cnt[fq[a[i]]]--;
    if (cnt[fq[a[i]]] == 0) s.erase(fq[a[i]]);
    fq[a[i]]--;
    s.insert(fq[a[i]]);
    cnt[fq[a[i]]]++;
  }
  return false;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    if (n == 1) {
      cout << -1 << '\n';
      continue;
    }
    int lo = 2, hi = n;
    while (hi - lo > 1) {
      int mid = (hi + lo) / 2;
      if (f(mid))
        hi = mid;
      else
        lo = mid;
    }
    if (f(lo))
      cout << lo << '\n';
    else if (f(hi))
      cout << hi << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
