#include <bits/stdc++.h>
using namespace std;
const long long base = 1e9 + 7;
const long long MAX_N = 2e5 + 7;
long long dd[MAX_N];
void Solves() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n + 1];
    long long minn = base;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (dd[a[i]] != 0) {
        minn = min(minn, i - dd[a[i]] + 1);
      }
      dd[a[i]] = i;
    }
    for (int i = 1; i <= n; i++) dd[a[i]] = 0;
    if (minn == base)
      cout << -1 << "\n";
    else
      cout << minn << "\n";
  }
}
int main() { Solves(); }
