#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long l = 0, r = 0, ans = INT_MAX;
    set<long long> s;
    while (l < n && r < n) {
      s.insert(a[r]);
      if (s.size() < (r - l + 1)) {
        ans = min(ans, r - l + 1);
        s.erase(a[l]);
        l++;
      } else {
        r++;
      }
    }
    ans = (ans == INT_MAX) ? -1 : ans;
    cout << ans << "\n";
  }
  return 0;
}
