#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    map<long long, vector<long long>> m;
    for (long long j = 1, i = 0; j < n + 1, i < n; j++, i++) {
      m[a[i]].push_back(j);
    }
    long long res = INT_MAX;
    for (long long i = 1; i < n; i++) {
      long long temp = INT_MAX;
      if (m[i].size() > 1) {
        for (long long j = 1; j < m[i].size(); j++) {
          temp = min(temp, m[i][j] - m[i][j - 1] + 1);
        }
      }
      res = min(res, temp);
    }
    if (res != INT_MAX) {
      cout << res << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
