#include <bits/stdc++.h>
auto clk = clock();
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
const int dx8[] = {-1, -1, -1, 0, 1, 1, 1, 0},
          dy8[] = {-1, 0, 1, 1, 1, 0, -1, -1};
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long Gcd(long long a, long long b) {
  if (a % b == 0) return b;
  return Gcd(b, a % b);
}
void printvector(std::vector<long long> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << "" << endl;
}
void printarray(long long a[], long long n) {
  for (long long i = 0; i < n; ++i) {
    cout << a[i] << " ";
  }
  cout << "" << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    long long dp[n + 1];
    memset(dp, -1, sizeof(dp));
    long long ans = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (dp[arr[i]] != -1) ans = min(ans, i - dp[arr[i]] + 1);
      dp[arr[i]] = i;
    }
    if (ans == INT_MAX) ans = -1;
    cout << ans << endl;
  }
  return 0;
}
