#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
using namespace std;
int pr[2 * 100500], m[2 * 100500], a[2 * 100500], n, i, q;
int main() {
  ios_base::sync_with_stdio(0);
  iostream::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> q;
  while (q--) {
    cin >> n;
    int ans = 1e9;
    for (i = 1; i <= n; i++) cin >> a[i], m[i] = -1, pr[i] = 0;
    pr[0] = 0;
    for (i = 1; i <= n; i++) {
      int x = a[i];
      pr[i] += pr[i - 1];
      if (m[x] == -1)
        m[x] = i;
      else {
        int prev = m[x];
        if (pr[i] - pr[prev - 1] - 1 <= 0) ans = min(ans, i - prev + 1);
        m[x] = i;
        pr[i + 1]++;
      }
    }
    if (ans != 1e9)
      cout << ans << endl;
    else
      cout << "-1" << endl;
  }
}
