#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int size;
    cin >> size;
    int arr[size + 1];
    for (int j = 0; j <= size; j++) arr[j] = 0;
    int v[size + 1];
    v[0] = 0;
    for (int j = 1; j <= size; j++) {
      cin >> v[j];
      arr[v[j]]++;
    }
    int mx = 0;
    int counter = 0;
    int mx2 = 0;
    int counter2 = 0;
    for (int j = 1; j <= size; j++) {
      if (arr[j] >= counter) {
        mx2 = mx;
        counter2 = counter;
        mx = j;
        counter = arr[j];
      }
    }
    int startIndex = 0;
    int endIndex = 0;
    int minDistance = -1;
    bool first = true;
    if (counter == counter2 || counter < 2) {
      cout << minDistance << endl;
      continue;
    } else {
      for (int j = 1; j <= size; j++) {
        if (v[j] == mx && first) {
          first = false;
          startIndex = j;
        } else if (v[j] == mx) {
          endIndex = j;
          if (minDistance == -1 || minDistance > (endIndex - startIndex + 1)) {
            minDistance = endIndex - startIndex + 1;
          }
          startIndex = j;
        }
      }
      cout << minDistance << endl;
    }
  }
}
