#include <bits/stdc++.h>
using namespace std;
long long x, t, n, inp, mn, temp, f, arr[200002];
map<long long, long long> m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  for (int l = 0; l < t; l++) {
    m.clear();
    f = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    m[arr[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (m.find(arr[i]) == m.end()) m[arr[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
      if (arr[i] == arr[i - 1]) {
        cout << 2 << '\n';
        f = 1;
        break;
      }
    }
    if (f == 1) continue;
    mn = LLONG_MAX;
    for (int i = 2; i <= n; i++) {
      temp = i - m[arr[i]] + 1;
      m[arr[i]] = i;
      if (temp == 1) continue;
      mn = min(temp, mn);
    }
    if (mn == LLONG_MAX)
      cout << -1 << '\n';
    else
      cout << mn << '\n';
  }
}
