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