#include <bits/stdc++.h>
using namespace std;
long long int n, m, ar[1000005], sum, t, tut[1000005];
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    sum = INT_MAX;
    memset(tut, 0, sizeof(tut));
    cin >> n;
    for (long long int i = 1; i <= n; i++) cin >> ar[i];
    for (long long int i = 1; i <= n; i++) {
      if (tut[ar[i]]) sum = min(sum, i - tut[ar[i]]);
      tut[ar[i]] = i;
    }
    if (sum == INT_MAX) sum = -2;
    cout << sum + 1 << "\n";
  }
}
