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