#include <bits/stdc++.h>
using namespace std;
int n, t;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    int ans = 1e9;
    map<int, int> C, L;
    int x;
    for (int i = 1; i <= n; i++) {
      cin >> x;
      if (C[x] == 0)
        C[x] = i;
      else {
        L[x] = C[x], C[x] = i;
        ans = min(ans, i - L[x] + 1);
      }
    }
    if (ans == 1e9) ans = -1;
    cout << ans << "\n";
  }
}
