#include <bits/stdc++.h>
using namespace std;
void checkpoint1() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  checkpoint1();
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    set<long long> second;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      second.insert(a[i]);
    }
    if (second.size() == n)
      cout << -1 << "\n";
    else {
      map<long long, long long> m;
      long long j = 0, ans = 1000000000000000000;
      for (long long i = 0; i < n; i++) {
        m[a[i]]++;
        if (m[a[i]] > 1) {
          while (m[a[i]] != 1) {
            m[a[j]]--;
            j++;
          }
          ans = min(ans, i - j + 2);
        }
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
