#include <bits/stdc++.h>
using namespace std;
const int maximum = 2e5 + 5;
int n;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long tc;
  cin >> tc;
  while (tc--) {
    cin >> n;
    vector<int> array1(n);
    vector<int> check(n + 1, -1);
    int answer = n + 5;
    for (int i = 0; i < n; i++) {
      cin >> array1[i];
    }
    if (array1.size() == 1) {
      cout << -1 << "\n";
    } else {
      for (int i = 0; i < n; i++) {
        if (check[array1[i]] != -1) {
          answer = min(answer, i - check[array1[i]] + 1);
        }
        check[array1[i]] = i;
      }
      if (answer > n) {
        answer = -1;
      }
      cout << answer << "\n";
    }
  }
  return 0;
}
