#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[n];
    for (long long int i = 0; i < n; i++) cin >> a[i];
    vector<pair<long long int, long long int> > b(n);
    long long int ans = n + 1, temp = 0;
    for (long long int i = 0; i < n; i++) b[i].first = 0;
    for (long long int i = 0; i < n; i++) {
      if (b[a[i] - 1].first == 0) {
        b[a[i] - 1].first = 1;
        b[a[i] - 1].second = i + 1;
      } else {
        if (i + 1 - b[a[i] - 1].second < ans)
          ans = i + 1 - b[a[i] - 1].second + 1;
        b[a[i] - 1].second = i + 1;
      }
    }
    if (n == 1 || ans == n + 1)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
}
