#include <bits/stdc++.h>
const int oo = 0x3f3f3f3f, mod = 1e9 + 7;
using namespace std;
int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
long long OO = (1LL * oo * oo);
void K_K() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  K_K();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n + 2);
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      mp[v[i]]++;
    }
    int mx = 0, num = 0, cnt = 0;
    for (auto it : mp) {
      if (it.second > mx) {
        mx = it.second;
        num = it.first;
      }
    }
    for (auto it : mp) {
      if (it.second == mx) {
        cnt++;
      }
    }
    if (cnt > 1) {
      cout << "-1\n";
      continue;
    }
    vector<int> vv;
    for (int i = 0; i < n; i++) {
      if (v[i] == num) {
        vv.push_back(i + 1);
      }
    }
    int mn = n + 1;
    if ((int)(vv.size()) == 2) mn = (vv[1] - vv[0]) + 1;
    for (int i = 1; i < (int)(vv.size()) - 1; i++) {
      int x = abs(vv[i - 1] - vv[i]);
      x++;
      mn = min(mn, x);
      x = abs(vv[i] - vv[i + 1]);
      x++;
      mn = min(mn, x);
    }
    if (mn != n + 1)
      cout << mn << "\n";
    else
      cout << "-1\n";
  }
}
