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