#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    map<int, int> mymap;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      mymap[arr[i]]++;
    }
    int ocr = INT_MIN;
    int x = 0;
    for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++) {
      if (it->second >= ocr) {
        ocr = it->second;
        x = it->first;
      }
    }
    int numOfDom = 0;
    for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++) {
      if (it->second == ocr) {
        numOfDom++;
      }
    }
    if (n < 2) {
      cout << -1 << endl;
      continue;
    }
    int cnt = INT_MAX;
    int lastP = -1;
    for (int i = 0; i < n; i++) {
      if (arr[i] == x) {
        if (lastP == -1)
          lastP = i;
        else
          cnt = min(cnt, (i - lastP));
        lastP = i;
      }
    }
    cout << cnt + 1 << endl;
  }
  return 0;
}
