#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    map<long long, long long> mp;
    vector<long long> v;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (mp[a[i]] == 0) {
        mp[a[i]] = i + 1;
      } else {
        long long push = i + 1 - mp[a[i]];
        v.push_back(push + 1);
        mp[a[i]] = i + 1;
      }
    }
    if (v.size() == 0) {
      cout << "-1\n";
    } else {
      sort(v.begin(), v.end());
      cout << v[0] << "\n";
    }
  }
}
