#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, a, b, i;
    cin >> n;
    map<int, int> arr;
    int m = INT_MAX, store;
    for (i = 1; i <= n; i++) {
      cin >> a;
      if (arr[a] > 0) {
        m = min(m, i - arr[a]);
      }
      arr[a] = i;
    }
    (m != INT_MAX) ? cout << m + 1 : cout << -1;
    cout << endl;
  }
  return 0;
}
