#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1);
const long long mod = 1e9 + 7;
const long long N = 2e5 + 500;
const long long INF = 1e18 + 7;
void file() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int t, a[N], u[N];
int main() {
  file();
  boost();
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v[n + 1];
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      v[a[i]].push_back(i);
    }
    int ans = mod;
    for (int i = 1; i <= n; ++i) {
      if (v[i].size() >= 2) {
        for (int j = 1; j < v[i].size(); ++j) {
          ans = min(ans, v[i][j] - v[i][j - 1] + 1);
        }
      }
    }
    if (ans == mod)
      cout << "-1\n";
    else
      cout << ans << endl;
  }
}
