#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    set<int> s;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      s.insert(arr[i]);
    }
    if (n == 1 || s.size() == n) {
      cout << -1 << endl;
    } else {
      int brr[200005] = {0};
      int ans = n;
      for (int i = 1; i < n; i++) {
        if (brr[arr[i]] == 0 && arr[0] != arr[i]) {
          brr[arr[i]] = i;
        } else {
          ans = min(ans, i - brr[arr[i]] + 1);
          brr[arr[i]] = i;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
