#include <bits/stdc++.h>
using namespace std;
long long n, x;
int last[200009];
int main() {
  int t;
  cin >> t;
  while (t--) {
    memset(last, -1, sizeof(last));
    scanf("%lld", &n);
    long long ans = INT_MAX;
    scanf("%lld", &x);
    last[x] = 0;
    for (long long i = 1; i < n; i++) {
      scanf("%lld", &x);
      if (last[x] != -1) ans = min(ans, (i - last[x] + 1));
      last[x] = i;
    }
    if (ans > n)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
}
