#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 7;
long long modulo(long long num, long long MOD = 1e9 + 7) {
  return ((num % MOD) + MOD) % MOD;
}
long long power(long long b, long long e, long long MOD = 1e9 + 7) {
  long long ans = 1;
  while (e) {
    if (e % 2) ans = (ans * b) % MOD;
    b = (b * b) % MOD;
    e /= 2;
  }
  return ans;
}
long long inv(long long num, long long MOD = 1e9 + 7) {
  return power(modulo(num), MOD - 2, MOD);
}
long long gcd(long long a, long long b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
int a[MX], ans[MX], c[MX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int b;
    for (int i = 1; i <= n; i++) {
      cin >> ans[i];
      a[ans[i]]++;
    }
    int mx = 0, res;
    for (int i = 1; i <= n; i++) {
      if (a[i] > mx) {
        res = i;
        mx = a[i];
      }
    }
    int tmp = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] == mx) tmp++;
    }
    if (tmp > 1 || n < 2) {
      cout << "-1" << endl;
    } else {
      int temp = 0;
      for (int i = 1; i <= n; i++) {
        if (ans[i] == res) c[++temp] = i;
      }
      int mi = 1e9;
      for (int i = 2; i <= temp; i++) {
        int tt = 0;
        for (int j = 1; j <= temp - i + 1; j++) {
          int u = c[j], v = c[j + i - 1], q = 0;
          if (v - u + 1 >= mi) continue;
          for (int k = u + 1; k <= v - 1; k++) a[ans[k]] = 0;
          for (int k = u + 1; k <= v - 1; k++) {
            a[ans[k]]++;
            if (a[ans[k]] >= i) {
              q = 1;
              break;
            }
          }
          if (!q) {
            mi = v - u + 1;
            tt = 1;
          }
        }
        if (!tt) break;
      }
      cout << mi << endl;
    }
    for (int i = 1; i <= n; i++) a[ans[i]] = 0;
  }
}
