#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, a, b, i;
    cin >> n;
    map<int, int> arr;
    vector<pair<int, int>> vcc;
    int m = INT_MIN, store;
    for (i = 0; i < n; i++) {
      cin >> a;
      vcc.push_back(make_pair(a, i));
      arr[a]++;
      if (arr[a] > m) {
        m = arr[a];
        store = a;
      }
    }
    int cou = 0;
    for (auto x : arr) {
      if (m == x.second) cou++;
    }
    int start, stop, counter = 0;
    sort(vcc.begin(), vcc.end());
    int res;
    m = INT_MAX;
    if (cou < 2) {
      for (i = 0; i < n; i++) {
        if (vcc[i].first == store) {
          if (counter > 0) {
            res = vcc[i].second - vcc[i - 1].second;
            m = min(m, res);
          }
          counter++;
        }
      }
    }
    (m != INT_MAX) ? cout << m + 1 : cout << -1;
    cout << endl;
  }
  return 0;
}
