#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, ans;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<int> last_idx(n + 1, -1);
    ans = n + 5;
    for (int i = 0; i < n; i++) {
      if (last_idx[a[i]] != -1) ans = min(ans, i - last_idx[a[i]] + 1);
      last_idx[a[i]] = i;
    }
    if (ans > n) ans = -1;
    cout << ans << "\n";
  }
  return 0;
}
