#include <bits/stdc++.h>
using namespace std;
long long a[200005], pos[200005], c[200005];
int main() {
  long long t, i, j, n, x, y, d;
  cin >> t;
  while (t--) {
    int current = -1, previous = -1, ans = INT_MAX, p = INT_MIN, l = 0, k = 0;
    cin >> n;
    for (i = 0; i < 200005; i++) pos[i] = -1;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      c[i] = a[i];
    }
    sort(c, c + n);
    for (i = 0; i < n - 1; i++) {
      if (c[i] == c[i + 1] && i != n - 2)
        k++;
      else if (c[i] == c[i + 1] && i == n - 2) {
        k++;
        if (p <= (k + 1) && (k + 1) != 1) {
          pos[l++] = i;
          p = k + 1;
        }
        k = 0;
      } else {
        if (p <= (k + 1) && (k + 1) != 1) {
          pos[l++] = i;
          p = k + 1;
        }
        k = 0;
      }
    }
    sort(pos, pos + 200005, greater<int>());
    for (int j = 0; j < l; j++) {
      previous = -1;
      current = -1;
      for (i = 0; i < n; i++) {
        if (a[i] == c[pos[j]]) {
          previous = current;
          current = i;
        }
        if (previous != -1 && current != -1)
          ans = min(ans, (current - previous + 1));
      }
    }
    if (n == 1 || ans == INT_MAX)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
