#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long ans = n + 1;
    map<long long, long long> m;
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      if (m.count(x)) {
        ans = min(i - m[x] + 1, ans);
        m[x] = i;
      } else
        m[x] = i;
    }
    if (ans == n + 1) ans = -1;
    cout << ans << "\n";
  }
}
