#include <bits/stdc++.h>
using namespace std;
int getDominated(int* arr, int i, int j) {
  map<int, int> mp;
  int maxkey = arr[i];
  for (int k = i; k <= j; k++) {
    int key = arr[k];
    if (mp.count(key)) {
      mp[key] += 1;
    } else {
      mp[key] = 1;
    }
    if (maxkey != key && mp[key] > mp[maxkey]) {
      maxkey = key;
    }
  }
  int maxCount = mp[maxkey];
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->first != maxkey && it->second == maxCount) return -1;
  }
  return maxkey;
}
int getResult() {
  int length;
  cin >> length;
  int arr[length];
  for (int i = 0; i < length; i++) {
    cin >> arr[i];
  }
  int result = -1;
  if (length < 2) return result;
  int dom = getDominated(arr, 0, length - 1);
  if (dom == -1) return result;
  int minLenght = length;
  for (int j = 1; j < length; j++) {
    for (int i = 0; i < j; i++) {
      if (j - i + 1 >= minLenght) continue;
      if (dom == getDominated(arr, i, j)) {
        minLenght = j - i + 1;
      }
    }
  }
  return minLenght;
}
int main() {
  int roundNum;
  cin >> roundNum;
  for (int i = 0; i < roundNum; i++) {
    int result = getResult();
    cout << result << endl;
  }
  return 0;
}
