#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> sur = {{1, 0},   {0, -1}, {0, 1},  {-1, 0},
                              {-1, -1}, {1, 1},  {1, -1}, {-1, 1}};
bool bounds(int i, int j, int n, int m) {
  return i >= 0 && i <= n && j >= 0 && j <= m;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<long long> d(n);
    map<long long, long long> o;
    for (int i = 0; i < n; i++) {
      cin >> d[i];
      o[d[i]]++;
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    long long mostOccur = LLONG_MIN;
    long long mostNum = LLONG_MIN;
    for (auto x : o) {
      if (x.second > mostNum) {
        mostOccur = max(mostOccur, x.first);
        mostNum = x.second;
      }
    }
    long long prevIndex = 0;
    for (int i = 0; i < n; i++) {
      if (d[i] == mostOccur) {
        prevIndex = i;
        break;
      }
    }
    long long maxLength = LLONG_MAX;
    for (long long i = prevIndex + 1; i < n; i++) {
      if (d[i] == mostOccur) {
        long long length = i - prevIndex + 1;
        maxLength = min(maxLength, length);
        prevIndex = i;
      }
    }
    if (maxLength == LLONG_MAX) {
      cout << -1 << endl;
    } else {
      cout << maxLength << endl;
    }
  }
  return 0;
}
