#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  int n, a;
  for (int i = 0; i < t; i++) {
    cin >> n;
    vector<int> v(200005, 0);
    vector<int> oqq(200005, 0);
    for (int j = 0; j < n; j++) {
      cin >> a;
      v[j] = a;
      oqq[a]++;
    }
    if (n < 2) {
      cout << -1 << endl;
      continue;
    }
    int max = 0;
    int elem;
    for (int j = 0; j < 200005; j++) {
      if (oqq[j] > max) {
        max = oqq[j];
        elem = j;
      }
    }
    vector<int> index;
    for (int k = 0; k < n; k++) {
      if (v[k] == elem) {
        index.push_back(k);
      }
    }
    sort(index.begin(), index.end());
    int res = INT_MAX;
    for (int k = 0; k < index.size() - 1; k++) {
      if (index[k + 1] - index[k] + 1 < res) {
        res = index[k + 1] - index[k] + 1;
      }
    }
    cout << res << endl;
  }
  return 0;
}
