#include <bits/stdc++.h>
using namespace std;
int k, n, a[100000], b[100000], f, s;
int main() {
  cin >> n;
  while (n) {
    cin >> k;
    map<int, int> m;
    vector<pair<int, int>> index(100000);
    for (int i = 1; i <= k; i++) {
      cin >> a[i];
      m[a[i]]++;
      if (index[a[i]].first == 0)
        index[a[i]].first = i;
      else if (index[a[i]].second == 0 && index[a[i]].first != 0)
        index[a[i]].second = i;
      else {
        if (index[a[i]].second - index[a[i]].first > i - index[a[i]].second) {
          index[a[i]].first = index[a[i]].second;
          index[a[i]].second = i;
        }
      }
    }
    int res = INT_MAX;
    for (int i = 0; i < k; i++) {
      if (m[a[i]] >= 2) {
        if ((index[a[i]].second - index[a[i]].first) < res) {
          res = (index[a[i]].second - index[a[i]].first);
          s = a[i];
        }
      }
    }
    int count = 0;
    for (int i = index[s].first; i <= index[s].second; i++) {
      count++;
    }
    if (k <= 1)
      cout << -1 << "\n";
    else
      cout << count << "\n";
    n--;
  }
}
