#include <bits/stdc++.h>
using namespace std;
int t, n, a[2 * 100004], b[2 * 100004], c[2 * 100004], e[2 * 100004];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    for (int i = 1; i <= n; i++) {
      e[i] = 0;
      c[i] = 0;
    }
    int d1 = 1, ma = 0, gt;
    if (n < 2)
      cout << "-1"
           << "\n";
    else {
      for (int i = 1; i <= n - 1; i++)
        if (a[i] == a[i + 1]) d1++;
      if (d1 == n)
        cout << "2"
             << "\n";
      else {
        sort(a + 1, a + n + 1);
        int d = 1;
        c[1] = a[1];
        for (int i = 1; i <= n - 1; i++)
          if (a[i] != a[i + 1]) {
            d++;
            c[d] = a[i + 1];
          }
        for (int i = 1; i <= d; i++)
          for (int j = 1; j <= n; j++)
            if (c[i] == a[j]) e[i]++;
        for (int i = 1; i <= d; i++)
          if (ma < e[i]) {
            ma = e[i];
            gt = c[i];
          }
        if (ma == 1)
          cout << "-1"
               << "\n";
        else {
          int d3 = 0;
          for (int i = 1; i <= d; i++)
            if (ma == e[i]) d3++;
          if (d3 == d)
            cout << "2"
                 << "\n";
          else {
            if (d3 == 1) {
              int l = 2 * 100004;
              for (int i = 1; i <= n - 1; i++)
                for (int j = i + 1; j <= n; j++)
                  if ((b[i] == gt) && (b[j] == gt)) {
                    l = min(l, j - i + 1);
                  }
              cout << l << "\n";
            } else
              cout << "-1"
                   << "\n";
          }
        }
      }
    }
  }
  return 0;
}
