#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int k, n, a[N], b[N], f, s;
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int mn = 1e9;
    for (int i = 1; i <= n; i++) {
      if (b[a[i]] == 0)
        b[a[i]] = i;
      else {
        mn = min(mn, i - b[a[i]] + 1);
        b[a[i]] = i;
      }
    }
    cout << (mn == 1e9 ? -1 : mn) << "\n";
    memset(b, 0, sizeof(b));
  }
  return 0;
}
