#include <bits/stdc++.h>
using namespace std;
long long n, x, a, b, ans;
map<long long, long long> m;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    cin >> n;
    ans = n + 1;
    m.clear();
    for (long long i = (0), i__end = (n); i < i__end; i++) {
      cin >> x;
      if (m.count(x)) ans = min(ans, i - m[x] + 1);
      m[x] = i;
    }
    if (ans == n + 1)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
