#include <bits/stdc++.h>
using namespace std;
int in[300005];
int mp[300005];
int cnt[300005];
bool chk(int x, int n) {
  memset(mp, 0, (n << 2) + 10);
  memset(cnt, 0, (n << 2) + 10);
  int nowmx = 0;
  for (int i = 1; i <= x; i++) {
    ++cnt[++mp[in[i]]];
    nowmx = max(nowmx, mp[in[i]]);
  }
  if (cnt[nowmx] == 1) return true;
  for (int i = x + 1; i <= n; i++) {
    cnt[mp[in[i - x]]--]--;
    while (nowmx > 0 && cnt[nowmx] == 0) nowmx--;
    ++cnt[++mp[in[i]]];
    nowmx = max(nowmx, mp[in[i]]);
    if (cnt[nowmx] == 1) return true;
  }
  return false;
}
int32_t main() {
  {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
  };
  int T, n;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> in[i];
    if (n == 1) {
      cout << "-1\n";
      continue;
    }
    int l = 2, r = n;
    while (l != r) {
      int m = l + r >> 1;
      if (chk(m, n))
        r = m;
      else
        l = m + 1;
    }
    if (!chk(l, n))
      cout << -1 << '\n';
    else
      cout << l << '\n';
  }
  return 0;
}
