#include <bits/stdc++.h>
using namespace std;
long long freq[200005], a[200005];
vector<long long> adjlist[200005];
int main() {
  long long tc, n, i, mx_fr, mx_val = 0;
  scanf("%lld", &tc);
  while (tc--) {
    long long x, ans = 100000000;
    mx_fr = 0;
    mx_val = 0;
    scanf("%lld", &n);
    for (i = 1; i <= n; i++) freq[i] = 0;
    for (i = 0; i < n; i++) {
      scanf("%lld", a + i);
      freq[a[i]]++;
      mx_fr = max(freq[a[i]], mx_fr);
    }
    for (i = 0; i < n; i++) {
      if (freq[a[i]] == mx_fr) {
        adjlist[a[i]].push_back(i);
      }
    }
    for (i = 1; i <= n; i++) {
      if (freq[i] == mx_fr) {
        mx_val++;
        if (mx_val > 1) break;
        long long sz = (long long)adjlist[i].size();
        for (long long j = 1; j < sz; j++) {
          ans = min(ans, adjlist[i][j] - adjlist[i][j - 1]);
        }
      }
    }
    if (mx_fr == 1)
      printf("-1\n");
    else
      printf("%lld\n", ans + 1);
    for (i = 1; i <= n; i++) adjlist[i].clear();
  }
  return 0;
}
