#include <bits/stdc++.h>
using namespace std;
int cnt[2 * (int)1e5 + 1];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      cnt[arr[i]] = 0;
    }
    if (n < 2) {
      cout << -1 << endl;
      continue;
    }
    int res = 2 * (int)1e5 + 1;
    int fp = 0, sp = 1;
    cnt[arr[fp]]++;
    while (sp < n) {
      cnt[arr[sp]]++;
      if (cnt[arr[sp]] == 2) {
        while (arr[fp] != arr[sp] && fp < sp) {
          cnt[arr[fp]]--;
          fp++;
        }
        res = min(res, sp - fp + 1);
        cnt[arr[fp]]--;
        fp++;
      }
      sp++;
    }
    if (res == 2 * (int)1e5 + 1) {
      cout << -1 << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}
