#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long N = 4e5 + 3;
const long long md = 1e9 + 7;
const long long inf = 1e18;
const int MAX_CHARS = 256;
string first(string str);
int main() {
  long long t;
  cin >> t;
  while (t--) {
    std::vector<long long> arr1;
    long long n, mini = INT_MAX, maxi = INT_MIN, diff, z;
    cin >> n;
    long long arr[n];
    long long freq[200001];
    memset(freq, 0, sizeof(freq));
    for (long long i = 0; i < (n); i++) {
      cin >> arr[i];
      freq[arr[i]]++;
    }
    if (n > 1) {
      for (long long i = 0; i < (200001); i++) {
        if (freq[i] > maxi) {
          maxi = freq[i];
          z = i;
        }
      }
      for (long long i = 0; i < n; i++) {
        if (arr[i] == z) {
          arr1.push_back(i);
        }
      }
      for (int i = 0; i < arr1.size() - 1; i++) {
        diff = arr1[i + 1] - arr1[i] + 1;
        mini = min(mini, diff);
      }
      cout << mini << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
