#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int maxN = (int)3e5 + 100;
int a[maxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    int ans = INT_MAX;
    unordered_map<int, int> umap;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      if (umap.find(tmp) == umap.end()) {
        umap[tmp] = i;
      } else {
        ans = min(ans, 1 + i - umap[tmp]);
        umap[tmp] = i;
      }
    }
    if (ans != INT_MAX)
      cout << ans << '\n';
    else
      cout << -1 << '\n';
  }
}
