#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f;
const long long inv2 = 500000004;
const int maxn = 2e5 + 5;
int n, a[maxn], pos[maxn];
int low[maxn];
void add(int i, int x) {
  while (i <= n) {
    low[i] += x;
    i += (i & (-i));
  }
  return;
}
int getsum(int i) {
  int s = 0;
  while (i > 0) {
    s += low[i];
    i -= (i & (-i));
  }
  return s;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      pos[i] = -1;
      low[i] = 0;
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    int ans = maxn;
    for (int i = 1; i <= n; i++) {
      if (pos[a[i]] == -1) {
        pos[a[i]] = i;
        add(i, 1);
      } else {
        int pre = pos[a[i]];
        if (pre == i - 1)
          ans = 2;
        else {
          int sum = getsum(i - 1) - getsum(pre);
          if (sum == i - 1 - pre) ans = min(ans, i - pre + 1);
        }
        add(i, 1);
        add(pre, -1);
        pos[a[i]] = i;
      }
    }
    if (ans == maxn)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
