#include <bits/stdc++.h>
using namespace std;
int occ(int val, int start, int end, int arr[]) {
  int cnt = 0;
  for (int k = start; k <= end; k++) {
    if (arr[k] == val) cnt++;
  }
  return cnt;
}
int main() {
  int t;
  cin >> t;
  for (int m = 0; m < t; m++) {
    int n, s = 0, e = 1;
    long int ans = 100000;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    if (n < 2) {
      ans = -1;
      cout << ans << endl;
      continue;
    }
    while (e < n) {
      if (occ(arr[s], s, e, arr) > occ(arr[s + 1], s, e, arr)) {
        if (ans > e - s) ans = e - s + 1;
        s = e;
        e = s + 1;
      } else
        ++e;
      if (occ(arr[s], s, e, arr) == occ(arr[s + 1], s, e, arr) &&
          arr[s] == arr[s + 1])
        ans = 2;
    }
    ans == 100000 ? cout << -1 << endl : cout << ans << endl;
  }
  return 0;
}
