#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 5e5 + 20;
const long long mod = 1e9 + 7;
vector<int> q[MAXN];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      int x;
      cin >> x;
      q[x].push_back(i);
    }
    int maxn = 0, flag = 0, flag1 = 0;
    for (int i = 1; i <= n; ++i) {
      int x = q[i].size();
      if (maxn < x)
        maxn = x, flag = i, flag1 = 0;
      else if (maxn == x)
        flag1 = 1;
    }
    if (flag1 == 1)
      cout << -1 << endl;
    else {
      int len = q[flag].size(), minn = 0x3f3f3f3f;
      for (int i = 1; i < len; ++i) {
        int x = q[flag][i] - q[flag][i - 1] + 1;
        if (minn > x) minn = x;
      }
      if (minn == 0x3f3f3f3f)
        cout << -1 << endl;
      else
        cout << minn << endl;
    }
    for (int i = 1; i <= n; ++i) q[i].clear();
  }
}
