#include <bits/stdc++.h>
using namespace std;
int main() {
  int T, n, t, glob_min;
  map<int, int> m;
  cin >> T;
  while (T--) {
    m.clear();
    cin >> n;
    glob_min = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> t;
      if (m[t] == 0)
        m[t] = i;
      else {
        if (glob_min > i - m[t]) {
          glob_min = i - m[t];
        }
        m[t] = i;
      }
    }
    if (n < 2) {
      cout << -1 << endl;
      continue;
    }
    cout << glob_min + 1 << endl;
  }
}
