#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    map<long long, long long> mp;
    long long n;
    cin >> n;
    long long arr[n];
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
      mp[arr[i]]++;
    }
    map<long long, long long>::iterator it = mp.begin();
    long long mp_max = it->second, ans = -1, freq = 0;
    ;
    if (mp_max == 1) {
      cout << "-1" << endl;
    } else {
      for (long long i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1]) {
          cout << "2" << endl;
          ans = 1;
          break;
        }
      }
      if (ans != 1) {
        long long len = n, i, j;
        for (i = 0; i < n - 1; i++) {
          map<long long, long long> mt;
          mt[arr[i]]++;
          for (j = i + 1; j < n; j++) {
            mt[arr[j]]++;
            if (arr[j] == arr[i]) break;
          }
          if (j < n) {
            it = mt.begin();
            if (it->first == arr[i]) {
              it++;
              if (it->second < 2) {
                len = min(len, j - i);
              }
            }
          }
        }
        cout << len + 1 << endl;
      }
    }
  }
  return 0;
}
