#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int i, j;
  int T;
  int n, k, x, m, temp, all = 0, mini, maxi, c, y;
  string s;
  vector<int> a(200005);
  vector<pair<int, int> > b(200005);
  cin >> T;
  while (T--) {
    cin >> n;
    for (i = 0; i < n; ++i) cin >> a[i];
    for (i = 0; i < 200005; ++i) b[i] = {0, 0};
    int mini = 3000000;
    if (n == 1) {
      cout << -1 << endl;
    } else {
      for (i = 0; i < n; ++i) {
        if (b[a[i]].first == 0) {
          b[a[i]] = {1, i};
        } else {
          temp = i - b[a[i]].second + 1;
          b[a[i]].second = i;
          mini = min(mini, temp);
        }
      }
      if (mini != 3000000)
        cout << mini << endl;
      else
        cout << -1 << endl;
    }
  }
  return 0;
}
