#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    unordered_map<int, int> left;
    unordered_map<int, int> count;
    int min = n;
    for (int i = 0; i < n; i++) {
      if (count[arr[i]] == 0) {
        count[arr[i]]++;
        left[arr[i]] = i;
      } else {
        if (i - left[arr[i]] + 1 < min && i - left[arr[i]] + 1 >= 2) {
          min = i - left[arr[i]] + 1;
        }
        left[arr[i]] = i;
      }
    }
    if (min >= 2)
      cout << min << endl;
    else
      cout << "-1\n";
  }
}
