#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    long long int diff = INT_MAX, d;
    memset(a, -1, n * sizeof(int));
    for (long long int i = 0; i < n; i++) {
      cin >> d;
      d--;
      if (a[d] == -1) {
        a[d] = i;
      } else {
        if (i - a[d] <= diff) {
          diff = i - a[d];
        }
        a[d] = i;
      }
    }
    if (diff == INT_MAX || n == 1) {
      cout << "-1"
           << "\n";
    } else
      cout << ++diff << "\n";
  }
  return 0;
}
