#include <bits/stdc++.h>
const int long long N = 10e8 + 7;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    map<int, int> mm;
    vector<int> v[n + 5];
    for (int i = 0; i < n; i++) cin >> arr[i];
    int l = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (mm[arr[i]] >= 1) {
        int pos = v[arr[i]].back();
        l = min(l, i - pos + 1);
      } else
        mm[arr[i]]++;
      v[arr[i]].push_back(i);
    }
    if (l != INT_MAX)
      cout << l << '\n';
    else
      cout << -1 << '\n';
  }
}
