#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int l = 1, h, i, n, x, t, q, y = -1, z = 0, a = 0, b = 0, c = 0, k,
                sum = 0, m, d = 0, r, w, j, ans = INT_MAX, s, mini = INT_MAX,
                maxi = INT_MAX;
  queue<int> que;
  bool cou;
  float kl;
  cin >> t;
  vector<bool> sie(1000001, 0);
  char cg;
  string str, std;
  while (t--) {
    cin >> n;
    mini = INT_MAX;
    vector<int> vec(n + 1);
    for (i = 1; i < n + 1; ++i) cin >> vec[i];
    map<int, int> mp;
    for (i = 1; i < n + 1; ++i) {
      if (mp[vec[i]] == 0)
        mp[vec[i]] = i;
      else {
        mini = (mini < i - mp[vec[i]] + 1 ? mini : i - mp[vec[i]] + 1);
        mp[vec[i]] = i;
      }
    }
    if (mini == INT_MAX)
      cout << -1;
    else
      cout << mini;
    cout << "\n";
  }
}
