#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    long long int arr[n + 1];
    map<long long int, pair<long long int, long long int> > mp;
    for (int i = 1; i <= n; ++i) {
      cin >> arr[i];
      if (mp[arr[i]].first == 0) {
        mp[arr[i]].first = i;
        mp[arr[i]].second = 1000000;
      } else {
        mp[arr[i]].second = min(mp[arr[i]].second, (i - mp[arr[i]].first));
        mp[arr[i]].first = i;
      }
    }
    map<long long int, pair<long long int, long long int> >::iterator itr =
        mp.begin();
    long long int maxi = 10000000000;
    for (; itr != mp.end(); itr++) {
      maxi = min((itr->second).second, maxi);
    }
    if (maxi == 1000000) {
      cout << -1 << endl;
      ;
      continue;
    }
    cout << maxi + 1 << endl;
  }
}
