#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
long long q, n, a[N], b[N], f[N];
int main() {
  cin >> q;
  for (long long k = 1; k <= q; k++) {
    cin >> n;
    long long ma = -1;
    long long mi = 1e9;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      ma = max(ma, a[i]);
      mi = min(mi, a[i]);
    }
    if (n == 1) {
      cout << "-1\n";
      continue;
    }
    for (long long i = mi; i <= ma; i++) {
      b[i] = 0;
      f[i] = 1e9 + 10;
    }
    for (long long i = 1; i <= n; i++) {
      long long j = b[a[i]];
      if (j) {
        f[a[i]] = min(f[a[i]], i - j + 1);
      }
      b[a[i]] = i;
    }
    long long res = 1e9 + 10;
    for (long long i = mi; i <= ma; i++) {
      res = min(res, f[i]);
    }
    if (res != 1e9 + 10) {
      cout << res << "\n";
    } else {
      cout << "-1\n";
    }
  }
}
