#include <bits/stdc++.h>
using namespace std;
static const int maxn = 2e5 + 5;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tc;
  cin >> tc;
  for (int tcase = 1; tcase <= tc; tcase++) {
    int n;
    cin >> n;
    vector<vector<int> > store(n + 1);
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      store[x].push_back(i);
    }
    if (n == 1) {
      cout << -1 << '\n';
      continue;
    }
    int mini = n + 1;
    for (int x = 1; x <= n; x++) {
      int len = store[x].size();
      if (len <= 1) continue;
      for (int i = 1; i < len; i++) {
        int sz = store[x][i] - store[x][i - 1] + 1;
        mini = min(mini, sz);
      }
    }
    if (mini == n + 1) mini = -1;
    cout << mini << '\n';
  }
}
