#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#pragma optimization_level 3
#pragma GCC optimize("O2")
#pragma GCC optimize("O3")
#pragma GCC optimize("01")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("Os")
#pragma GCC target("avx,avx2,fma")


#define yes cout << "YES" << endl;
#define no  cout << "NO" << endl;
#define sz(X) (int)(X).size()
#define all(X) (X).begin() , (X).end()
#define rall(X) (X).rbegin() , (X).rend()
#define pb push_back
#define pf push_front
#define elif else if
#define endl '\n'
#define tab '\t'
#define run_case(t,T) for(__typeof(t) t=1;t<=T;t++)

using namespace std;
using namespace __gnu_pbds;

template < class T1 >
struct range {
    struct I {
        T1 i;
        void operator++() { ++i; }
        T1 operator*() const { return i; }
        bool operator!=(I o) const { return i < *o; }
    };
    const T1 l_, r_;
    range(T1 l, T1 r) : l_(l), r_(r) {}
    range(T1 n) : range(0, n) {}
    I begin() const { return {l_}; }
    I end() const { return {r_}; }
};

template < class T1 >
struct rrange {
    struct I {
        T1 i;
        void operator++() { --i; }
        T1 operator*() const { return i; }
        bool operator!=(I o) const { return i > *o; }
    };
    const T1 l_, r_;
    rrange(T1 l, T1 r) : l_(l), r_(r) {}
    rrange(T1 n) : rrange(0, n) {}
    I begin() const { return {r_ - 1}; }
    I end() const { return {l_ - 1}; }
};


typedef long long ll;
typedef unsigned long long ull;
template < class T1 >
T1 fast_pow(T1 a, T1 b){T1 res = 1;while(b){if (b & 1)res *= a;a *= a;b >>= 1;}return res;}
template < class T1 >
T1 fast_pow(T1 a , T1 b , T1 mod){T1 res = 1;a %= mod;if (!a)return 0;while(b){if (b & 1)res = (res * a) % mod;a = (a * a) % mod;b >>= 1;}return res;}

template < typename T1 >T1 inexact_pow(T1 a , T1 b){return round(exp((long double)a * log(b)));}

template < typename T1 > inline T1 lcm(T1 a , T1 b){return a / __gcd(a , b) * b;}

template < class T1 , class T2 , class T3 > struct node{T1 first = 0;T2 second = 0;T3 third = 0;};

template < typename T1 >  ostream &operator << (std::ostream &out, vector < T1 > &ar){for (auto i = 0;i < ar.size();++i) out << ar[i] << " ";return out;}
template < typename T1 >  istream &operator >> (std::istream &in , vector < T1 > &ar){for (auto i = 0;i < ar.size();++i) in >> ar[i];return in;}
template < typename T1 >  ostream &operator << (std::ostream &out, deque < T1 > &ar){for (auto i = 0;i < ar.size();++i) out << ar[i] << " ";return out;}
template < typename T1 >  istream &operator >> (std::istream &in , deque < T1 > &ar){for (auto i = 0;i < ar.size();++i) in >> ar[i];return in;}
template < typename T1 >  istream &operator >> (std::istream &in , vector < vector < T1 > > &ar){for (auto i = 0;i < ar.size();++i){for (auto j = 0;j < ar[i].size();++j) in >> ar[i][j];}return in;}
template < typename T1 >  ostream &operator << (std::ostream &out , vector < vector < T1 > > &ar){for (auto i = 0;i < ar.size();++i){for (auto j = 0;j < ar[i].size();++j){ out << ar[i][j];} out << '\n';}return out;}

template <class T> inline bool isSquare(T n) {
    T s = sqrt(n);
    return s * s == n || (s + 1) * (s + 1) == n;
}
template < typename T1 > static constexpr T1 inf_max = numeric_limits < T1 >::max();
template < typename T2 > static constexpr T2 inf_min = numeric_limits < T2 >::min();

template < class T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
template < class T > using ordered_multiset = tree < T , null_type , greater_equal < T > , rb_tree_tag , tree_order_statistics_node_update >;

template < class T1 >// only if N is small
bool bruteforce(T1 n){for (auto i = 1;i <= n;++i) if (!(n % i))return true;return false;}

template < typename t > bool is_prime(t num){for (auto i = 2;i * i <= num;++i){if (num % i == 0){return false;}}return true;}
template < class t > void sieve(vector < t > &is_prime){for (auto i = 2;i * i <= is_prime.size();++i){if (is_prime[i]){for (auto j = i * i;j <= is_prime.size();j += i)is_prime[j] = false;}}}

bool is_palindrom(const string &s){return equal(s.begin() , s.end() , s.rbegin());}
template < class T1 >T1 sum_of_num(T1 num){T1 sum = 0;while (num){sum += num % 10;num /= 10;}return sum;}
template < class T1 > T1 lsb(T1 i){return i & -i;} // least significant bit

int mod = 1e9 + 7;

void solve() { //don't forget to initialize T;
    int n;
    cin >> n;
    vector < int > ar(n);
    cin >> ar;
    if (n == 1){
        cout << -1 << endl;
        return;
    }
    int min_length = inf_max < int >;
    unordered_map < int , int > mp;
    for (auto i = 0;i < ar.size();++i){
        if (mp[ar[i]]){
            min_length = min(min_length , i - (mp[ar[i]] - 1) + 1);
        }
        mp[ar[i]] = i + 1;
    }
    cout << min_length << endl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ull t = 1;
    cin >> t;
    while (t--){
        solve();
    }
    return 0;
}