#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  int t;
  cin >> t;
  while (t--) {
    unordered_set<int> s;
    long long int n, min_len = 2000000;
    cin >> n;
    if (n == 1) {
      cout << "-1\n";
      cin >> n;
      continue;
    }
    bool res = false;
    long long int hash[2000000] = {0};
    long long int a[n];
    for (int i = 0; i < n; ++i) {
      long long int e;
      cin >> e;
      if (s.count(e) == 0)
        s.insert(e);
      else {
        res = true;
        long long new_len = i - hash[e] + 1;
        min_len = min_len < new_len ? min_len : new_len;
        hash[e] = i;
      }
    }
    if (res)
      cout << min_len << endl;
    else
      cout << "-1" << endl;
  }
  return 0;
}
