#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 55;
const long long MD = 1e9 + 7;
long long T, n, m;
int last[N];
int arr[N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 0; i <= n; i++) last[i] = -1;
    for (int i = 0; i < n; i++) cin >> arr[i];
    int ans = n + 1;
    for (int i = 0; i < n; i++) {
      if (last[arr[i]] != -1) ans = min(ans, i - last[arr[i]] + 1);
      last[arr[i]] = i;
    }
    if (ans == n + 1)
      cout << "-1\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
