#include <bits/stdc++.h>
using namespace std;
long long const INF = 2e18 + 100;
int const MAX = 3 * 1e5 + 10;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t, n, i, x;
  cin >> t;
  while (t--) {
    cin >> n;
    int arr[n + 1];
    map<int, int> mp;
    x = n;
    for (i = 1; i <= n; i++) {
      cin >> arr[i];
      if (mp[arr[i]]) {
        x = min(x, i - mp[arr[i]]);
      }
      mp[arr[i]] = i;
    }
    if (x == n)
      x = -1;
    else
      x++;
    cout << x << '\n';
  }
  return 0;
}
