#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int n;
  while (t--) {
    cin >> n;
    int i, a, min = 100000;
    map<int, int> m;
    if (n == 1) {
      cin >> a;
      cout << "-1" << endl;
    } else if (n == 2) {
      int b;
      cin >> a >> b;
      if (a == b)
        cout << "2" << endl;
      else
        cout << "-1" << endl;
    } else {
      for (i = 0; i < n; i++) {
        cin >> a;
        if (m.find(a) == m.end()) {
          m[a] = i;
        } else {
          int k = i - m[a] + 1;
          if (min > k) min = k;
          m[a] = i;
        }
      }
      if (min < 100000) {
        cout << min << endl;
      } else
        cout << "-1" << endl;
    }
  }
  return 0;
}
