#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  cin >> TESTS;
  while (TESTS--) {
    long long int n;
    cin >> n;
    long long int a[n], arr[3][n + 1], flg = 0, k;
    for (int i = 0; i < n + 1; i++) {
      arr[0][i] = 0;
      arr[1][i] = -1;
      arr[2][i] = 2 * 100005;
    }
    for (int i = 0; i < n; i++) {
      cin >> k;
      arr[0][k]++;
      if (arr[0][k] == 1)
        arr[1][k] = i;
      else if (arr[0][k] >= 2) {
        flg = 1;
        arr[2][k] = min(arr[2][k], i - arr[1][k]);
        arr[1][k] = i;
      }
    }
    long long int mi = 2 * 2 * 100005;
    if (flg) {
      for (int i = 1; i < n + 1; i++) {
        mi = min(mi, arr[2][i]);
      }
      cout << mi + 1 << '\n';
    } else
      cout << -1 << '\n';
  }
  return 0;
}
