#include <bits/stdc++.h>
using namespace std;
long long n, t, a[200001];
map<long long, long long> b, c;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    long long mi = 200002;
    b.clear();
    c.clear();
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      if (!b[a[i]]) {
        b[a[i]]++;
        c[a[i]] = i;
      } else {
        mi = min(mi, i - c[a[i]] + 1);
        c[a[i]] = i;
      }
    }
    if (n < 2 || mi == 200002)
      cout << -1 << endl;
    else
      cout << mi << endl;
  }
}
