#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, _, a[maxn];
int main() {
  cin >> _;
  while (_--) {
    cin >> n;
    int ans = n + 1;
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (mp.count(a[i])) {
        ans = min(ans, i - mp[a[i]] + 1);
      }
      mp[a[i]] = i;
    }
    if (ans == (n + 1)) {
      cout << -1 << endl;
    } else
      cout << ans << endl;
  }
  return 0;
}
