#include <bits/stdc++.h>
using namespace std;
int t, n;
map<int, vector<int>> _map;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    _map.clear();
    for (int i = 0; i < n; i++) {
      int tp;
      scanf("%d", &tp);
      _map[tp].push_back(i);
    }
    int maxVal = -1;
    int dominator = -1;
    int cnt = 0;
    for (auto v : _map) {
      int s = v.second.size();
      if (maxVal < s) {
        maxVal = s;
        dominator = v.first;
        cnt = 1;
      } else if (maxVal == s) {
        cnt++;
      }
    }
    if (maxVal == 1) {
      printf("-1\n");
      continue;
    }
    int ans = 0x3f3f3f3f;
    vector<int> tmp = _map[dominator];
    for (int i = 0; i < tmp.size() - 1; i++) {
      ans = min(ans, tmp[i + 1] - tmp[i] + 1);
    }
    printf("%d\n", ans);
  }
  return 0;
}
