#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    int ans = INT_MAX;
    int hash[n + 1];
    memset(hash, -1, sizeof(hash));
    for (int i = 0; i < n; i++) {
      if (hash[a[i]] != -1) {
        ans = min(ans, i - hash[a[i]] + 1);
        hash[a[i]] = i;
      } else
        hash[a[i]] = i;
    }
    if (ans != INT_MAX)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
