#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    int temp;
    cin >> n;
    map<int, vector<int>> arr;
    for (int i = 0; i < n; i++) {
      cin >> temp;
      arr[temp].push_back(i);
    }
    int min_diff = INT_MAX;
    for (auto itr = arr.begin(); itr != arr.end(); itr++) {
      sort(itr->second.begin(), itr->second.end());
      for (int i = 0; i < (int)itr->second.size() - 1; i++) {
        if ((itr->second)[i + 1] - (itr->second)[i] < min_diff)
          min_diff = itr->second[i + 1] - itr->second[i];
      }
    }
    min_diff++;
    if (min_diff < 2)
      cout << -1 << endl;
    else
      cout << min_diff << endl;
  }
  return 0;
}
