#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[n];
    long long int h[100000 + 1] = {};
    long long int h1[100000 + 1] = {};
    long long int m = INT_MAX;
    long long int d;
    long long int f = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      if (h[a[i]] == 0) {
        h[a[i]]++;
        h1[a[i]] = i + 1;
      } else {
        f = 1;
        h[a[i]]++;
        d = abs(h1[a[i]] - (i + 1));
        m = min(m, d);
      }
    }
    if (f == 1) {
      if (m == 1)
        m += 1;
      else if (m == n - 1)
        m = n;
      cout << m << "\n";
    } else
      cout << "-1\n";
  }
  return 0;
}
