#include <bits/stdc++.h>
using namespace std;
const int maxx = 2e5 + 100;
long long t, n;
long long a[maxx], vis[maxx];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> t;
  while (t--) {
    cin >> n;
    memset(vis, 0, sizeof vis);
    memset(a, 0, sizeof a);
    long long num = -1, va = 0;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      vis[a[i]]++;
      num = max(num, vis[a[i]]);
      if (num == vis[a[i]]) {
        va = a[i];
      }
    }
    if (n == 1) {
      cout << "-1" << endl;
      continue;
    } else if (num == 1 && n > 1) {
      cout << "-1" << endl;
      continue;
    } else {
      long long l = -1, ans = maxx;
      int flag = 1;
      for (int i = 1; i <= n; i++) {
        if (vis[a[i]] == num) {
          if (a[i] != va) {
            flag = 0;
            break;
          }
          if (l == -1) {
            l = i;
          } else {
            ans = min(ans, i - l + 1);
            l = i;
          }
        }
      }
      if (!flag) {
        cout << "-1" << endl;
      } else {
        cout << ans << endl;
      }
    }
  }
  return 0;
}
