#include <bits/stdc++.h>
long long a[300000], T, n;
long long ok[300000];
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cin >> n;
    memset(ok, 0, sizeof(ok));
    long long minn = 0x7fffffffffffffff;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (ok[a[i]] == 0) {
        ok[a[i]] = i;
      } else {
        minn = min(minn, i - ok[a[i]] + 1);
        ok[a[i]] = i;
      }
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    cout << minn << endl;
  }
  return 0;
}
