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