#include <bits/stdc++.h>
using namespace std;
int a[200005];
int loc[200005];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, MIN = 200005;
    scanf("%d", &n);
    memset(loc, 0, sizeof(loc));
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      if (loc[a[i]]) MIN = min(MIN, i - loc[a[i]] + 1);
      loc[a[i]] = i;
    }
    if (MIN == 200005) MIN = -1;
    printf("%d\n", MIN);
  }
  return 0;
}
