#include <bits/stdc++.h>
const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;
template <typename T>
inline void read(T& x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
void solve() {
  int n;
  cin >> n;
  map<int, vector<int> > m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]].push_back(i);
  }
  if (n == m.size()) {
    cout << -1 << endl;
    return;
  }
  int ans = inf;
  for (auto it : m) {
    for (int i = 1; i < it.second.size(); i++) {
      ans = min(ans, it.second[i] - it.second[i - 1]);
    }
  }
  cout << ans + 1 << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
