#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long n, i, j, ar[100005], x, a, b, y;
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    map<long long, long long> m;
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      scanf("%lld", &ar[i]);
      m[ar[i]]++;
    }
    long long mx = -1;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      if (mx < itr->second) mx = itr->second;
    }
    long long cnt = 0, raj;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      if (itr->second == mx) {
        cnt++;
        raj = itr->first;
      }
    }
    if (cnt > 1)
      cout << "-1\n";
    else {
      for (long long i = 0; i < n; i++) {
        if (ar[i] == raj) v.push_back(i);
      }
      long long mn = mod;
      for (long long i = 1; i < v.size(); i++) {
        if (v[i] - v[i - 1] < mn) mn = v[i] - v[i - 1];
      }
      if (mn == mod)
        cout << "-1\n";
      else {
        cout << (mn + 1);
        printf("\n");
      }
    }
  }
}
