#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    unordered_map<int, int> M;
    map<int, int> occ;
    vector<int> tab(n);
    for (int i = 0; i < n; i++) cin >> tab[i];
    if (n == 1) {
      cout << "-1" << endl;
      continue;
    }
    int ind1 = 0, ind2 = 1;
    M[tab[0]]++;
    M[tab[1]]++;
    if (tab[0] == tab[1])
      occ[2]++;
    else
      occ[1] = 2;
    int ret = n;
    bool flag = false;
    while (1) {
      if (ind1 == ind2 && ind2 == n - 1) break;
      auto it = occ.rbegin();
      if ((it->second > 1 || ind2 == ind1) && ind2 < n - 1) {
        ind2++;
        M[tab[ind2]]++;
        occ[M[tab[ind2]]]++;
        if (occ.find(M[tab[ind2]] - 1) != occ.end()) {
          occ[M[tab[ind2]] - 1]--;
          if (occ[M[tab[ind2]] - 1] == 0) occ.erase(M[tab[ind2]] - 1);
        }
      } else {
        if (it->second == 1 && ind1 < ind2) {
          flag = true;
          ret = min(ret, ind2 - ind1 + 1);
        }
        M[tab[ind1]]--;
        if (occ.find(M[tab[ind1]] + 1) != occ.end()) {
          occ[M[tab[ind1]] + 1]--;
          if (occ[M[tab[ind1]] + 1] == 0) occ.erase(M[tab[ind1]] + 1);
        }
        occ[M[tab[ind1]]]++;
        ind1++;
      }
    }
    if (flag) {
      cout << ret << endl;
    } else
      cout << "-1" << endl;
  }
  return 0;
}
