#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> mp;
    int flag = 0;
    int len = 0;
    int ans = 1e9;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
      mp[a[i]]++;
      if (mp[a[i]] == 1)
        len++;
      else if (mp[a[i]] > 1) {
        len++;
        ans = min(ans, len);
        mp.clear();
        len = 0;
        i--;
        flag = 1;
      }
    }
    if (flag == 0)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
