#include <bits/stdc++.h>
using namespace std;
int main() {
  int test;
  cin >> test;
  while (test--) {
    long long i, n, ar[200005] = {0};
    map<long long, long long> mp;
    long long ele, mx = 0;
    set<long long> st;
    vector<long long> vec;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> ar[i];
      mp[ar[i]]++;
      if (mp[ar[i]] > mx) {
        mx = mp[ar[i]];
        ele = ar[i];
      }
    }
    for (i = 1; i <= n; i++) {
      if (mx == mp[ar[i]]) {
        st.insert(ar[i]);
      }
    }
    if (st.size() > 1 || n == 1)
      cout << -1 << endl;
    else {
      for (i = 1; i <= n; i++) {
        if (ar[i] == ele) {
          vec.push_back(i);
        }
      }
      long long mn = 100000000;
      for (i = 1; i < vec.size(); i++) {
        mn = min(mn, (vec[i] - vec[i - 1] + 1));
      }
      cout << mn << endl;
    }
  }
}
