#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 long long INF = 0x3f3f3f;
const int mod = 1e9 + 7;
long long n, arr[maxn], brr[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long f = 0;
    n = read();
    for (long long i = 0; i < n; i++) {
      arr[i] = read();
      if (i && arr[i] == arr[i - 1])
        f = 2;
      else if (i > 1 && arr[i] == arr[i - 2])
        f = 3;
      else if (i > 2 && arr[i] == arr[i - 3])
        f = 4;
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    if (f) {
      cout << f << endl;
      continue;
    }
    memset(brr, INF, sizeof brr);
    for (long long i = 0; i < n; i++) {
      for (long long j = i - 1; j >= 0; j--) {
        if (arr[j] == arr[i]) {
          brr[i] = i - j + 1;
          break;
        }
      }
    }
    sort(brr, brr + n);
    if (brr[0] < INF)
      cout << brr[0] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
