#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<long long, long long> map;
    long long ans = 1e9;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
      if (map.find(a[i]) == map.end()) {
        map[a[i]] = i;
        continue;
      } else {
        ans = min(ans, i - map[a[i]] + 1);
        map[a[i]] = i;
        continue;
      }
    }
    if (ans == 1e9) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}
