#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    vector<vector<int> > a(n, vector<int>()), calc(n + 1);
    set<int> digs;
    for (int j = 0; j < n; j++) {
      int dig;
      cin >> dig;
      calc[dig].push_back(j);
      digs.insert(dig);
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    int mdig = 0, mzn = 0;
    set<int>::iterator it = digs.begin();
    for (it; it != digs.end(); it++) {
      if (calc[*it].size() > mzn) {
        mzn = calc[*it].size();
        mdig = *it;
      }
    }
    it = digs.begin();
    int mzn1 = 0, mdig1 = 0;
    for (it; it != digs.end(); it++) {
      if (calc[*it].size() > mzn1 && *it != mdig) {
        mzn1 = calc[*it].size();
        mdig1 = *it;
      }
    }
    if (mzn == mzn1) {
      cout << -1 << endl;
      continue;
    }
    int ans = 1e9 + 7;
    for (int i = 0; i < calc[mdig].size(); i++) {
      for (int j = i + 1; j < calc[mdig].size(); j++) {
        if (calc[mdig][j] - calc[mdig][i] < ans) {
          ans = calc[mdig][j] - calc[mdig][i];
          break;
        }
      }
    }
    cout << ans + 1 << endl;
  }
}
