#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <stdio.h>
#include <map>
using namespace std;
#define slope(x11, y11, x22, y22) ((double)y11-(double)y22)/((double)x11-(double)x22)

bool ys[1000][1000];
string s;
int res, len;

int main() {
	cin >> s;
	len = s.size();
	if (s.size() % 2) {
		cout << -1 << endl;
		return 0;
	}
	int u, l, r, d;
	l = r = u = d = 0;
	for (int i = 0; i < len; i++) if (s[i] == 'L')
		l++;
	else if (s[i] == 'R')
		++r;
	else if (s[i] == 'D')
		d++;
	else
		u++;
	if (l != r || d != u) {
		if (abs(l - r) % 2 && abs(u - d) % 2)
			res += 2, --u, --r;
		else if (abs(l - r) % 2)
			--r, ++u;
		else if (abs(u - d) % 2)
			--u, ++r;
		res = abs(u - d) / 2 + abs(l - r) / 2;
	}
	cout << res << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <stdio.h>
#include <map>
using namespace std;
#define slope(x11, y11, x22, y22) ((double)y11-(double)y22)/((double)x11-(double)x22)

bool ys[1000][1000];
string s;
int res, len;

int main() {
	cin >> s;
	len = s.size();
	if (s.size() % 2) {
		cout << -1 << endl;
		return 0;
	}
	ys['L']['R'] = ys['R']['L'] = ys['D']['U'] = ys['U']['D'] = 1;
	for (int i = 0; i < len / 2;i++) {
		res += !ys[s[i]][s[len - i - 1]];

	}
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,right=0,left=0,up=0,down=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')right++;
        else if(s[i]=='L')left++;
        else if(s[i]=='U')up++;
        else down++;
    }
    if((right&&(!left&&!up&&!down))||(left&&(!right&&!up&&!down))||(up&&(!right&&!left&&!down))||(down&&(!right&&!left&&!up)))printf("%d",len-1);
    else{
        if(len%4)printf("-1\n");
        else{
            int sum=up+down+left+right;
            sum/=4;
            step=abs(up-sum)+abs(down-sum)+abs(left-sum)+abs(right-sum);
            printf("%d\n",step/2);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,hor=0,ver=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')hor++;
        else if(s[i]=='L')hor--;
        else if(s[i]=='U')ver++;
        else ver--;
    }
    if(abs(hor-ver)%2==0)printf("%d",(hor+ver)/2);
    else printf("-1\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,right=0,left=0,up=0,down=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')right++;
        else if(s[i]=='L')left++;
        else if(s[i]=='U')up++;
        else down++;
    }
    if(len==1)printf("-1\n");
    else if((right&&(!left&&!up&&!down))||(left&&(!right&&!up&&!down))||(up&&(!right&&!left&&!down))||(down&&(!right&&!left&&!up)))printf("%d",len-1);
    else{
            int sum=len/4;
            step=abs(up-sum)+abs(down-sum)+abs(left-sum)+abs(right-sum);
            //cout<<step<<endl;
            if(step%2==0)printf("%d\n",step/2);
            else printf("-1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,hor=0,ver=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')hor++;
        else if(s[i]=='L')hor--;
        else if(s[i]=='U')ver++;
        else ver--;
    }
    if(abs(hor+ver)%2==0)printf("%d",(hor+ver)/2);
    else printf("-1\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,right=0,left=0,up=0,down=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')right++;
        else if(s[i]=='L')left++;
        else if(s[i]=='U')up++;
        else down++;
    }
    if((right&&(!left&&!up&&!down))||(left&&(!right&&!up&&!down))||(up&&(!right&&!left&&!down))||(down&&(!right&&!left&&!up)))printf("%d",len-1);
    else{
        if(len%4)printf("-1\n");
        else{
            int sum=up+down+left+right;
            sum/=4;
            step=abs(up-sum)+abs(down-sum)+abs(left-sum)+abs(right-sum);
            if(step%2==0)printf("%d\n",step/2);
            else printf("-1\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,hor=0,ver=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')hor++;
        else if(s[i]=='L')hor--;
        else if(s[i]=='U')ver++;
        else ver--;
    }
    hor=abs(hor),ver=abs(ver);
    if(abs(hor+ver)%2==0)printf("%d",(hor+ver)/2);
    else printf("-1\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int i,len,right=0,left=0,up=0,down=0,step;
    cin>>s;
    len=s.length();
    for(i=0;i<len;i++){
        if(s[i]=='R')right++;
        else if(s[i]=='L')left++;
        else if(s[i]=='U')up++;
        else down++;
    }
    if((right&&(!left&&!up&&!down))||(left&&(!right&&!up&&!down))||(up&&(!right&&!left&&!down))||(down&&(!right&&!left&&!up)))printf("%d",len-1);
    else{
            int sum=len/4;
            step=abs(up-sum)+abs(down-sum)+abs(left-sum)+abs(right-sum);
            //cout<<step<<endl;
            if(step%2==0)printf("%d\n",step/2);
            else printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int cc[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
		int mn = 1e9;
        for (int i = 1; i <= n; i++) cc[i] = 0;
		for (int i = 1, x; i <= n; i++) {
			cin >> x;
			if (cc[x]) mn = min(mn, i - cc[x]);
			cc[x] = i;
		}
		if (mn > 1e6) cout << "-1\n";
		else cout << mn + 1 << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;}
#define in hmt()
#define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;})
#define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define forv(a,b) for(auto &a:b)
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define reset(f,x) memset(f,x,sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define onbit(x,i) (x|(1<<(i-1)))
#define offbit(x,i) (x&~(1<<(i-1)))
const int N=100010;
int n,m,a[N],ma[N],c[N];
pii b[N];
int main()
{
    //freopen("1257D.inp","r",stdin);
    forinc(cs,1,in)
    {
        n=in;
        forinc(i,1,n) a[i]=in;
        m=in;
        forinc(i,1,m) b[i]={in,in};
        sort(b+1,b+m+1);
        forinc(i,1,m) c[i]=b[i].fi;
        ma[m+1]=0;
        fordec(i,m,1) ma[i]=max(ma[i+1],b[i].se);
        int i=1,ok=0,res=0;
        while(i<=n)
        {
            ++res;
            int j=i,M=a[i];
            while(j<=n)
            {
                int x=lower_bound(c+1,c+m+1,M)-c;
                if(x==m+1) break;
                if(ma[x]<(j-i+1)) break;
                ++j;M=max(M,a[j]);
            }
            if(i==j) {ok=1;break;}
            i=j;
        }
        if(ok) cout<<-1<<"\n";
        else cout<<res<<"\n";
    }

}


#include <bits/stdc++.h>
using namespace std;
int main(){
    vector<int> v;
    unordered_set<int> s;
    int x, q, val, i, j, len;
    cin >> q;
    while(q--){
        v.clear();
        s.clear();
        cin >> x;
        while (x--){
            cin >> val;
            v.push_back(val);
        }
        len = INT_MAX;
        i = 0, j = 0;
        while(j < v.size()){
            if (s.count(v[j]) == 0){
                s.insert(v[j]);
                ++j;
            } else {
                while(v[i] != v[j]){
                    s.erase(v[i]);
                    ++i;
                }
                len = min(j-i+1, len);
                ++i;
                ++j;
            }
        }
        len == INT_MAX ? cout << -1 : cout << len;
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;}
#define in hmt()
#define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;})
#define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define forv(a,b) for(auto &a:b)
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define reset(f,x) memset(f,x,sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define onbit(x,i) (x|(1<<(i-1)))
#define offbit(x,i) (x&~(1<<(i-1)))
const int N=100010;
int n,m,a[N],ma[N],c[N];
pii b[N];
int main()
{
    //freopen("1257D.inp","r",stdin);
    forinc(cs,1,in)
    {
        n=in;
        forinc(i,1,n) a[i]=in;
        m=in;
        forinc(i,1,m) b[i]={in,in};
        sort(b+1,b+m+1);
        forinc(i,1,m) c[i]=b[i].fi;
        ma[m+1]=0;
        fordec(i,m,1) ma[i]=max(ma[i+1],b[i].se);
        int i=1,ok=0,res=0;
        while(i<=n)
        {
            ++res;
            int j=i,M=a[i];
            while(j<=n)
            {
                int x=lower_bound(c+1,c+m+1,M)-c;
                if(x==m+1) break;
                if(ma[x]<(j-i+1)) break;
                ++j;M=max(M,a[j]);
            }
            if(i==j) {ok=1;break;}
            i=j;
        }
        if(ok) cout<<-1<<"\n";
        else cout<<res<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int ans = INT_MAX;
		map<int, int> mymap;
		vector<int> arr(n);
		for(int i=0;i<n;i++)
			cin>>arr[i];
		int l=0, r=0;
		while(l<n && r<n){
			mymap[arr[r]]++;
			if(mymap[arr[r]]>1){
				ans = min(ans, (r-l)+1);
				mymap[arr[l]]--;
				l++;
				while(l<n && mymap[arr[r]]>1){
					ans = min(ans, (r-l)+1);
					mymap[arr[l]]--;
					l++;
				}
			}
			r++;
		}
		if(ans==INT_MAX)
			ans=-1;
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FOD(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl;}
#define fs first
#define sc second

typedef pair <int, int> II;
const int N = 2E5 + 10;

int n;
int a[N], pre[N], has[N];

void Read_Input() {
    scanf("%d", &n);
    FOR(i, 1,  n) {
        scanf("%d", &a[i]);
        pre[i] = has[a[i]];
        has[a[i]] = i;
    }
}

void Solve() {
    int ans = 1E9;
    FOR(i, 1, n) if (pre[i]) {
        ans = min(ans, i - pre[i] + 1);
    }
    if (ans == 1E9) ans = -1;
    printf("%d\n", ans);
}

void Reset() {
    FOR(i, 1, n) has[a[i]] = 0;
}

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // LOCAL
    int TC; scanf("%d", &TC);
    while (TC--) {
        Read_Input();
        Solve();
        Reset();
    }
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, l, r) for(int i = (int)l; i <= (int)r; ++i)

using namespace std;
const int Nmax = 2e5 + 112;
const int inf = 1e9 + 112;

int T, n, a[Nmax], ans, pos[Nmax];

int main() {
//    freopen("in.inp", "r", stdin);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> T;
    while(T--) {
        cin >> n;
        FOR(i, 1, n) {
            cin >> a[i];
            pos[i] = 0;
        }
        ans = inf;
        FOR(i, 1, n) {
            if (pos[a[i]] != 0) ans = min(ans, i - pos[a[i]] + 1);
            pos[a[i]] = i;
        }
        if (ans == inf) cout << -1 << '\n';
        else cout << ans << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int mod = 1e9 + 7;

const int inf = 1e9;

int add(int x, int y) {
    return (1ll * x + 1ll * y) % mod;
}

int del(int x, int y) {
    return ((1ll * x - 1ll * y) % mod + mod) % mod;
}

int mul(int x, int y) {
    return (1ll * x * 1ll * y) % mod;
}

const int N = 2e5 + 5;

int test, n, a[N], nxt[N];

int main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    cin >> test;
    while(test--) {
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        int mn = inf;
        for(int i = 1; i <= n; i++) {
            if(nxt[a[i]] != 0)
            mn = min(mn, i - nxt[a[i]] + 1);
            nxt[a[i]] = i;
        }
        for(int i = 1; i <= n; i++)
            nxt[a[i]] = 0;
        if(mn == inf)
            cout << "-1\n";
        else
            cout << mn << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 200020;

int a[N];
int last[N];

int main(){
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int T; cin >> T;
  while(T--){
    int n; cin >> n;
    for(int i = 1; i <= n; ++i) last[i] = -1;

    int mx_last = -1, ans = n + 5;

    for(int i = 1; i <= n; ++i) {
      cin >> a[i];
      if(last[a[i]] != -1) {
        if(last[a[i]] > mx_last) {
          ans = min(ans, i - last[a[i]] + 1);
        }
        mx_last = max(mx_last, last[a[i]]);
      }
      last[a[i]] = i;
    }

    cout << (ans > n ? -1 : ans) << endl;
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=0;i<n;i++)
#define vi     vector <int>
#define vl     vector <ll>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
int main()
{
    ll t,i,x,n,m=0;
    cin>>t;
    while(t--)
    {
        m=-2;
        cin>>n;
        int a[n+5];
        for(i=1;i<=n;++i)
        {
			a[i]=-1;
		}
		for(i=0;i<n;++i)
		{
			cin>>x;
			if(a[x]>-1&&(m==-2||m>(i-a[x])))
			    m=i-a[x];
			a[x]=i;
		}
		cout<<m+1<<endl;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=0;i<n;i++)
#define vi     vector <int>
#define vl     vector <ll>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
int main()
{
   ll t,i,n,a;
   cin>>t;
   while(t--)
   {
        cin>>n;
        vector <pair<ll,ll> > v;
        rep(i,n)
        {
            cin>>a;
            v.pb({a,i});
        }
       sort(v.begin(),v.end());
        if(n<2)
            cout<<"-1"<<"\n";
        else
        {
           ll m=INT_MAX;
            for(i=1;i<n;i++)
            {
                if(v[i].fr==v[i-1].fr)
                    m=min(m,v[i].sec-v[i-1].sec+1);
            }
            if(m==INT_MAX)
                m=-1;
            cout<<m<<"\n";
        }
   }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=0;i<n;i++)
#define vi     vector <int>
#define vl     vector <ll>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
int main()
{
   ll t,i,n,a;
   cin>>t;
   while(t--)
   {
        cin>>n;
        vector <pair<ll,ll> > v;
        rep(i,n)
        {
            cin>>a;
            v.pb({a,i});
        }
       sort(v.begin(),v.end());
        if(n<2)
            cout<<"-1"<<"\n";
        else
        {
           ll m=INT_MAX;
            for(i=1;i<n;i++)
            {
                if(v[i].first==v[i-1].first)
                    m=min(m,v[i].second-v[i-1].second+1);
            }
            if(m==INT_MAX)
                m=-1;
            cout<<m<<"\n";
        }
   }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=0;i<n;i++)
#define vi     vector <int>
#define vl     vector <ll>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
int main()
{
    ll t,i,x,n,m=0;
    cin>>t;
    while(t--)
    {
        m=-2;
        cin>>n;
        int a[n+5];
        for(i=1;i<=n;++i)
        {
			a[i]=-1;
		}
		for(i=0;i<n;++i)
		{
			cin>>x;
			if(a[x]>-1&&(m==-2||m>(i-a[x])))
			    m=i-a[x];
			a[x]=i;
		}

		cout<<m+1<<endl;
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=0;i<n;i++)
#define vi     vector <int>
#define vc     vector <char>
#define vl     vector <ll>
#define vb     vector <bool>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
#define M_PI   3.14159265358979323846
#define pp     pair<int,int>
#define nn     100003
#define MM     1000000000
#define MN     1000000000000000000
int main()
{
    ios;
    ll t,n,i,a;
    cin>>t;
    while(t--)
    {
        cin>>n;
        map <ll,pair<ll,vl>> m;
        rep(i,n)
        {
            cin>>a;
            m[a].fr++;
            m[a].sec.pb(i);
        }
        if(n<2)
        {
            cout<<"-1\n";
            continue;
        }
        map <ll,pair<ll,vl>> :: iterator it;
        ll x=0,ma;
        for(it=m.begin();it!=m.end();it++)
        {
            if(it->second.fr>x)
            {
                x=it->second.fr;
                ma=it->first;
            }
        }
        ll p=m[ma].sec.size(),ans=INT_MAX;
        rep(i,p-1)
            ans=min(ans,m[ma].sec[i+1]-m[ma].sec[i]+1);
        if(ans==INT_MAX)
            cout<<"-1\n";
        else
            cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll     long long
#define fr     first
#define sec    second
#define rep(i,n)  for(i=1;i<n;i++)
#define vi     vector <int>
#define vl     vector <ll>
#define pb     push_back
#define M      1000000007
#define ios    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open   freopen("input.txt","r",stdin);
#define close  freopen("output.txt","w",stdout);
int main()
{
    ll t,i,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll a[n+1];
        rep(i,n+1)
            cin>>a[i];
        if(n<2)
            cout<<"-1"<<"\n";
        else
        {
            ll b[n+1];
            memset(b,-1,sizeof(b));
            rep(i,n+1)
            {
                b[a[i]]=i;
            }
            int k,k1,p,flag=0;
            k=b[a[1]];
            for(i=2;i<n+1;i++)
            {
                p=b[a[i]]-i+1;
                if(p==k)
                {
                    k1=k;
                    flag=1;
                }
                else
                {
                    if(k==1)
                        k=max(p,k);
                    else
                    {
                        if(p!=1)
                            k=min(p,k);
                    }
                }
            }
            if(flag==1&&k1==k)
                cout<<"-1"<<"\n";
            else
                cout<<k<<"\n";

        }
    }
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    vector<int> initV;
    vector<int> afterV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    for (int j = 0; j < n; j++) {
      if (find(afterV.begin(), afterV.end(), initV[j]) == afterV.end()) {
        afterV.push_back(initV[j]);
      }
    }

    if (afterV.size() == n || n == 1) {
      cout << -1 << endl;
    } else if (afterV.size() == (n - 1)) {
      cout << n << endl;
    } else if (afterV.size() == 1) {
      cout << 2 << endl;
    } else {
      cout << 3 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    set<int> nums;

    cin >> n;

    if (n == 1) {
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n; j++) {
      cin >> number;
      const bool is_in = nums.find(number) != nums.end();
      if (!is_in) {
        nums.insert(number);
      } else {
        count++;
      }
    }

    if (count == n) {
      cout << 2 << endl;
    } else if (count ==1) {
      cout << n << endl;
    } else {
      cout << 3 << endl;
    }

    cin >> n;

  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    set<int> nums;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      const bool is_in = nums.find(number) != nums.end();
      if (!is_in) {
        nums.insert(number);
      } else {
        count++;
      }
    }

    if (n == 1) {
      cout << -1 << endl;
    } else if (count == n - 1) {
      cout << 2 << endl;
    } else if (count == 1) {
      cout << n << endl;
    } else {
      cout << 3 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    vector<int> initV;
    vector<int> afterV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    for (int j = 0; j < n; j++) {
      if (std::find(afterV.begin(), afterV.end(), initV[j]) != afterV.end()) {
        afterV.push_back(initV[j]);
      }
    }

    if (afterV.size() == n || n == 1) {
      cout << -1 << endl;
    } else if (afterV.size() == (n - 1)) {
      cout << n << endl;
    } else if (afterV.size() == 1) {
      cout << 2 << endl;
    } else {
      cout << 3 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int min = INT_MAX;
    vector<int> initV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    if (n == 1) {
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (initV[j] == initV[k]) {
          if (k - j < min) {
            min = k - j;
          }
        }
      }
    }

    if (min == n - 1) {
      cout << -1 << endl;
    } else {
      cout << min + 1 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    set<int> nums;

    cin >> n;

    if (n == 1) {
      cin >> number;
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n; j++) {
      cin >> number;
      const bool is_in = nums.find(number) != nums.end();
      if (!is_in) {
        nums.insert(number);
      } else {
        count++;
      }
    }

    if (count == n) {
      cout << 2 << endl;
    } else if (count ==1) {
      cout << n << endl;
    } else {
      cout << 3 << endl;
    }

    cin >> n;

  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <unordered_map>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int number;
    int min_dist = INT_MAX;
    int dist;
    unordered_map<int, int> nums;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      if (!(nums.find(number) == nums.end())) {
        dist = j - nums[number];
        if (dist < min_dist) {
          min_dist = dist;
        }
      }
      nums[number] = j;
    }

    if (min_dist == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << min_dist + 1 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    set<int> nums;

    cin >> n;

    if (n == 1) {
      cin >> number;
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n; j++) {
      cin >> number;
      const bool is_in = nums.find(number) != nums.end();
      if (!is_in) {
        nums.insert(number);
      } else {
        count++;
      }
    }
    cout << count << " " << n - 1 << endl;
    if (count == n - 1) {
      cout << 2 << endl;
    } else if (count == 1) {
      cout << n << endl;
    } else {
      cout << 3 << endl;
    }

    cin >> n;

  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int min = INT_MAX;
    vector<int> initV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    if (n == 1) {
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (initV[j] == initV[k]) {
          if (k - j < min) {
            min = k - j;
          }
        }
      }
    }

    if (min == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << min + 1 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    set<int> nums;

    cin >> n;

    if (n == 1) {
      cin >> number;
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n; j++) {
      cin >> number;
      const bool is_in = nums.find(number) != nums.end();
      if (!is_in) {
        nums.insert(number);
      } else {
        count++;
      }
    }

    if (count == n - 1) {
      cout << 2 << endl;
    } else if (count == 1) {
      cout << n << endl;
    } else {
      cout << 3 << endl;
    }

    cin >> n;

  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int count = 0;
    vector<int> initV;
    vector<int> afterV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    for (int j = 0; j < n; j++) {
      if (find(afterV.begin(), afterV.end(), initV[j]) == afterV.end()) {
        afterV.push_back(initV[j]);
      }
    }

    if (afterV.size() == n || n == 1) {
      cout << -1 << endl;
    } else if (afterV.size() == (n - 1)) {
      cout << n << endl;
    } else if (afterV.size() == 1 || afterV.size() == 2) {
      cout << 2 << endl;
    } else {
      cout << 3 << endl;
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n;
    int res;
    int number;
    int min = INT_MAX;
    vector<int> initV;

    cin >> n;

    for (int j = 0; j < n; j++) {
      cin >> number;
      initV.push_back(number);
    }

    if (n == 1) {
      cout << -1 << endl;
      continue;
    }

    for (int j = 0; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (initV[j] == initV[k]) {
          if (k - j < min) {
            min = k - j;
          }
        }
      }
    }

    cout << min + 1 << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e9 + 10;
int n, num[MAX] = { 0 };
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
			m[num[i]]++;
			if (m[num[i]] > maxx) {
				maxx = m[num[i]];
				max_num = num[i];
			}
		}
		if (maxx == 1) {
			printf("-1\n");
			continue;
		}
		int last = -1;
		for (int i = 0; i < n; i++) {
			if (max_num == num[i]) {
				last = i;
				break;
			}
		}
		int ans = MAXX;
		for (int i = last + 1; i < n; i++) {
			if (max_num == num[i]) {
				ans = min(ans, i - last + 1);
				last = i;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n, num[MAX] = { 0 }, place[MAX] = { 0 };
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
			m[num[i]]++;
			if (m[num[i]] > maxx) {
				maxx = m[num[i]];
				max_num = num[i];
			}
		}
		for (int i = 0; i < n; i++) {
			if (m[num[i]] == maxx && num[i] != max_num) {
				maxx = 1;
				break;
			}
		}
		if (maxx == 1) {
			printf("-1\n");
			continue;
		}
		int ans = MAXX, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (max_num == num[i]) {
				place[cnt++] = i;
			}
		}
		for (int k = 0; k < cnt; k++) {
			int max_sum1 = 1, max_sum2 = 0;
			map<int, int>m2;
			for (int i = place[k]+1; i < n; i++) {
				if (num[i] != max_num) {
					m2[num[i]]++;
					max_sum2 = max(max_sum2, m2[num[i]]);
				}
				else {
					max_sum1++;
				}
				if (max_sum1 > max_sum2&& max_sum1 != 1) {
					ans = min(ans, i - place[k] + 1);
				}
			}
			if (ans == 2) break;
		}

		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n, num[MAX] = { 0 };
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
			m[num[i]]++;
			if (m[num[i]] > maxx) {
				maxx = m[num[i]];
				max_num = num[i];
			}
		}
		for (int i = 0; i < n; i++) {
			if (m[num[i]] == maxx && num[i] != max_num) {
				maxx = 1;
				break;
			}
		}
		if (maxx == 1) {
			printf("-1\n");
			continue;
		}
		int last = 0;
		for (int i = 0; i < n; i++) {
			if ( max_num == num[i]) {
				last = i;
				break;
			}
		}
		int ans = MAXX;
		for (int i = last + 1; i < n; i++) {
			if (max_num == num[i]) {
				ans = min(ans, i - last + 1);
				last = i;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n;
struct node {
	int x, next;
}num[MAX];
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		for (int i = 0; i < n; i++) num[i].next = -1;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i].x);
			num[m[num[i].x]].next = i;
			m[num[i].x] = i;
		}
		int ans = MAXX;
		for (int i = 0; i < n; i++) {
			if (num[i].next != -1) {
				ans = min(ans, num[i].next - i + 1);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n;
struct node {
	int x, next;
}num[MAX];
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		for (int i = 1; i <= n; i++) num[i].next = -1;
		map<int, int>m;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &num[i].x);
			if (m[num[i].x]) {
				num[m[num[i].x]].next = i;
				m[num[i].x] = i;
			}
			else m[num[i].x] = i;
		}
		int ans = MAXX;
		for (int i = 1; i <= n; i++) {
			if (num[i].next != -1) {
				ans = min(ans, num[i].next - i + 1);
			}
		}
		if (ans == MAXX) printf("-1\n");
		else printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n, num[MAX] = { 0 }, place[MAX] = { 0 };
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
			m[num[i]]++;
			if (m[num[i]] > maxx) {
				maxx = m[num[i]];
				max_num = num[i];
			}
		}
		for (int i = 0; i < n; i++) {
			if (m[num[i]] == maxx && num[i] != max_num) {
				maxx = 1;
				break;
			}
		}
		if (maxx == 1) {
			printf("-1\n");
			continue;
		}
		int ans = MAXX, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (max_num == num[i]) {
				place[cnt++] = i;
			}
		}
		for (int k = 0; k < cnt; k++) {
			int max_sum1 = 1, max_sum2 = 0;
			map<int, int>m2;
			for (int i = place[k]+1; i < n; i++) {
				if (num[i] != max_num) {
					m2[num[i]]++;
					max_sum2 = max(max_sum2, m2[num[i]]);
				}
				else {
					max_sum1++;
				}
				if (max_sum1 > max_sum2&& max_sum1 >= 1) {
					ans = min(ans, i - place[k] + 1);
					break;
				}
			}
			if (ans == 2) break;
		}

		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e9 + 10;
int n, num[MAX] = { 0 };
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
			m[num[i]]++;
			if (m[num[i]] > maxx) {
				maxx = m[num[i]];
				max_num = num[i];
			}
		}
		for (int i = 0; i < n; i++) {
			if (m[num[i]] == maxx && num[i] != max_num) {
				maxx = 1;
				break;
			}
		}
		if (maxx == 1) {
			printf("-1\n");
			continue;
		}
		int last = -1;
		for (int i = 0; i < n; i++) {
			if (max_num == num[i]) {
				last = i;
				break;
			}
		}
		int ans = MAXX;
		for (int i = last + 1; i < n; i++) {
			if (max_num == num[i]) {
				ans = min(ans, i - last + 1);
				last = i;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int MAXX = 1e8 + 10;
int n;
struct node {
	int x, next;
}num[MAX];
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d", &n);
		int maxx = 0, max_num = 0;
		for (int i = 0; i < n; i++) num[i].next = -1;
		map<int, int>m;
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i].x);
			if (m[num[i].x]) {
				num[m[num[i].x]].next = i;
				m[num[i].x] = i;
			}
		}
		int ans = MAXX;
		for (int i = 0; i < n; i++) {
			if (num[i].next != -1) {
				ans = min(ans, num[i].next - i + 1);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n), count(n+1, 0);
        for0(i, n){
            cin>>arr[i];
            count[arr[i]]++;
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
            if(count[arr[i]] >= 2){
                int j = i+1;
                for(; j < n; j++)
                    if(arr[j] == arr[i])
                        break;

                if((j-i+1) < ans)
                   ans = j-i+1;
            }
            count[arr[i]]--;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n);
        for0(i, n){
            cin>>arr[i];
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
            vi count(n+1, 0);
            count[arr[i]]++;
            int j = i+1;
            for(; j < n; j++){
                count[arr[j]]++;
                if(count[arr[j]] == 2)
                    break;
            }
            if((j-i+1) < ans)
                ans = j-i+1;

            if(ans == 2)
                break;
        }
        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n), count(n+1, 0);
        for0(i, n){
            cin>>arr[i];
            count[arr[i]]++;
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
            if(count[arr[i]] >= 2){
                int j = i+1;
                for(; j < n; j++)
                    if(arr[j] == arr[i])
                        break;

                if((j-i+1) < ans)
                   ans = j-i+1;
            }
            count[arr[i]]--;
            if(ans == 2)
               break;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n), count(n+1, 0);
        for0(i, n){
            cin>>arr[i];
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
           count[arr[i]]++;
           if(count[arr[i]] >= 2){
               int j = i-1;
               for(; j >= 0; j--)
                  if(arr[j] == arr[i])
                     break;

                if((i-j+1) < ans)
                  ans = i-j+1;
           }
           if(ans == 2)
              break;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n);
        for0(i, n){
            cin>>arr[i];
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
            vi count(n+1, 0);
            count[arr[i]]++;
            int j = i+1;
            for(; j < n; j++){
                count[arr[j]]++;
                if(count[arr[j]] == 2)
                    break;
            }
            if(j < n && (j-i+1) < ans)
                ans = j-i+1;

            if(ans == 2)
                break;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n), count(n+1, 0);
        vvi adj(n+1);
        for0(i, n){
            cin>>arr[i];
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
           count[arr[i]]++;
           if(count[arr[i]] >= 2){
              int j = adj[arr[i]][-1];
              if((i-j+1) < ans)
                 ans = i-j+1;
           }
           adj[arr[i]].pb(i);
           if(ans == 2)
              break;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>  // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;  // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b)  (a > b) ?a :b  // max of 2 elements
#define min(a, b)  (a < b) ?a :b  // min of 2 elements

//short hand for usual tokens
#define pb              push_back
#define fi              first
#define se              second
#define mp              make_pair
#define mii             map<int,int>
#define pqb             priority_queue<int>   // Max Heap
#define pqs             priority_queue<int,vi,greater<int> >   // Min Heap
#define setbits(x)      __builtin_popcountll(x)   // returns the number of set bits in the binary representation of x
#define zrobits(x)      __builtin_ctzll(x)   // returns the number of zeroes before the first set bit in the binary representation of x
#define mod             1000000007   // 1e9 + 7
#define inf             1e18   // pow(10, 18)
#define mk(arr,n,type)  type *arr=new type[n];   // generates a dynamic array
#define w(x)            int x; cin>>x; while(x--)   // for taking test cases
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());   // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;   // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi arr(n), count(n+1, 0);
        vvi adj(n+1);
        for0(i, n){
            cin>>arr[i];
        }

        int ans = mod;
        for(int i = 0; i < n; i++){
           count[arr[i]]++;
           if(count[arr[i]] >= 2){
              int j = *adj[arr[i]].rbegin();
              if((i-j+1) < ans)
                 ans = i-j+1;
           }
           adj[arr[i]].pb(i);
           if(ans == 2)
              break;
        }
        if(ans == mod)
            ans = -1;

        cout<<ans<<"\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		vector<ll> arr[n];
		for(ll i=0;i<n;i++){
			ll j;
			cin >> j; j--;
			arr[j].push_back(i);
		}
		ll k = n+1;
		for(ll i=0;i<n;i++){
			for(ll j=1;j<arr[i].size();j++){
				k=min(k,arr[i][j]-arr[i][j-1]+1);
			}
		}
		if(k==n+1){
			cout << -1;
		}else{
			cout << k;
		}
		cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		ll arr[n];
		ll f[n+1] = {0};
		ll min = LLONG_MAX;
		for(ll i=0;i<n;i++){
			ll x = 0;
			re(x);
			arr[i] = x;
			f[x]++;
			if(f[x]>1&&i+1<min){
				min = i+1;
			}
		}
		for(ll i=1;i<n-1;i++){
			ll cf[n] = {0};
			for(ll j=i;j<n;j++){
				ll x = arr[j];
				cf[x]++;
				if(cf[x]>1&&(j-i)+1<min){
					min = (j-i)+1;
				}
			}
		}
		if(n==1||min==LLONG_MAX){
			cout << -1 << endl;
		}else{
			cout << min << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		ll arr[n];
		ll f[n+1] = {0};
		ll min = LLONG_MAX;
		for(ll i=0;i<n;i++){
			ll x = 0;
			re(x);
			arr[i] = x;
			f[x]++;
			if(f[x]>1&&i+1<min){
				min = i+1;
			}
		}
		for(ll i=1;i<n;i++){
			ll cf[n] = {0};
			for(ll j=i;j<n-1;j++){
				ll x = arr[j];
				cf[x]++;
				if(cf[x]>1&&(j-i)+1<min){
					min = (j-i)+1;
				}
			}
		}
		if(n==1||min==LLONG_MAX){
			cout << -1 << endl;
		}else{
			cout << min << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		ll arr[n];
		ll f[n] = {0};
		ll min = LLONG_MAX;
		for(ll i=0;i<n;i++){
			ll x = 0;
			re(x);
			arr[i] = x;
			f[x]++;
			if(f[x]>1&&i+1<min){
				min = i+1;
			}
		}
		for(ll i=1;i<n;i++){
			ll cf[n] = {0};
			for(ll j=i;j<n;j++){
				ll x = arr[j];
				cf[x]++;
				if(cf[x]>1&&(j-i)+1<min){
					min = (j-i)+1;
				}
			}
		}
		if(n==1||min==LLONG_MAX){
			cout << -1 << endl;
		}else{
			cout << min << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		ll arr[n];
		ll f[n] = {0};
		ll min = LLONG_MAX;
		for(ll i=0;i<n;i++){
			ll x = 0;
			re(x);
			arr[i] = x;
			f[x]++;
			if(f[x]>1&&f[x]<min){
				min = i+1;
			}
		}
		for(ll i=1;i<n;i++){
			ll cf[n] = {0};
			for(ll j=i;j<n;j++){
				ll x = arr[j];
				cf[x]++;
				if(cf[x]>1&&cf[x]<min){
					min = (j-i)+1;
				}
			}
		}
		if(n==1||min==LLONG_MAX){
			cout << -1 << endl;
		}else{
			cout << min << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		vector<ll> arr[n];
		for(ll i=0;i<n;i++){
			ll j;
			cin >> j;
			arr[j].push_back(i);
		}
		ll k = n+1;
		for(ll i=0;i<n;i++){
			for(ll j=1;j<arr[i].size();j++){
				k=min(k,arr[i][j]-arr[i][j-1]+1);
			}
		}
		if(k==n-1){
			cout << -1;
		}else{
			cout << k;
		}
		cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		vector<ll> arr[n];
		for(ll i=0;i<n;i++){
			ll j;
			cin >> j;
			arr[j].push_back(i);
		}
		ll k = n+1;
		for(ll i=0;i<n;i++){
			for(ll j=1;j<arr[i].size()lj++){
				k=min(k,arr[i][j]-arr[i][j-1]+1);
			}
		}
		if(k==n-1){
			cout << -1;
		}else{
			cout << k;
		}
		cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) ll t; re(t); while(t--)

int main(){
	te(t){
		ll n;
		re(n);
		ll arr[n];
		ll f[n+1] = {0};
		ll min = LLONG_MAX;
		for(ll i=0;i<n;i++){
			ll x = 0;
			re(x);
			arr[i] = x;
			f[x]++;
			if(f[x]>1&&i+1<min){
				min = i+1;
			}
		}
		for(ll i=1;i<n;i++){
			ll cf[n] = {0};
			for(ll j=i;j<n;j++){
				ll x = arr[j];
				cf[x]++;
				if(cf[x]>1&&(j-i)+1<min){
					min = (j-i)+1;
				}
			}
		}
		if(n==1||min==LLONG_MAX){
			cout << -1 << endl;
		}else{
			cout << min << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long p,t,i,c,k,x,j,y,a,m,n,b,sum1,sum2;
int main()
{
    cin>>t;
    for(p=0;p<t;p++)
    {
        m=0;
        cin>>n;
        vector< long long >v(n);
        for(i=0;i<n;i++)
            cin>>v[i];
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(v[j]==v[i])
                {
                    b=j-i+1;
                    break;
                }
            }
            if(m==0)
                m=b;
            else
                m=min(m,b);

        }
        if(m==0)
        {
            cout<<-1<<endl;
        }
        else
            cout<<m<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long p,t,i,c,k,x,j,y,a,m,n,b,sum1,sum2;
int main()
{
    cin>>t;
    for(p=0;p<t;p++)
    {
        m=0;
        cin>>n;
        vector< long long >v(n);
        for(i=0;i<n;i++)
            cin>>v[i];
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(v[j]==v[i])
                {
                    b=j-i+1;
                    break;
                }
            }
            if(m==0)
                m=b;
            else
                m=min(m,b);
            if(m==2)
                break;
        }
        if(m==0)
        {
            cout<<-1<<endl;
        }
        else
            cout<<m<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long p,t,i,c,k,x,j,y,a,m,n,b,sum1,sum2;
int main()
{
    cin>>t;
    for(p=0;p<t;p++)
    {
        m=0;
        c=0;
        b=0;
        cin>>n;
        vector< long long >v(n);
        for(i=0;i<n;i++)
            cin>>v[i];
        vector< long long >v1(v);
        sort(v1.begin(),v1.end());
        for(i=0;i<n-1;i++)
        {
            if(v1[i]==v[i+1])
            {
                c++;
                break;
            }
        }

        if(c==0)
        {
            cout<<-1<<endl;

        }
        else
        {
            for(i=0;i<n-1;i++)
            {
                b=0;
                for(j=i+1;j<n;j++)
                {
                    if(v[j]==v[i])
                    {
                        b=j-i+1;
                        break;
                    }

                }
                if(b!=0)
                {
                    if(m==0)
                        m=b;
                    else
                        m=min(m,b);
                }

            }
            if(m==0)
            {
                cout<<-1<<endl;
            }
            else
                cout<<m<<endl;
            }
    }
}


#include<bits/stdc++.h>
using namespace std;
long long n,m,i,k,t,c,a1,x,sum,y;
int main()
{
    cin>>t;
    for(i=0;i<t;i++)
    {
        m=0;
        cin>>n;
        vector< int >v(n);
        for(k=0;k<n;k++)
        {
            cin>>v[k];
        }
        for(k=0;k<n;k++)
        {
            for(x=k+1;x<n;x++)
            {
                if(v[k]==v[x])
                {
                    if(sum==0)
                    {
                        sum=abs(x-k+1);
                        break;
                    }
                    else
                    {
                        if(abs(x-k+1)<sum )
                        {
                            sum=abs(x-k+1);
                            break;
                        }
                    }
                }
            }
        }
        if(sum==0)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<sum<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
long long p,t,i,c,k,x,j,y,a,m,n,b,sum1,sum2;
int main()
{
    cin>>t;
    for(p=0;p<t;p++)
    {
        m=0;
        cin>>n;
        vector< long long >v(n);
        for(i=0;i<n;i++)
            cin>>v[i];
        for(i=0;i<n-1;i++)
        {
            b=0;
            for(j=i+1;j<n;j++)
            {
                if(v[j]==v[i])
                {
                    b=j-i+1;
                    break;
                }

            }
            if(b!=0)
            {
                if(m==0)
                    m=b;
                else
                    m=min(m,b);
            }

        }
        if(m==0)
        {
            cout<<-1<<endl;
        }
        else
            cout<<m<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long p,t,i,c,k,x,j,y,a,m,n,b,sum1,sum2;
int main()
{
    cin>>t;
    for(p=0;p<t;p++)
    {
        m=0;
        c=0;
        b=0;
        cin>>n;
        vector< long long >v(n);
        for(i=0;i<n;i++)
            cin>>v[i];
        vector< long long >v1(v);
        sort(v1.begin(),v1.end());
        for(i=0;i<n-1;i++)
        {
            if(v1[i]==v1[i+1])
            {
                c++;
                break;
            }
        }

        if(c==0)
        {
            cout<<-1<<endl;

        }
        else
        {
            for(i=0;i<n-1;i++)
            {
                b=0;
                for(j=i+1;j<n;j++)
                {
                    if(v[j]==v[i])
                    {
                        b=j-i+1;
                        break;
                    }

                }
                if(b!=0)
                {
                    if(m==0)
                        m=b;
                    else
                        m=min(m,b);
                }

            }
            if(m==0)
            {
                cout<<-1<<endl;
            }
            else
                cout<<m<<endl;
            }
    }
}


#include<bits/stdc++.h>
using namespace std;
long long n,m,i,k,t,c,a1,x,sum,y;
int main()
{
    cin>>t;
    for(i=0;i<t;i++)
    {
        m=0;
        cin>>n;
        vector< int >v(n);
        for(k=0;k<n;k++)
        {
            cin>>v[k];
        }
        for(k=0;k<n;k++)
        {
            for(x=k+1;x<n;x++)
            {
                if(v[k]==v[x])
                {
                    if(sum==0)
                    {
                        sum=abs(x-k+1);
                        break;
                    }
                    else
                    {
                        if(abs(x-k+1)<sum )
                        {
                            sum=abs(x-k+1);
                        }
                    }
                }
            }
        }
        if(sum==0)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<sum<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,i,t;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector< ll >v(n);
        for(i=0;i<n;i++)
        {
            cin>>v[i];
        }
        map< ll, vector< ll > >m;
        for(i=0;i<n;i++)
        {
            m[v[i]].push_back(i);
        }
        ll res=INT_MAX;
        for(map< ll, vector< ll > >::iterator it=m.begin();it!=m.end();it++)
        {
            //cout<<it->first<<" "<<(it->second).size()<<endl;
            if((it->second).size()>1)
            {
                for(ll j=1;j<(it->second).size();j++)
                {
                    res=min(res,abs((it->second)[j]-(it->second)[j-1])+1);
                    //cout<<(it->second)[i]<<" "<<(it->second)[i-1]<<endl;
                }
            }
            else
            {
                continue;
            }
        }
        if(res!=INT_MAX)
            cout<<res<<endl;
        else
            cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long mi=INT_MAX;
		vector<long long> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(value);
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<a.size())
	{
	    if(a[ptr1]==a[ptr2] && ptr2<a.size())
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<a.size())
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }

	}

	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
}
return 0;
}


/*JANVI PATEL, DAIICT ,INDIA  */
#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define dob double
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define deb(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define ff first
#define ss second
#define forr(z,y) for(int i=z;i<y;i++)
#define rfor(z,y) for(int i=z;i>=y;i--)
#define  Vp vector<pair<ll,ll>>
#define V vector
#define sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define M map
#define len length()
#define fix fixed<<setprecision (15)
const int MOD=1e9+7;
const int nax=1e6+10;
vector<int> tree(4*nax);
ll i,j,x,y,k,val;

bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
    return (a.second<b.second);
}
class bitmask
{
public:
void PowerSet(char *set, int set_size)
{
    unsigned int pow_set_size = pow(2, set_size);
    int counter, j;

    for(counter = 0; counter < pow_set_size; counter++)
    {
    for(j = 0; j < set_size; j++)
    {
        if(counter & (1 << j))
            cout << set[j];
    }
    cout << endl;
    }
}
};
ll powerLL(ll x, ll n)
{

    ll result = 1;
    while (n) {
        if (n & 1)
            result = result * x % MOD;
        n = n / 2;
        x = x * x % MOD;
    }
   return result;
}

int32_t main()
{
        FAST
        ll t;
        cin>>t;
        while(t--)
        {
            ll n;
            cin>>n;
            ll ans=INT_MAX,i=0;
            unordered_map<ll,ll> mp;
            forr(0,n)
            {
                cin>>val;
                if(mp.find(val)!=mp.end())
                {
                    ans=min(ans,i-mp[val]+1);
                }
                mp[val]=i;
            }
            if(ans==INT_MAX)
            ans=-1;
            cout<<ans<<endl;

        }
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long mi=INT_MAX;
		vector<long long> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(value);
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<a.size())
	{
	    if(a[ptr1]==a[ptr2] && ptr2<a.size())
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<a.size())
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    if(mi==2)
	    {
	        cout<<mi<<endl;
	        break;
	    }
	}

	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main() {
	long long t,n,i,j;
	cin>>t;
	int flag=0;

	while(t--)
	 {
	    flag=0;
	    cin>>n;
	    long long mi=INT_MAX;
		vector<pair<long long,long long>> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(make_pair(i,value));
	}
	  sort(a.begin(), a.end(), sortbysec);
      for(i=0;i<a.size()-1;i++)
	{
	   if(a[i].second==a[i+1].second)
	       mi=min(abs((a[i].first-a[i+1].first))+1,mi);
	   if(mi==2)
	   {
	       flag=1;
	       cout<<mi<<endl;
	       break;
	   }
	}

	if(flag==0)
	{
	if(mi==INT_MAX)
	cout<<-1<<endl;
	else
	cout<<mi<<endl;
	}
}

return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long mi=INT_MAX;
		vector<long long> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(value);
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<n)
	{
	    if(a[ptr1]==a[ptr2])
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<n)
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }

	}

	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;

		while(t--)
	{
	    cin>>n;
	    int flag=0;
	    long long mi=INT_MAX;
		vector<long long> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(value);
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<a.size())
	{
	    if(ptr2<a.size() && a[ptr1]==a[ptr2])
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<a.size())
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    if(mi==2)
	    {
	        cout<<mi<<endl;
	        flag=1;
	        break;
	    }
	}

	if(flag==0)
	{
	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
	}
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long mi=INT_MAX;
	    long long a[n];
		vector<long long> v;
	for(i=0;i<n;i++)
	{
	    cin>>a[i];
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<n)
	{
	    if(a[ptr1]==a[ptr2])
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<n)
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }

	}

	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long mi=INT_MAX;
		vector<long long> a;
	for(i=0;i<n;i++)
	{
	    long long value;
	    cin>>value;
	    a.push_back(value);
	}
	long long ptr1=0,ptr2=1;
	while(ptr1<a.size())
	{
	    if(ptr2<a.size() && a[ptr1]==a[ptr2])
	    {
	        mi=min(ptr2-ptr1+1,mi);
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    else if(ptr2<a.size())
	    {
	        ptr2++;
	    }
	    else
	    {
	        ptr1++;
	        ptr2=ptr1+1;
	    }
	    if(mi==2)
	    {
	        cout<<mi<<endl;
	        break;
	    }
	}

	if(mi==INT_MAX)
	cout<<-1<<endl;
    else
	 cout<<mi<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
	long long t,n,i,j;
	cin>>t;
		while(t--)
	{
	    cin>>n;
	    long long a[n];
		vector<long long> v;
	for(i=0;i<n;i++)
	{
	    cin>>a[i];
	}
	for(i=0;i<n;i++)
	{
	    for(j=i+1;j<n;j++)
	    {
	        if(a[i]==a[j])
	        {
	            v.push_back(j-i+1);
	        }
	    }
	}
	for(auto k=v.begin();k!=v.end();k++)
	{
	    if(*k==0)
	    {
	        v.erase(k);
	    }

	}

	if(v.size()>0)
	cout<<*min_element(v.begin(), v.end())<<endl;
	else
	cout<<-1<<endl;
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n], nm[n] = {0};
        for (ll i = 0; i < n; i++) {
            cin >> arr[i];
        }
        nm[arr[0]-1]++;
        ll u = 1, b = 0, c = n+1;
        while (u<n) {
            nm[arr[u]-1]++;
            if (nm[arr[u]-1] > 1) {
                //cout << u << "g ";
                while (arr[u] != arr[b]) {
                    nm[arr[b]-1]--;
                    b++;
                }
                if ( c > u-b+1 && (u - b) != 0) {
                    c = u - b + 1;
                }
                //cout << u - b + 1 << ' ';
                b++;
                //u = b+1;
            }
            u++;
        }
        if (c != n+1) {
            cout << c;
        } else {
            cout << -1;
        }
        cout << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n];
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        vector <ll> v;
        for (int i = 0; i < n; i++) {
            for (int j = i; i < n; i++) {
                if (arr[i] == arr[j]) {
                    v.push_back(i-j);
                }
            }
        }
        if (!v.size()) {
            cout << -1;
        } else {
            sort(v.begin(), v.end());
            cout << v[0];
        }
        cout << "\n";

        }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n];
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        vector <ll> v;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                if (arr[i] == arr[j]) {
                    v.push_back(j - i);
                    break;
                } else {
                    v.push_back(n);
                }
            }
        }
        if (!v.size()) {
            cout << -1;
        } else {
            sort(v.begin(), v.end());
            if (v[0] == n) {
                cout << -1;
            } else {
                cout << v[0]+1;
            }
        }
        cout << "\n";

        }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n];
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        vector <ll> v;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                if (arr[i] == arr[j]) {
                    v.push_back(j - i);
                    break;
                }
            }
        }
        if (!v.size()) {
            cout << -1;
        } else {
            sort(v.begin(), v.end());
            cout << v[0]+1;
        }
        cout << "\n";

        }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n], nm[n] = {0};
        for (ll i = 0; i < n; i++) {
            cin >> arr[i];
        }
        nm[arr[0]-1]++;
        ll u = 1, b = 0, c = n+1;
        while (u<n) {
            nm[arr[u]-1]++;
            if (nm[arr[u]-1] > 1) {
                //cout << u << "g ";
                while (arr[u] != arr[b]) {
                    nm[arr[b]-1]--;
                    b++;
            }
                if ( c > u-b+1) {
                    c = u - b + 1;
                }
                //cout << u - b + 1 << ' ';
                b++;
                u = b+1;
                nm[n] = {0};
                for (ll i = 0; i < n; i++) {
                    nm[i] = 0;
                }
                nm[arr[b]-1]++;
            }
            u++;
        }
        if (c != n+1) {
            cout << c;
        } else {
            cout << -1;
        }
        cout << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n], nm[n] = {0};
        for (ll i = 0; i < n; i++) {
            cin >> arr[i];
        }
        nm[arr[0]-1]++;
        ll u = 1, b = 0, c = n+1;
        while (u<n) {
            nm[arr[u]-1]++;
            if (nm[arr[u]-1] > 1) {
                //cout << u << "g ";
                while (arr[u] != arr[b]) {
                    nm[arr[b]-1]--;
                    b++;
            }
                if ( c > u-b+1) {
                    c = u - b + 1;
                }
                //cout << u - b + 1 << ' ';
                b = u;
                u = b;
                nm[n] = {0};
                for (ll i = 0; i < n; i++) {
                    nm[i] = 0;
                }
                nm[arr[b]-1]++;
            }
            u++;
        }
        if (c != n+1) {
            cout << c;
        } else {
            cout << -1;
        }
        cout << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll arr[n], nm[n] = {0};
        for (ll i = 0; i < n; i++) {
            cin >> arr[i];
        }
        nm[arr[0]-1]++;
        ll u = 1, b = 0, c = n+1;
        while (u<n) {
            nm[arr[u]-1]++;
            if (nm[arr[u]-1] > 1) {
                //cout << u << "g ";
                while (arr[u] != arr[b]) {
                    nm[arr[b]-1]--;
                    b++;
                }
                if ( c > u-b+1) {
                    c = u - b + 1;
                }
                //cout << u - b + 1 << ' ';
                b++;
                //u = b+1;
            }
            u++;
        }
        if (c != n+1) {
            cout << c;
        } else {
            cout << -1;
        }
        cout << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        for(auto it=m.begin();it!=m.end();it++)
        {
        if(it->second>ans){
            ans=it->second;
        sum=it->first;}
        }
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(int i=0;i<v[sum].size()-1;i++)
        {
            int p=v[sum][i]-v[sum][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[sum][i]-v[sum][i+1]));
        }
        if(mi==INT_MAX)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        set<int>s;
        for(auto it=m.begin();it!=m.end();it++)
        {
        if(it->second=>ans&&it->second>0){
            ans=it->second;
        s.insert(it->first);}
        }
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(mi==INT_MAX)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];
set<int>s;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);
            s.insert(a[i]);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        //set<int>s;
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(s.size()==n)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        set<int>s;
        for(auto it=m.begin();it!=m.end();it++)
        {
        if(it->second>=ans&&it->second>0){
            ans=it->second;
        s.insert(it->first);}
        }
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(mi==INT_MAX)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];
set<int>s;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);
            s.insert(a[i]);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        //set<int>s;
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(mi==INT_MAX)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        set<int>s;
        for(auto it=m.begin();it!=m.end();it++)
        {
        if(it->second>ans&&it->second>0){
            ans=it->second;
        s.insert(it->first);}
        }
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(mi==INT_MAX)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include<bits/stdc++.h>
using namespace std;
vector<int>v[2*100055];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
       // v.clear();
    //    vector<int>v[2*100055];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);
            s.insert(a[i]);

            m[a[i]]++;
        }
        int ans=0;
        int sum=0;
        set<int>s;
        //cout<<sum<<endl;
        //cout<<v[sum].size()<<endl;
        int mi=INT_MAX;
        for(auto it=s.begin();it!=s.end();it++){
        for(int i=0;i<v[*it].size()-1;i++)
        {
            int p=v[*it][i]-v[*it][i+1];
         //   cout<<p<<endl;
           // cout<<555;
            mi=min(mi,abs(v[*it][i]-v[*it][i+1]));
        }}
        if(s.size()==n)
        {
            cout<<-1<<endl;
        }
        else{
            cout<<mi+1<<endl;
        }
        for(int i=0;i<n;i++)
        {
            v[a[i]].clear();
        }
    }

    }


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		if(l<2){
			int pusto;
			scanf("%d", &pusto);
			printf("-1\n");
			continue;
		}
		int min = 100000000;
		vector <int> myvec(1000000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			if(min==2){
				continue;
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		printf("%d\n", min);
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		if(l<2){
			int pusto;
			scanf("%d", &pusto);
			printf("-1\n");
			continue;
		}
		int min = 100000000;
		vector <int> myvec(200000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			if(min==2){
				continue;
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		printf("%d\n", min);
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		if(l<2){
			int pusto;
			scanf("%d", &pusto);
			printf("-1\n");
			continue;
		}
		int min = 100000000;
		vector <int> myvec(1000000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		printf("%d\n", min);
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		int min = 100000000;
		vector <int> myvec(200000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			if(min==2){
				continue;
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		if(min!=100000000){
			printf("%d\n", min);
		} else{
			printf("-1\n");
		}
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		int min = 100000000;
		vector <int> myvec(200000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			if(min==2){
				continue;
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		if(min!=100000000){
			printf("%d\n", min);
		} else{
			printf("-1");
		}
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n;i++){
		int l;
		scanf("%d", &l);
		int min = 100000000;
		vector <int> myvec(200000);
		for(int k = 0; k<l;k++){
			int c;
			scanf("%d", &c);
			if(myvec[c]!=0){
				if(myvec[c]+1<min){
					min = myvec[c]+1;
				}
			}
			myvec[c]=1;
			for(int g = 1; g<=myvec.size();g++){
				if(myvec[g]!=0 and g!=c){
					myvec[g]++;
				}
			}
		}
		if(min!=100000000){
			printf("%d\n", min);
		} else{
			printf("-1\n");
		}
    }
    return 0;
}