#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n], min = -1, temp = 0;
    set<int> s;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (s.find(a[i]) == s.end()) {
        s.insert(a[i]);
        temp++;
      } else {
        int j = i - 1;
        temp = 1;
        while (a[i] != a[j]) {
          temp++;
          if (temp > min && min != -1) {
            temp = INT_MAX;
            break;
          }
          j--;
        }
        temp++;
        if (temp < min || min == -1) {
          min = temp;
        }
        temp = 1;
        s.clear();
        s.insert(a[i]);
      }
    }
    if (min > 1)
      cout << min << endl;
    else
      cout << "-1" << endl;
  }
}
