#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + 19;
int n, a[maxN], cnt[maxN];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int res = 3e5 + 19;
    memset(cnt, 0, sizeof(cnt));
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      if (cnt[a[i]] != 0) {
        res = min(res, i - cnt[a[i]] + 1);
        cnt[a[i]] = i;
      }
      if (cnt[a[i]] == 0) cnt[a[i]] = i;
    }
    cout << (res == 3e5 + 19 ? -1 : res) << "\n";
  }
  return 0;
}
