#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)(10e9) + 1;
int main() {
  ios_base::sync_with_stdio(false);
  int t, n, ans;
  cin >> t;
  while (t--) {
    cin >> n;
    int a[n + 1];
    for (int i = 0; i < (int)n; i++) cin >> a[i];
    int e[n + 1];
    memset(e, -1, sizeof(e));
    ans = n + 5;
    for (int i = 0; i < n; i++) {
      if (e[a[i]] != -1) {
        ans = min(ans, i - e[a[i]] + 1);
      }
      e[a[i]] = i;
    }
    if (ans > n)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
