#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main() {
  int N;
  cin >> N;
  while (N--) {
    int a[200001];
    map<int, int> ii;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    int temp = inf;
    for (int i = 0; i < n; i++) {
      ii.clear();
      ii[a[i]]++;
      for (int j = i + 1; j < n; j++) {
        if (a[i] != a[j]) continue;
        int flag = 0;
        ii[a[j]]++;
        int ans = ii[a[i]];
        for (int k = i + 1; k < j; k++) {
          if (ii[a[k]] > ans) {
            flag = 1;
            break;
          }
        }
        if (flag) continue;
        temp = min(temp, j - i + 1);
      }
    }
    cout << (temp > 1 ? temp : -1) << endl;
  }
  return 0;
}
