#include <bits/stdc++.h>
using namespace std;
int cnt[3000000];
int main() {
  int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      cnt[arr[i]] = 0;
    }
    int i = 0, j = 1, ans = 3000000;
    cnt[arr[i]]++;
    while (i < j && j < n) {
      cnt[arr[j]]++;
      if (cnt[arr[j]] > 1) {
        cnt[arr[j]]--;
        cnt[arr[i]]--;
        ans = min(ans, j - i + 1);
        i++;
      } else {
        j++;
      }
    }
    if (ans == 3000000)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
}
