#include <bits/stdc++.h>
using namespace std;
map<int, int> used;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, n, x, i;
  vector<int> v;
  set<int> st;
  set<int>::iterator it;
  cin >> t;
  while (t--) {
    int mx = -1, in;
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> x;
      used[x]++;
      if (used[x] > mx) {
        mx = used[x];
        in = x;
      }
      st.insert(x);
      v.push_back(x);
    }
    if (n == 1) {
      cout << "-1\n";
    } else {
      int cnt = 0;
      for (it = st.begin(); it != st.end(); it++) {
        if (used[*it] == mx) cnt++;
      }
      if (cnt > 1) {
        cout << "-1\n";
      } else {
        mx = 99999999;
        int p = -1;
        for (i = 0; i < n; i++) {
          if (v[i] == in) {
            if (p == -1)
              p = i;
            else {
              mx = min(mx, i - p + 1);
              p = i;
            }
          }
        }
        cout << mx << endl;
      }
    }
    st.clear();
    v.clear();
    used.clear();
  }
  return 0;
}
