#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, n, m, l, t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long a[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    map<long long, vector<long long> > ma;
    for (i = 0; i < n; i++) {
      ma[a[i]].push_back(i);
    }
    long long ans = INT_MAX;
    for (auto i : ma) {
      if (i.second.size() >= 2) {
        for (auto j = 1; j < i.second.size(); j++) {
          ans = min(i.second[j] - i.second[j - 1] + 1, ans);
        }
      }
    }
    if (ans == INT_MAX)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
