#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int s = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    s = (s << 3) + (s << 1) + c - '0';
    c = getchar();
  }
  return s;
}
int a[200001], last[200001], c[200001], n;
bool v[200001];
int T;
inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
  T = read();
  while (T--) {
    n = read();
    register int maxe = 0;
    register int mine = 2147483647;
    memset(c, 0, sizeof(c));
    memset(v, 0, sizeof(v));
    for (register int i = 1; i <= n; ++i) {
      a[i] = read();
      ++c[a[i]];
      maxe = max(c[a[i]], maxe);
    }
    for (register int i = 1; i <= n; ++i) {
      if (c[a[i]] != maxe) continue;
      if (v[a[i]] == 0) {
        last[a[i]] = i;
        v[a[i]] = 1;
        continue;
      } else {
        mine = min(mine, i - last[a[i]] + 1);
        last[a[i]] = i;
      }
    }
    printf("%d\n", mine == 2147483647 ? -1 : mine);
  }
  return 0;
}
