#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, i, j;
    cin >> n;
    vector<long long> a(n);
    map<long long, vector<long long>> mp;
    for (i = 0; i < n; i++) cin >> a[i], mp[a[i]].push_back(i);
    long long ans = 1e10, f = 0;
    for (auto p : mp) {
      if (int((p.second).size()) > 1) {
        f = 1;
        for (i = 1; i < int((p.second).size()); i++) {
          ans = min(ans, p.second[i] - p.second[i - 1] + 1);
        }
      }
    }
    if (f)
      cout << ans << "\n";
    else
      cout << "-1\n";
  }
}
