#include <bits/stdc++.h>
using namespace std;
const int N = 2333333;
int t, n, a[N], ans;
int head[N];
void shit() {
  ans = N;
  memset(head, 0, sizeof(head));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (head[a[i]]) ans = min(ans, i - head[a[i]] + 1);
    head[a[i]] = i;
  }
  if (ans < N)
    printf("%d\n", ans);
  else
    puts("-1");
}
int main() {
  scanf("%d", &t);
  while (t--) shit();
  return 0;
}
