#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7, maxn = 2e5 + 20;
int n, k, m, x, y, q, a[maxn], ps[maxn], cnt[maxn << 2];
vector<int> v[maxn];
int ans;
int32_t main() {
  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> n;
    vector<int> v[n + 1];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      v[a[i]].push_back(i);
    }
    ans = 1e9;
    for (int i = 1; i <= n; i++) {
      if (v[i].size() > 1)
        for (int t = 0; t < v[i].size() - 1; t++) {
          ans = min(ans, v[i][t + 1] - v[i][t] + 1);
        }
    }
    if (ans == 1e9)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
