#include <bits/stdc++.h>
using namespace std;
int T, n, i, v[200005], f[200005], last[200005];
int main() {
  cin >> T;
  for (; T--;) {
    cin >> n;
    for (i = 1; i <= n; i++) cin >> v[i];
    for (i = 1; i <= 200005; i++) {
      f[i] = 0;
      last[i] = 0;
    }
    int maxim = 0;
    int lungmin = n;
    for (i = 1; i <= n; i++) {
      f[v[i]]++;
      if (f[v[i]] == 1) last[v[i]] = i;
      if (f[v[i]] == 2) {
        if (last[v[i]] != 0) lungmin = min(lungmin, i - last[v[i]] + 1);
        last[v[i]] = i;
        f[v[i]]--;
      }
    }
    if (lungmin <= 1)
      cout << -1 << "\n";
    else
      cout << lungmin << "\n";
  }
  return 0;
}
