#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (long long i = 0; i < (long long)n; i++) cin >> a[i];
    unordered_map<int, int> left;
    int ans = (int)pow(10, 6);
    int flag = 0;
    for (long long i = 0; i < (long long)n; i++) {
      if (left[a[i]] > 0 and i + 1 - left[a[i]] + 1 < ans) {
        ans = i + 1 - left[a[i]] + 1;
        flag = 1;
      }
      left[a[i]] = i + 1;
    }
    if (flag)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
