#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const long long mod = 1e9 + 7;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return f * x;
}
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long poww(long long x, long long y, long long p) {
  long long ans = 1;
  while (y) {
    if (y & 1) ans = ans * x % p;
    x = x * x % p;
    y >>= 1;
  }
  return ans;
}
int t, n;
map<int, int> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    mp.clear();
    int ans = maxn;
    cin >> n;
    int x;
    for (int i = 0; i < n; i++) {
      cin >> x;
      if (mp.count(x) != 0) {
        ans = min(ans, i - mp[x] + 1);
        mp[x] = i;
      } else
        mp[x] = i;
    }
    if (ans == maxn)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
