#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long n, ans = LLONG_MAX;
    scanf("%lld", &n);
    long long a[n + 5];
    vector<long long> v[n + 5];
    for (long long i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
      v[a[i]].push_back(i);
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 1; j < v[a[i]].size(); j++)
        ans = min(ans, v[a[i]][j] - v[a[i]][j - 1]);
    }
    if (ans != LLONG_MAX)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
}
