#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int current;
    if (n == 1) {
      cin >> current;
      cout << -1 << endl;
    } else {
      int i;
      map<int, int> hash;
      bool repetitionExist = false;
      int minDif = 100000000;
      for (i = 0; i < n; i++) {
        cin >> current;
        if (hash.find(current) != hash.end()) {
          repetitionExist = true;
          minDif = (i - hash[current]) < minDif ? i - hash[current] : minDif;
        }
        hash[current] = i;
      }
      if (repetitionExist) {
        cout << minDif + 1 << endl;
      } else {
        cout << -1 << endl;
      }
    }
  }
}
