#include <bits/stdc++.h>
using namespace std;
const int N_max = (1 << 18);
int t, n, a = 100000000, x;
int used[N_max];
int ans[N_max];
int main() {
  cin >> t;
  for (int i = 0; i < t; ++i) {
    memset(used, 0, sizeof(used));
    memset(ans, 0, sizeof(ans));
    cin >> n;
    for (int j = 1; j <= n; ++j) {
      cin >> x;
      if (used[x] == 0)
        used[x] = j;
      else
        ans[x] = min(ans[x], j - used[x] + 1), used[x] = j;
    }
    for (int j = 0; j <= 200000; ++j)
      if (ans[j] != 0) a = min(a, ans[j]);
    if (a == 100000000)
      cout << -1 << endl;
    else
      cout << a << endl;
  }
  return 0;
}
