#include <bits/stdc++.h>
using namespace std;
struct Values {
  int min;
  int max;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int minDif = 100000000;
    int current;
    if (n == 1) {
      cin >> current;
      cout << -1 << endl;
    } else {
      int i = 0;
      map<int, Values> hash;
      while (n--) {
        cin >> current;
        if (hash.find(current) != hash.end()) {
          Values temp = hash[current];
          if ((temp.max - temp.min) > (i - temp.max)) {
            Values newValues;
            newValues.max = i;
            newValues.min = temp.max;
            hash.at(current) = newValues;
            if ((newValues.max - newValues.min) < minDif) {
              minDif = newValues.max - newValues.min;
            }
          }
        } else {
          Values tmp;
          tmp.min = -1000;
          tmp.max = i;
          hash.insert(pair<int, Values>(current, tmp));
        }
        i++;
      }
      cout << minDif + 1 << endl;
    }
  }
}
