#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n], pos[n], len = 1e9 + 7, ans;
    int flag = 0;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      pos[i + 1] = -1;
    }
    for (long long i = 0; i < n; i++) {
      if (pos[a[i]] == -1)
        pos[a[i]] = i;
      else if (pos[a[i]] != -1) {
        ans = i - pos[a[i]] + 1;
        pos[a[i]] = i;
        len = min(len, ans);
        flag = 1;
      }
    }
    if (flag)
      cout << len << "\n";
    else
      cout << "-1"
           << "\n";
  }
  return 0;
}
