#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &a, U b) {
  a = (a > b ? b : a);
}
template <typename T, typename U>
inline void amax(T &a, U b) {
  a = (a > b ? a : b);
}
void solve() {
  int N;
  cin >> N;
  vector<int> a(N);
  set<int> s;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    s.insert(a[i]);
  }
  if (((int)s.size()) == N) {
    cout << "-1\n";
    return;
  }
  int L = 2, R = N;
  int ans = N;
  while (L <= R) {
    int m = (L + R) >> 1;
    bool ok = false;
    vector<int> freq(N + 1, 0);
    multiset<int> mt;
    for (int i = 0; i < m; ++i) mt.insert(0);
    for (int i = 0; i < m - 1; ++i) {
      mt.erase(freq[a[i]]);
      freq[a[i]]++;
      mt.insert(freq[a[i]]);
    }
    for (int i = 0; i < N - m + 1; ++i) {
      mt.erase(freq[a[i + m - 1]]);
      freq[a[i + m - 1]]++;
      mt.insert(freq[a[i + m - 1]]);
      if (*mt.begin() > *(++mt.begin())) {
        ok = true;
        break;
      }
      mt.erase(freq[a[i]]);
      freq[a[i]]--;
      if (freq[a[i]]) mt.insert(freq[a[i]]);
    }
    if (ok) {
      amin(ans, m);
      R = m - 1;
    } else {
      L = m + 1;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tests = 1;
  cin >> tests;
  while (tests--) {
    solve();
  }
}
