#include <bits/stdc++.h>
using namespace std;
int t;
int a[200010];
int last[200010];
int f[200010];
int main() {
  cin >> t;
  while (t--) {
    memset(a, 0, sizeof(a));
    memset(last, 0, sizeof(last));
    memset(f, 0x3f, sizeof(f));
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 1; i <= n; i++) {
      if (last[a[i]] == 0) {
        f[i] = f[i - 1];
      } else {
        f[i] = min(i - last[a[i]] + 1, f[i - 1]);
      }
      last[a[i]] = i;
    }
    if (f[n] > 1 && f[n] != 0x3f3f3f3f)
      cout << f[n] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
