#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e7 + 7;
long long arr[200009];
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, flag = 0, maxSet = 0, maxVal = 0, ind, ans = INT_MAX;
    map<long long, long long> mp1;
    stack<long long> st;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      mp1[arr[i]]++;
    }
    if (n < 2)
      cout << -1 << endl;
    else {
      for (auto it = mp1.begin(); it != mp1.end(); it++) {
        if (it->second == maxSet)
          flag = 1;
        else if (it->second > maxSet) {
          flag = 0;
          maxSet = it->second;
          maxVal = it->first;
        }
      }
      if (flag)
        cout << -1 << endl;
      else {
        for (int i = 0; i < n; i++) {
          if (arr[i] == maxVal) st.push(i);
        }
        ind = st.top();
        st.pop();
        while (!st.empty()) {
          ans = min(ans, (ind - st.top()));
          ind = st.top();
          st.pop();
        }
        cout << (ans + 1) << endl;
      }
    }
  }
}
