#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long min(long long x, long long y) { return x < y ? x : y; }
const int maxn = 2e5 + 1;
const int mod = 1e9 + 7;
long long n, arr[maxn], brr[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    n = read();
    for (long long i = 0; i < n; i++) {
      arr[i] = read();
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++) {
      brr[i] = (find(arr + i + 1, arr + n, arr[i]) - arr + 1);
      if (brr[i] > n) brr[i] = 0;
      brr[i] -= i;
      if (brr[i] < 0) brr[i] = 2e5 + 9;
    }
    sort(brr, brr + n);
    if (brr[0] < 2e5 + 9)
      cout << brr[0] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
