#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long i, a[n];
    for (i = 0; i < n; i++) cin >> a[i];
    map<long long, long long> mp;
    long long j = 0;
    long long mn = n;
    bool f = 0;
    for (i = 0; i < n; i++) {
      mp[a[i]]++;
      if (mp[a[i]] >= 2) {
        f = 1;
        while (mp[a[i]] >= 2) {
          mn = min(i - j + 1, mn);
          mp[a[j]]--;
          j++;
        }
      }
    }
    if (f == 0) mn = -1;
    cout << mn << "\n";
  }
}
