#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int temp = power(a, b >> 1);
  if (b & 1)
    return (a * temp * temp);
  else
    return temp * temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int arr[n];
    map<long long int, long long int> m;
    for (long long int i = 0; i < n; i++) {
      cin >> arr[i];
      m[arr[i]] = -2;
    }
    map<long long int, long long int> minval;
    for (long long int i = 0; i < n; i++) {
      minval[arr[i]] = INT_MAX;
    }
    for (long long int i = 0; i < n; i++) {
      if (m[arr[i]] == -2) {
        m[arr[i]] = i;
      } else {
        minval[arr[i]] = min(minval[arr[i]], i - m[arr[i]] + 1);
        m[arr[i]] = i;
      }
    }
    long long int ans = INT_MAX;
    for (long long int i = 0; i < n; i++) {
      ans = min(ans, minval[arr[i]]);
    }
    for (long long int i = 0; i < n; i++) {
    }
    if (ans == INT_MAX)
      cout << -1;
    else
      cout << ans;
    cout << "\n";
  }
  return 0;
}
