#include <bits/stdc++.h>
using namespace std;
int main() {
  long long test;
  cin >> test;
  while (test--) {
    long long n, i, j, k = 0;
    cin >> n;
    long long ara[n], check[200000];
    memset(check, 0, sizeof(check));
    for (i = 0; i < n; i++) {
      cin >> ara[i];
      check[ara[i]]++;
    }
    long long mx = check[0], ans = 0;
    for (i = 1; i < 200000; i++) {
      if (check[i] > mx) {
        mx = check[i];
        ans = i;
      }
    }
    for (i = 1; i < 200000; i++) {
      if (check[i] == mx) {
        k++;
      }
    }
    if (k >= 2) {
      cout << -1 << endl;
      continue;
    }
    long long l = 0, pos = 0, cc = 0, dj = 1000000000;
    for (i = 0; i < n; i++) {
      if (ara[i] == ans && l == 0) {
        pos = i;
        l = 1;
      } else if (ara[i] == ans) {
        if (cc < dj) {
          dj = cc;
          cc = 0;
        }
        cc = 0;
      } else if (l == 1) {
        cc++;
      }
    }
    if (dj == 1000000000)
      cout << -1 << endl;
    else
      cout << dj + 2 << endl;
  }
  return 0;
}
