#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, i, ans = INT_MAX;
    cin >> n;
    int arr[n], brr[n + 1];
    for (i = 0; i < n; i++) {
      cin >> arr[i];
    }
    for (i = 0; i <= n; i++) brr[i] = -1;
    for (i = 0; i < n; i++) {
      if (brr[arr[i]] != -1) {
        ans = min(ans, i - brr[arr[i]] + 1);
      }
      brr[arr[i]] = i;
    }
    if (ans == INT_MAX)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
}
