#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int mx = n + 1, x;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
      cin >> x;
      if (mp.find(x) != mp.end()) {
        mx = min(mx, i - mp[x] + 1);
        mp[x] = i;
      } else {
        mp.insert(make_pair(x, i));
      }
    }
    if (n <= 2) {
      cout << -1 << endl;
    } else {
      cout << mx << endl;
    }
  }
}
