#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    memset(a, 0, sizeof(a));
    int n, x, res = 2147483647;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> x;
      if (a[x]) res = min(res, i - a[x] + 1);
      a[x] = i;
    }
    if (n == 1 || res == 2147483647) res = -1;
    cout << res << "\n";
  }
  return 0;
}
