#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr(n, 0);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int start = 0;
    int end = 0;
    set<int> occ;
    int ans = 1e9;
    while (end < n) {
      if (occ.find(arr[end]) == occ.end()) {
        occ.emplace(arr[end]);
      } else {
        while (arr[start] != arr[end]) {
          occ.erase(arr[start]);
          start++;
        }
        ans = min(ans, end - start + 1);
        start++;
      }
      end++;
    }
    cout << ((ans == 1e9) ? -1 : ans) << endl;
  }
  return 0;
}
