#include <bits/stdc++.h>
using namespace std;
void deb(istream_iterator<string> it) { it = it; }
template <typename T, typename... Args>
void deb(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  deb(++it, args...);
}
using lli = long long;
using ld = long double;
using ulli = unsigned long long;
using ii = pair<int, int>;
using vlli = vector<lli>;
template <class T>
struct rge {
  T b, e;
};
template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}
template <class A, class B>
ostream& operator<<(ostream& os, pair<A, B> p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, rge<T> d) {
  os << "[";
  for (auto it = d.b; it != d.e; it++) os << (it == d.b ? "" : ", ") << *it;
  return os << "]";
}
const lli mod = 1e7 + 7;
const lli N = 2e5 + 5;
const lli inf = 1e9 + 5;
int freq[N], a[N], n, tc, mn = inf;
bool stop = false;
void add(int i) {
  freq[a[i]]++;
  if (freq[a[i]] == 2) {
    stop = true;
  }
}
void rem(int i) {
  freq[a[i]]--;
  if (freq[a[i]] == 1) {
    stop = true;
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> tc;
  while (tc--) {
    memset(freq, 0, sizeof freq);
    memset(a, 0, sizeof a);
    stop = false;
    cin >> n;
    for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
         i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1)))
      cin >> a[i];
    if (n == 1) {
      cout << -1 << '\n';
      continue;
    }
    int l = 1, r = 0;
    while (r < n) {
      add(++r);
      if (stop) {
        stop = false;
        while (l < r) {
          rem(l);
          if (stop) {
            stop = false;
            mn = min(mn, r - l + 1);
            ++l;
            break;
          }
          ++l;
        }
      }
    }
    cout << mn << '\n';
  }
  return 0;
}
