#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);
}
bool check(int m, vector<int> &a) {
  int N = ((int)a.size());
  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())) {
      return 1;
    }
    mt.erase(freq[a[i]]);
    freq[a[i]]--;
    if (freq[a[i]]) mt.insert(freq[a[i]]);
  }
  return 0;
}
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 (R - L > 2) {
    int m = (L + R) >> 1;
    do {
    } while (0);
    if (check(m, a)) {
      amin(ans, m);
      R = m;
    } else {
      L = m;
    }
  }
  for (int m = L; m <= R; ++m)
    if (check(m, a)) amin(ans, m);
  if (check(2, a)) {
    cout << 2 << '\n';
    return;
  }
  cout << (check(ans, a) ? ans : (check(2, a) ? 2 : -1)) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tests = 1;
  cin >> tests;
  while (tests--) {
    solve();
  }
}
