#include <bits/stdc++.h>
using namespace std;
int t, n, a[200005], d[200005], mn, pr[200005];
int main() {
  cin >> t;
  while (t--) {
    mn = 1000000009;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      d[i] = 1000000009;
      pr[i] = -1000000009;
    }
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      d[i] = min(d[i], i - pr[a[i]] + 1);
      pr[a[i]] = i;
      mn = min(d[i], mn);
    }
    if (mn > n)
      cout << -1 << '\n';
    else
      cout << mn << '\n';
  }
}
