#include <bits/stdc++.h>
using namespace std;
const int M = 2 * 100 * 1000;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n), f(M + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      f[a[i]]++;
    }
    if (n == 1 && *max_element(f.begin(), f.end()) == 1) {
      cout << "-1\n";
      continue;
    }
    int val;
    int k = *max_element(f.begin(), f.end());
    for (int i = 1; i < M + 1; i++) {
      if (f[i] == k) {
        val = i;
        break;
      }
    }
    bool fq = 0;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] == a[i + 1]) {
        cout << "2\n";
        fq = 1;
        break;
      }
    }
    if (fq) {
      continue;
    }
    int ans = INT_MAX;
    vector<int> dist;
    for (int i = 0; i < n; i++) {
      if (a[i] == val) {
        dist.push_back(i + 1);
      }
    }
    for (int i = 1; i < dist.size(); i++) {
      ans = min(ans, dist[i] - dist[i - 1]);
    }
    cout << ans + 1 << endl;
  }
}
