#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    unordered_map<long long, long long> vis;
    long long ans = LLONG_MAX;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      if (vis.find(a[i]) != vis.end()) {
        ans = min(ans, i - vis[a[i]] + 1);
      }
      vis[a[i]] = i;
    }
    if (ans == LLONG_MAX) {
      ans = -1;
    }
    cout << ans << endl;
  }
}
