#include <bits/stdc++.h>
using namespace std;
bool ispowerof2(long long int num) {
  if ((num & (num - 1)) == 0) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    map<long long int, long long int> mp;
    long long int a[n], i, min1 = 1e18;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }
    long long int len, b[2];
    bool found = false, noless = false;
    for (auto it = mp.begin(); it != mp.end(); it++) {
      if (it->second == 1)
        continue;
      else {
        found = true;
        b[0] = -1;
        b[1] = -1;
        for (i = 0; i < n; i++) {
          if (a[i] == it->first) {
            if (b[0] == -1)
              b[0] = i;
            else if (b[1] == -1)
              b[1] = i;
            else {
              if (i > b[0])
                b[0] = i;
              else if (i > b[1])
                b[1] = i;
            }
            if (b[0] != -1 && b[1] != -1) {
              len = abs(b[0] - b[1]);
              len++;
              if (len < min1) min1 = len;
              if (min1 == 2) {
                noless = true;
                break;
              }
            }
          }
        }
        if (noless) break;
      }
    }
    if (!found)
      cout << "-1\n";
    else
      cout << min1 << "\n";
  }
  return 0;
}
