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