#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, flag = 0;
    cin >> n;
    vector<int> vc;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      vc.push_back(x);
    }
    int cnt = n + 1;
    unordered_map<int, int> mp1, mp2;
    for (int i = 0; i < n; i++) {
      mp1[vc[i]] = 0;
      mp2[vc[i]] = 0;
    }
    for (int i = 0; i < n; i++) {
      mp1[vc[i]]++;
      if (mp1[vc[i]] > 1) {
        cnt = min(cnt, i - mp2[vc[i]] + 1);
      }
      mp2[vc[i]] = i;
    }
    if (cnt == n + 1) {
      cout << "-1" << endl;
    } else {
      cout << cnt << endl;
    }
  }
}
