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