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