/******************************************************/
/******************************************************/
/**                                                  **/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         REAZ AHAMMED CHOWDHURY - reaziii         **/
/**  Department of Computer Science and Engineering  **/
/*         INSTITUTE OF SCIENCE AND TECHNOLOGY       **/
/**                                                  **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x)     printf("Case %d: ",x++)
#define pn           puts("")
#define fri(f)       for(int i=0;i<f;i++)
#define frj(f)       for(int j=0;j<f;j++)
#define reset(x)     memset(x,-1,sizeof(x))
#define all(x)       x.begin(),x.end()
#define input        freopen("input.txt","r",stdin);
#define output       freopen("output.txt","w",stdout)
#define infi         INT_MAX
#define linfi        LLONG_MAX
#define pii          pair<int,int>
#define pch          pair<char,char>
#define pll          pair<ll,ll>
#define mgraph       map<int,vector<int> >
#define mp           make_pair
#define pb           push_back
#define clr(x)       memset(x,0,sizeof(x))
#define frx(x,y)     for(int x=0;x<y;x++)
#define fro(i,x,y)   for(int i=x;i<y;i++)
#define ech(x,a)     for(auto &x : a)
#define ff           first
#define ss           second
#define sz(x)        x.size()
#define vi           vector<int>
#define vl           vector<ll>
#define pf           push_front
#define pi           acos(-1.0)

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class J> void print(J& x) {cout << x << " ";}
template<class K> void printn(K& x) {print(x); puts("");}
template<class H> void print(vector<H> &x) {ech(a, x) print(a); puts("");}
template<class J, class... H> void print(J h, H... t) {print(h); print(t...); puts("");}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
	cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
	debug(h);
	debug(t...);
	cerr << endl;
#endif

}

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;

bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}

ll geti() {ll x; read(x); return x;}

void read(int *aa, int sz) {fri(sz) scanf("%d", (aa + i));}
void read(ll *aa, int sz) {fri(sz) scanf("%lld", (aa + i));}
void read(ll *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%lld", (aa + i));}
void read(int *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%d", (aa + i));}
const ll mod =  1e9 + 7;
const ll N   =  1e6 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};

//......................___Start from here___.....................//
//......................_____________________....................//

int solve() {

	ll n;
	read(n);
	vl arr(n);

	read(arr);
	mgraph graph;

	fri(n) {
		graph[arr[i]].pb(i);
	}

	vi mx(n + 1, -1);

	for (int i = 1; i <= n; i++) {
		if (graph[i].size()) {
			mx[i] = graph[i][0] + 1;
		}
		for (int j = 0; j < graph[i].size(); j++) {
			mx[i] = max(mx[i], (int)(graph[i][j] - graph[i][j - 1]));
		}
		if (graph[i].size())mx[i] = max(mx[i], (int)(n - graph[i].back()));
	}

	int ln = n;
	vi res(n + 1, -1);
	for (int i = 1; i <= n; i++) {
		int x = mx[i];
		if (x == -1) continue;

		while (ln >= x) {
			res[ln] = i;
			ln--;
			if (ln <= 0) break;
		}

	}

	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;

	return 0;
}
int main(int argc, char* argv[]) {

	if (argc <= 1) {
#ifndef ONLINE_JUDGE
		input;
		output;
#endif
#ifdef ONLINE_JUDGE
		ios_base::sync_with_stdio(false);
		cin.tie(0);
#endif
	}
	int cs = 1, cn = 1;
	read(cs);
	while (cs--) {
		solve();

	}
}


/******************************************************/
/******************************************************/
/**                                                  **/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         REAZ AHAMMED CHOWDHURY - reaziii         **/
/**  Department of Computer Science and Engineering  **/
/*         INSTITUTE OF SCIENCE AND TECHNOLOGY       **/
/**                                                  **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x)     printf("Case %d: ",x++)
#define pn           puts("")
#define fri(f)       for(int i=0;i<f;i++)
#define frj(f)       for(int j=0;j<f;j++)
#define reset(x)     memset(x,-1,sizeof(x))
#define all(x)       x.begin(),x.end()
#define input        freopen("input.txt","r",stdin);
#define output       freopen("output.txt","w",stdout)
#define infi         INT_MAX
#define linfi        LLONG_MAX
#define pii          pair<int,int>
#define pch          pair<char,char>
#define pll          pair<ll,ll>
#define mgraph       map<int,vector<int> >
#define mp           make_pair
#define pb           push_back
#define clr(x)       memset(x,0,sizeof(x))
#define frx(x,y)     for(int x=0;x<y;x++)
#define fro(i,x,y)   for(int i=x;i<y;i++)
#define ech(x,a)     for(auto &x : a)
#define ff           first
#define ss           second
#define sz(x)        x.size()
#define vi           vector<int>
#define vl           vector<ll>
#define pf           push_front
#define pi           acos(-1.0)

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class J> void print(J& x) {cout << x << " ";}
template<class K> void printn(K& x) {print(x); puts("");}
template<class H> void print(vector<H> &x) {ech(a, x) print(a); puts("");}
template<class J, class... H> void print(J h, H... t) {print(h); print(t...); puts("");}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
	cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
	debug(h);
	debug(t...);
	cerr << endl;
#endif

}

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;

bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}

ll geti() {ll x; read(x); return x;}

void read(int *aa, int sz) {fri(sz) scanf("%d", (aa + i));}
void read(ll *aa, int sz) {fri(sz) scanf("%lld", (aa + i));}
void read(ll *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%lld", (aa + i));}
void read(int *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%d", (aa + i));}
const ll mod =  1e9 + 7;
const ll N   =  1e6 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};

//......................___Start from here___.....................//
//......................_____________________....................//

int solve() {

	ll n;
	read(n);
	vl arr(n);

	read(arr);
	mgraph graph;

	fri(n) {
		// if (graph[arr[i]].empty()) graph[arr[i]].pb(0);
		graph[arr[i]].pb(i);
	}

	vi mx(n + 1, -1);
	vi last(n + 1, 0);

	for (int i = 1; i <= n; i++) {
		if (graph[i].size()) {
			mx[i] = graph[i][0] + 1;
		}
		for (int j = 0; j < graph[i].size(); j++) {
			mx[i] = max(mx[i], (int)(graph[i][j] - graph[i][j - 1]));
		}
		if (graph[i].size())mx[i] = max(mx[i], (int)(n - graph[i].back()));
	}

	int ln = n;
	vi res(n + 1, -1);
	for (int i = 1; i <= n; i++) {
		int x = mx[i];
		if (x == -1) continue;

		while (ln >= x) {
			res[ln] = i;
			ln--;
		}
		if (ln < 0) break;

	}

	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;

	return 0;
}
int main(int argc, char* argv[]) {

	if (argc <= 1) {
#ifndef ONLINE_JUDGE
		input;
		output;
#endif
#ifdef ONLINE_JUDGE
		ios_base::sync_with_stdio(false);
		cin.tie(0);
#endif
	}
	int cs = 1, cn = 1;
	read(cs);
	while (cs--) {
		solve();

	}
}


/******************************************************/
/******************************************************/
/**                                                  **/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         REAZ AHAMMED CHOWDHURY - reaziii         **/
/**  Department of Computer Science and Engineering  **/
/*         INSTITUTE OF SCIENCE AND TECHNOLOGY       **/
/**                                                  **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x)     printf("Case %d: ",x++)
#define pn           puts("")
#define fri(f)       for(int i=0;i<f;i++)
#define frj(f)       for(int j=0;j<f;j++)
#define reset(x)     memset(x,-1,sizeof(x))
#define all(x)       x.begin(),x.end()
#define input        freopen("input.txt","r",stdin);
#define output       freopen("output.txt","w",stdout)
#define infi         INT_MAX
#define linfi        LLONG_MAX
#define pii          pair<int,int>
#define pch          pair<char,char>
#define pll          pair<ll,ll>
#define mgraph       map<int,vector<int> >
#define mp           make_pair
#define pb           push_back
#define clr(x)       memset(x,0,sizeof(x))
#define frx(x,y)     for(int x=0;x<y;x++)
#define fro(i,x,y)   for(int i=x;i<y;i++)
#define ech(x,a)     for(auto &x : a)
#define ff           first
#define ss           second
#define sz(x)        x.size()
#define vi           vector<int>
#define vl           vector<ll>
#define pf           push_front
#define pi           acos(-1.0)

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class J> void print(J& x) {cout << x << " ";}
template<class K> void printn(K& x) {print(x); puts("");}
template<class H> void print(vector<H> &x) {ech(a, x) print(a); puts("");}
template<class J, class... H> void print(J h, H... t) {print(h); print(t...); puts("");}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
	cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
	debug(h);
	debug(t...);
	cerr << endl;
#endif

}

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;

bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}

ll geti() {ll x; read(x); return x;}

void read(int *aa, int sz) {fri(sz) scanf("%d", (aa + i));}
void read(ll *aa, int sz) {fri(sz) scanf("%lld", (aa + i));}
void read(ll *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%lld", (aa + i));}
void read(int *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%d", (aa + i));}
const ll mod =  1e9 + 7;
const ll N   =  1e6 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};

//......................___Start from here___.....................//
//......................_____________________....................//

int solve() {

	ll n;
	read(n);
	vl arr(n);

	read(arr);
	mgraph graph;

	fri(n) {
		graph[arr[i]].pb(i);
	}

	vi mx(n + 1, -1);

	for (int i = 1; i <= n; i++) {
		if (graph[i].size()) {
			mx[i] = graph[i][0] + 1;
		}
		for (int j = 1; j < graph[i].size(); j++) {
			mx[i] = max(mx[i], (int)(graph[i][j] - graph[i][j - 1]));
		}
		if (graph[i].size())mx[i] = max(mx[i], (int)(n - graph[i].back()));
	}

	int ln = n;
	vi res(n + 1, -1);
	for (int i = 1; i <= n; i++) {
		int x = mx[i];
		if (x == -1) continue;

		while (ln >= x) {
			res[ln] = i;
			ln--;
			if (ln < 0) break;
		}

	}

	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;

	return 0;
}
int main(int argc, char* argv[]) {

	if (argc <= 1) {
#ifndef ONLINE_JUDGE
		input;
		output;
#endif
#ifdef ONLINE_JUDGE
		ios_base::sync_with_stdio(false);
		cin.tie(0);
#endif
	}
	int cs = 1, cn = 1;
	read(cs);
	while (cs--) {
		solve();

	}
}


/******************************************************/
/******************************************************/
/**                                                  **/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         REAZ AHAMMED CHOWDHURY - reaziii         **/
/**  Department of Computer Science and Engineering  **/
/*         INSTITUTE OF SCIENCE AND TECHNOLOGY       **/
/**                                                  **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x)     printf("Case %d: ",x++)
#define pn           puts("")
#define fri(f)       for(int i=0;i<f;i++)
#define frj(f)       for(int j=0;j<f;j++)
#define reset(x)     memset(x,-1,sizeof(x))
#define all(x)       x.begin(),x.end()
#define input        freopen("input.txt","r",stdin);
#define output       freopen("output.txt","w",stdout)
#define infi         INT_MAX
#define linfi        LLONG_MAX
#define pii          pair<int,int>
#define pch          pair<char,char>
#define pll          pair<ll,ll>
#define mgraph       map<int,vector<int> >
#define mp           make_pair
#define pb           push_back
#define clr(x)       memset(x,0,sizeof(x))
#define frx(x,y)     for(int x=0;x<y;x++)
#define fro(i,x,y)   for(int i=x;i<y;i++)
#define ech(x,a)     for(auto &x : a)
#define ff           first
#define ss           second
#define sz(x)        x.size()
#define vi           vector<int>
#define vl           vector<ll>
#define pf           push_front
#define pi           acos(-1.0)

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class J> void print(J& x) {cout << x << " ";}
template<class K> void printn(K& x) {print(x); puts("");}
template<class H> void print(vector<H> &x) {ech(a, x) print(a); puts("");}
template<class J, class... H> void print(J h, H... t) {print(h); print(t...); puts("");}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
	cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
	debug(h);
	debug(t...);
	cerr << endl;
#endif

}

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;

bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}

ll geti() {ll x; read(x); return x;}

void read(int *aa, int sz) {fri(sz) scanf("%d", (aa + i));}
void read(ll *aa, int sz) {fri(sz) scanf("%lld", (aa + i));}
void read(ll *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%lld", (aa + i));}
void read(int *aa, int s, int e) {for (int i = s; i < s + e; i++)scanf("%d", (aa + i));}
const ll mod =  1e9 + 7;
const ll N   =  1e6 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};

//......................___Start from here___.....................//
//......................_____________________....................//

int solve() {

	ll n;
	read(n);
	vl arr(n);

	read(arr);
	mgraph graph;

	fri(n) {
		graph[arr[i]].pb(i);
	}

	vi mx(n + 1, -1);

	for (int i = 1; i <= n; i++) {
		if (graph[i].size()) {
			mx[i] = graph[i][0] + 1;
		}
		for (int j = 0; j < graph[i].size(); j++) {
			mx[i] = max(mx[i], (int)(graph[i][j] - graph[i][j - 1]));
		}
		if (graph[i].size())mx[i] = max(mx[i], (int)(n - graph[i].back()));
	}

	int ln = n;
	vi res(n + 1, -1);
	for (int i = 1; i <= n; i++) {
		int x = mx[i];
		if (x == -1) continue;

		while (ln >= x) {
			res[ln] = i;
			ln--;
			if (ln < 0) break;
		}

	}

	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;

	return 0;
}
int main(int argc, char* argv[]) {

	if (argc <= 1) {
#ifndef ONLINE_JUDGE
		input;
		output;
#endif
#ifdef ONLINE_JUDGE
		ios_base::sync_with_stdio(false);
		cin.tie(0);
#endif
	}
	int cs = 1, cn = 1;
	read(cs);
	while (cs--) {
		solve();

	}
}


#include<bits/stdc++.h>
using namespace std;
#define iso ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define clr(x,n) memset((x), n, sizeof(x))
#define mod 1000000007
#define MAX 1000005

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_tree;*/
ll arr[100005], ans[100005], gg[100005];

int main()
{

    iso;
    int t;
    cin>>t;
    while(t--)
    {
        map<ll, vector<ll>> pos;
        ll n, k;
        cin>>n;
        map<ll, ll> dif, rev;
        for(int i = 1; i <= n; i++)
        {
            cin>>arr[i];
            if((pos[arr[i]].size() == 0) && (i != 1))
                pos[arr[i]].pb(0);
            pos[arr[i]].pb(i);

        }

        for(auto it: pos)
        {
            if(it.second.back() != n)
            {
                it.second.pb(n+1);
            }

            ll mini = 0;
            for(int i = 0; i < it.second.size() - 1; i++)
            {
                mini = max(mini, it.second[i+1] - it.second[i]);
            }
            //cout<<mini<<" "<<it.first<<"\n";
            if(dif[mini] == 0)
                dif[mini] = it.first;
            else
                dif[mini] = min(dif[mini], it.first);

        }

        ll prev = -1;

        for(int i = 1; i <=n; i++)
        {
            if(dif[i] == 0)
            {
                if(prev != -1)
                    cout<<prev<<" ";
                else
                    cout<<"-1 ";
            }
            else
            {
                if(prev == -1)
                {
                    prev = dif[i];
                    cout<<dif[i]<<" ";
                }
                else
                {
                    prev = min(dif[i], prev);
                    cout<<prev<<" ";
                }
            }

        }

        cout<<"\n";

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define iso ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define clr(x,n) memset((x), n, sizeof(x))
#define mod 1000000007
#define MAX 1000005

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_tree;*/
ll arr[100005], ans[100005], gg[100005];

int main()
{

    iso;
    int t;
    cin>>t;
    while(t--)
    {
        map<ll, vector<ll>> pos;
        ll n, k;
        cin>>n;
        map<ll, ll> dif, rev;
        for(int i = 1; i <= n; i++)
        {
            cin>>arr[i];
            if((pos[arr[i]].size() == 0) && (i != 1))
                pos[arr[i]].pb(0);
            pos[arr[i]].pb(i);

        }

        for(auto it: pos)
        {
            if(it.second.back() != n)
            {
                it.second.pb(n+1);
            }

            ll mini = 0;
            for(int i = 0; i < it.second.size() - 1; i++)
            {
                mini = max(mini, it.second[i+1] - it.second[i]);
            }
            //cout<<mini<<" "<<it.first<<"\n";
            if(dif[mini] == 0)
                dif[mini] = it.first;
            else
                dif[mini] = min(dif[mini], it.first);

        }
        k = n/2 + 1;
        ll prev = -1;

        for(int i = 1; i < k; i++)
        {
            if(dif[i] == 0)
            {
                if(prev != -1)
                    cout<<prev<<" ";
                else
                    cout<<"-1 ";
            }
            else
            {
                if(prev == -1)
                {
                    prev = dif[i];
                    cout<<dif[i]<<" ";
                }
                else
                {
                    prev = min(dif[i], prev);
                    cout<<prev<<" ";
                }
            }

        }

        if(n%2 == 1)
        {
            ll p, g = n/2 +1;
            if(prev == -1)
                p = arr[g];
            else p = prev;
            ll mid = g, b = 0;
            for(int i = 1; i <= g; i++)
            {
                p = min({p, arr[mid-b], arr[mid+b]});
                b++;
                cout<<p<<" ";
            }
        }
        else
        {
            ll g = n/2;
            ll l = g;
            ll r = g+1, p;
            if(prev == -1)
                p = arr[g];
            else
                p = prev;
            for(int i = 1; i <= g; i++)
            {
                p = min({p,arr[l], arr[r]});
                l--;
                r++;
                cout<<p<<" ";
            }
        }

        cout<<"\n";

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define iso ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define clr(x,n) memset((x), n, sizeof(x))
#define mod 1000000007
#define MAX 1000005

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_tree;*/
ll arr[100005], ans[100005], gg[100005];

int main()
{

    iso;
    int t;
    cin>>t;
    while(t--)
    {
        map<ll, vector<ll>> pos;
        ll n, k;
        cin>>n;
        map<ll, ll> dif, rev;
        for(int i = 1; i <= n; i++)
        {
            cin>>arr[i];
            if((pos[arr[i]].size() == 0) && (i != 1))
                pos[arr[i]].pb(0);
            pos[arr[i]].pb(i);

        }

        for(auto it: pos)
        {
            if(it.second.back() != n)
            {
                it.second.pb(n+1);
            }

            ll mini = 0;
            for(int i = 0; i < it.second.size() - 1; i++)
            {
                mini = max(mini, it.second[i+1] - it.second[i]);
            }
            //cout<<mini<<" "<<it.first<<"\n";
            if(dif[mini] == 0)
                dif[mini] = it.first;
            else
                dif[mini] = min(dif[mini], it.first);

        }
        k = n/2 + 1;
        ll prev = -1;

        for(int i = 1; i < k; i++)
        {
            if(dif[i] == 0)
            {
                if(prev != -1)
                    cout<<prev<<" ";
                else
                    cout<<"-1 ";
            }
            else
            {
                if(prev == -1)
                {
                    prev = dif[i];
                    cout<<dif[i]<<" ";
                }
                else
                {
                    prev = min(dif[i], prev);
                    cout<<prev<<" ";
                }
            }

        }

        int g = n/2 +1;

        if(prev == -1)
            prev = arr[g];
        for(int i = g; i <= n; i++)
        {
            prev = min({prev, arr[i], arr[n+1-i]});
            cout<<prev<<" ";
        }

        cout<<"\n";

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define iso ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define clr(x,n) memset((x), n, sizeof(x))
#define mod 1000000007
#define MAX 1000005

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_tree;*/
ll arr[300005];

int main()
{

    iso;
    int t;
    cin>>t;
    while(t--)
    {
        map<ll, vector<ll>> pos;
        ll n, k;
        cin>>n;
        map<ll, ll> dif;
        for(int i = 1; i <= n; i++)
        {
            cin>>arr[i];
            if((pos[arr[i]].size() == 0) && (i != 1))
                pos[arr[i]].pb(0);
            pos[arr[i]].pb(i);

        }

        for(auto it: pos)
        {
            if(it.second.back() != n)
            {
                it.second.pb(n+1);
            }

            ll mini = 0;
            for(int i = 0; i < it.second.size() - 1; i++)
            {
                mini = max(mini, it.second[i+1] - it.second[i]);
            }

            if(dif[mini] == 0)
                dif[mini] = it.first;
            else
                dif[mini] = min(dif[mini], it.first);

        }

        ll prev = -1;

        for(int i = 1; i <=n; i++)
        {
            if(dif[i] == 0)
            {
                if(prev != -1)
                    cout<<prev<<" ";
                else
                    cout<<"-1 ";
            }
            else
            {
                if(prev == -1)
                {
                    prev = dif[i];
                    cout<<dif[i]<<" ";
                }
                else
                {
                    prev = min(dif[i], prev);
                    cout<<prev<<" ";
                }
            }

        }

        cout<<"\n";

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{int t;
      cin>>t;
      while(t--)
      {     int n,k,m,maxx=1e9;
            cin>>n;
            int a[n+2],f[n+2]={0},p[n+2]={0},prev[n+2],len[n+2],b[n+2];
            vector<pair<int,int>>v;
            for(int i=1;i<=n;i++)
            {
                  cin>>a[i];
                  f[a[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                  p[a[i]]++;
                  if(p[a[i]]==1)
                  {len[a[i]]=i;
                  prev[a[i]]=i;}
                  else if(p[a[i]]>1&&p[a[i]]<f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                  }
                  else if(p[a[i]]==f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
            }
            sort(a+1,a+n+1);
            v.push_back({1,-1});
            for(int i=1;i<=n;i++)
            {
                  if(len[a[i]]<maxx)
                  {
                        maxx=len[a[i]];
                        v.push_back({len[a[i]],a[i]});
                  }
            }

            sort(v.begin(),v.end());

            for(int i=1;i<v.size();i++)
            {
                 int p= v[i-1].second;
                 int q= v[i-1].first;
                 int r=v[i].first;

                 for(int j=q;j<r;j++)
                 b[j]=p;
                  if(i==v.size()-1)
                 {
                 for(int j=r;j<=n;j++)
                 b[j]=v[v.size()-1].second;
                 }

            }
            for(int i=1;i<=n;i++)
            cout<<b[i]<<" ";
            cout<<endl;
      }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int main()
{int32_t t;
      cin>>t;
      while(t--)
      {     int n,k,m,maxx=1e9;
            cin>>n;
            int a[n+2],f[n+2]={0},p[n+2]={0},prev[n+2],len[n+2],b[n+2];
            vector<pair<int,int>>v;
            for(int i=1;i<=n;i++)
            {
                  cin>>a[i];
                  f[a[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                  p[a[i]]++;
                  if(p[a[i]]==1)
                  {len[a[i]]=i;
                  prev[a[i]]=i;}
                  else if(p[a[i]]>1&&p[a[i]]<f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                  }
                  else if(p[a[i]]==f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
            }
            sort(a+1,a+n+1);
            v.push_back({1,-1});
            for(int i=1;i<=n;i++)
            {
                  if(len[a[i]]<maxx)
                  {
                        maxx=len[a[i]];
                        v.push_back({len[a[i]],a[i]});
                  }
            }

            sort(v.begin(),v.end());

            for(int i=1;i<v.size();i++)
            {
                 int p= v[i-1].second;
                 int q= v[i-1].first;
                 int r=v[i].first;

                 for(int j=q;j<r;j++)
                 b[j]=p;
                  if(i==v.size()-1)
                 {
                 for(int j=r;j<=n;j++)
                 b[j]=v[v.size()-1].second;
                 }

            }
            for(int i=1;i<=n;i++)
            cout<<b[i]<<" ";
            cout<<endl;
      }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int main()
{int t;
      cin>>t;
      while(t--)
      {     int n,k,m,maxx=1e9;
            cin>>n;
            int a[n+2],f[n+2]={0},p[n+2]={0},prev[n+2],len[n+2],b[n+2];
            vector<pair<int,int>>v;
            for(int i=1;i<=n;i++)
            {
                  cin>>a[i];
                  f[a[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                  p[a[i]]++;
                  if(p[a[i]]==1)
                  {len[a[i]]=i;
                  prev[a[i]]=i;}
                  else if(p[a[i]]>1&&p[a[i]]<f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                  }
                  else if(p[a[i]]==f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
            }
            sort(a+1,a+n+1);
            v.push_back({1,-1});
            for(int i=1;i<=n;i++)
            {
                  if(len[a[i]]<maxx)
                  {
                        maxx=len[a[i]];
                        v.push_back({len[a[i]],a[i]});
                  }
            }

            sort(v.begin(),v.end());

            for(int i=1;i<v.size();i++)
            {
                 int p= v[i-1].second;
                 int q= v[i-1].first;
                 int r=v[i].first;

                 for(int j=q;j<r;j++)
                 b[j]=p;
                  if(i==v.size-1)
                 {
                 for(int j=r;j<=n;j++)
                 b[j]=v[v.size()-1].second;
                 }

            }
            for(int i=1;i<=n;i++)
            cout<<b[j]<<" ";
            cout<<endl;
      }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{int t;
      cin>>t;
      while(t--)
      {     int n,k,m,maxx=1e9;
            cin>>n;
            int a[n+2],f[n+2]={0},p[n+2]={0},prev[n+2],len[n+2],b[n+2];
            vector<pair<int,int>>v;
            for(int i=1;i<=n;i++)
            {
                  cin>>a[i];
                  f[a[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                  p[a[i]]++;
                  if(p[a[i]]==1)
                  {len[a[i]]=i;
                  prev[a[i]]=i;}
                  else if(p[a[i]]>1&&p[a[i]]<f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                  }
                  else if(p[a[i]]==f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
                  if(f[a[i]]==1)
                  {
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
            }
            sort(a+1,a+n+1);
            v.push_back({1,-1});
            for(int i=1;i<=n;i++)
            {
                  if(len[a[i]]<maxx)
                  {
                        maxx=len[a[i]];
                        v.push_back({len[a[i]],a[i]});
                  }
            }

            sort(v.begin(),v.end());

            for(int i=1;i<v.size();i++)
            {
                 int p= v[i-1].second;
                 int q= v[i-1].first;
                 int r=v[i].first;

                 for(int j=q;j<r;j++)
                 b[j]=p;
                  if(i==v.size()-1)
                 {
                 for(int j=r;j<=n;j++)
                 b[j]=v[v.size()-1].second;
                 }

            }
            for(int i=1;i<=n;i++)
            cout<<b[i]<<" ";
            cout<<endl;
      }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int main()
{int t;
      cin>>t;
      while(t--)
      {     int n,k,m,maxx=1e9;
            cin>>n;
            int a[n+2],f[n+2]={0},p[n+2]={0},prev[n+2],len[n+2],b[n+2];
            vector<pair<int,int>>v;
            for(int i=1;i<=n;i++)
            {
                  cin>>a[i];
                  f[a[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                  p[a[i]]++;
                  if(p[a[i]]==1)
                  {len[a[i]]=i;
                  prev[a[i]]=i;}
                  else if(p[a[i]]>1&&p[a[i]]<f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                  }
                  else if(p[a[i]]==f[a[i]])
                  {
                        k=i-prev[a[i]];
                        if(k>len[a[i]])
                        len[a[i]]=k;
                        prev[a[i]]=i;
                        m=n+1-i;
                        if(m>len[a[i]])
                        len[a[i]]=m;
                  }
            }
            sort(a+1,a+n+1);
            v.push_back({1,-1});
            for(int i=1;i<=n;i++)
            {
                  if(len[a[i]]<maxx)
                  {
                        maxx=len[a[i]];
                        v.push_back({len[a[i]],a[i]});
                  }
            }

            sort(v.begin(),v.end());

            for(int i=1;i<v.size();i++)
            {
                 int p= v[i-1].second;
                 int q= v[i-1].first;
                 int r=v[i].first;

                 for(int j=q;j<r;j++)
                 b[j]=p;
                  if(i==v.size()-1)
                 {
                 for(int j=r;j<=n;j++)
                 b[j]=v[v.size()-1].second;
                 }

            }
            for(int i=1;i<=n;i++)
            cout<<b[i]<<" ";
            cout<<endl;
      }
}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,long long int> m;
		map<long long int,long long int> l;
		long long int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];
			long long int temp = 0;
			if(m[a[i]] == 0) l[a[i]] =-1;

			m[a[i]] = max(i-l[a[i]],n-i);

			l[a[i]] = i;

		}

		for(int i = 0;i<n;i++)
		{
			if(ans[m[a[i]]]>a[i]) ans[m[a[i]]] = a[i];
		}
		long long int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f == -1)
			{
				if(ans[i] != INT_MAX)
					f = ans[i];
				else
					ans[i] = -1;
			}
			else
			{
				if(ans[i] > f) ans[i] = f;
				else f = ans[i];
			}
			cout<<ans[i]<<" ";
		}
		cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,vector<long long int>> m;
		map<long long int,long long int> l;
		long long int ans[n+5];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];

			if(m[a[i]].size() == 0) m[a[i]].push_back(0);
			m[a[i]].push_back(i);

		}
		//cout<<"\n";

		int w[n+1]={0};
		for(int i = 0;i<n;i++)
		{
			if(w[a[i]] == 0)
			{
				int diff = INT_MIN;
				m[a[i]].push_back(n-1);
				for(int j=1;j<m[a[i]].size();j++)
				{
					int c = 0;
					if(j == 1 || j == m[a[i]].size()-1) c = c + 1;
					else c = 0;
					c = c + m[a[i]][j] - m[a[i]][j-1];
					if(c > diff) diff = c;
				}

				if(ans[diff] > a[i])
				ans[diff] = a[i];
			w[a[i]] = 1;
			//cout<<a[i]<<" "<<diff<<"\n";
			}

		}
		//cout<<"\n";
		long long int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f == -1)
			{
				if(ans[i] != INT_MAX)
					f = ans[i];
				else
					ans[i] = -1;
			}
			else
			{
				if(ans[i] > f) ans[i] = f;
				else f = ans[i];
			}
			cout<<ans[i]<<" ";
		}
		cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,long long int> m;
		map<long long int,long long int> l;
		int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];
			if(m[a[i]] == 0) l[a[i]] = -1;
			m[a[i]] = max(i-l[a[i]],n-i);
			l[a[i]] = i;
			if(a[i]<ans[m[a[i]]]) ans[m[a[i]]] = a[i];
		}
		int f = -1;
		for(int i = 0;i<n;i++)
		{
			if(f==-1)
				{
					if(ans[i] == INT_MAX) ans[i] = -1;
					else f = ans[i];
				}

			else
			{
				if(ans[i]>f) ans[i] = f;
				else f = ans[i];
			}

		}

		for(int i = 1;i<=n;i++) cout<<ans[i]<<" ";
			cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,long long int> m;
		map<long long int,long long int> l;
		int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];
			if(m[a[i]] == 0) l[a[i]] = -1;
			m[a[i]] = max(i-l[a[i]],n-i);
			l[a[i]] = i;
			if(a[i]<ans[m[a[i]]]) ans[m[a[i]]] = a[i];
		}
		int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f==-1)
				{
					if(ans[i] == INT_MAX) ans[i] = -1;
					else f = ans[i];
				}

			else
			{
				if(ans[i]>f) ans[i] = f;
				else f = ans[i];
			}

		}

		for(int i = 1;i<=n;i++) cout<<ans[i]<<" ";
			cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,long long int> m;
		map<long long int,long long int> l;
		int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];
			int temp = 0;
			if(m[a[i]] == 0) l[a[i]] =-1;
			else
			{
				temp = m[a[i]];
			}
			m[a[i]] = max(i-l[a[i]],n-i);
			if(m[a[i]] > temp)
			{
				ans[temp] = INT_MAX;
			}
			l[a[i]] = i;
			if(a[i]<ans[m[a[i]]]) ans[m[a[i]]] = a[i];
		}
		int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f==-1)
				{
					if(ans[i] == INT_MAX) ans[i] = -1;
					else f = ans[i];
				}

			else
			{
				if(ans[i]>f) ans[i] = f;
				else f = ans[i];
			}

		}

		for(int i = 1;i<=n;i++) cout<<ans[i]<<" ";
			cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,vector<long long int>> m;
		map<long long int,long long int> l;
		long long int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];

			if(m[a[i]].size() == 0) m[a[i]].push_back(0);
			m[a[i]].push_back(i);

		}
		//cout<<"\n";

		int w[n+1]={0};
		for(int i = 0;i<n;i++)
		{
			if(w[a[i]] == 0)
			{
				int diff = INT_MIN;
				m[a[i]].push_back(n-1);
				for(int j=1;j<m[a[i]].size();j++)
				{
					int c = m[a[i]][j] - m[a[i]][j-1];
					if(c > diff) diff = c;
				}
				if(m[a[i]].size() == 3) diff += 1;
				if(ans[diff] > a[i])
				ans[diff] = a[i];
			//cout<<a[i]<<" "<<diff<<"\n";
			}

		}
		//cout<<"\n";
		long long int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f == -1)
			{
				if(ans[i] != INT_MAX)
					f = ans[i];
				else
					ans[i] = -1;
			}
			else
			{
				if(ans[i] > f) ans[i] = f;
				else f = ans[i];
			}
			cout<<ans[i]<<" ";
		}
		cout<<"\n";
	}

}


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

int main() {
	#ifndef ONLINE_JUDGE

	//for getting input from input.txt
	freopen("input.txt", "r", stdin);

	//for writting output to output.txt
	freopen("output.txt", "w", stdout);
	#endif

	int t;
	cin>>t;
	while(t--)
	{
		long long int n;
		cin>>n;
		long long int a[n];
		map<long long int,long long int> m;
		map<long long int,long long int> l;
		long long int ans[n+1];
		for(int i = 0;i<=n;i++) ans[i] = INT_MAX;
		for(int i = 0;i<n;i++)
		{
			cin>>a[i];
			long long int temp = 0;
			if(m[a[i]] == 0) l[a[i]] =0;

			m[a[i]] = max(i-l[a[i]],n-1-i)+1;

			l[a[i]] = i;

		}

		for(int i = 0;i<n;i++)
		{
			if(ans[m[a[i]]]>a[i]) ans[m[a[i]]] = a[i];

			//cout<<m[a[i]]<<" ";
		}
		//cout<<"\n";
		long long int f = -1;
		for(int i = 1;i<=n;i++)
		{
			if(f == -1)
			{
				if(ans[i] != INT_MAX)
					f = ans[i];
				else
					ans[i] = -1;
			}
			else
			{
				if(ans[i] > f) ans[i] = f;
				else f = ans[i];
			}
			cout<<ans[i]<<" ";
		}
		cout<<"\n";
	}

}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tc(t) int t; cin >> t; while (t--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define ps(x,y)   fixed<<setprecision(y)<<x
using namespace std;
const int N = (int)3e5 + 10;
void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	vector<vector<int>>ind(n + 2);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		ind[a[i]].push_back(i);
	}
	vector<int>ans(n + 2, 2 * n);
	for (int i = 0; i <= n; i++) {
		int st = -1;
		int mx = 0;
		for (int j : ind[i]) {
			mx = max(mx, j - st);
			st = j;
		}
		mx = max(mx, n - st);
		ans[mx] = min(ans[mx], i);
		// cout << mx << " -> " << ans[mx] << endl;
	}

	int mn = 2 * n;
	for (int i = 1; i <= n; i++) {
		mn = min(mn, ans[i]);
		if (mn > n) {
			cout << "-1 ";
		}
		else {
			cout << mn << " ";
		}
	}
	cout << endl;

}
int main()
{
	IOS
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	tc(t)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tc(t) int t; cin >> t; while (t--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define ps(x,y)   fixed<<setprecision(y)<<x
using namespace std;
const int N = (int)3e5 + 10;
void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	vector<vector<int>>ind(n + 2);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		ind[a[i]].push_back(i);
	}
	vector<int>ans(n + 2, 2 * n);
	for (int i = 0; i < n; i++) {
		int st = -1;
		int mx = 0;
		for (int j : ind[a[i]]) {
			mx = max(mx, j - st);
			st = j;
		}
		mx = max(mx, n - st);
		ans[mx] = min(ans[mx], i);
		// cout << mx << " -> " << ans[mx] << endl;
	}

	int mn = 2 * n;
	for (int i = 1; i <= n; i++) {
		mn = min(mn, ans[i]);
		if (mn > n) {
			cout << "-1 ";
		}
		else {
			cout << mn + 1 << " ";
		}
	}
	cout << endl;

}
int main()
{
	IOS
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	tc(t)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tc(t) int t; cin >> t; while (t--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define ps(x,y)   fixed<<setprecision(y)<<x
using namespace std;
const int N = (int)3e5 + 10;
void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	vector<vector<int>>ind(n + 2);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		ind[a[i]].push_back(i);
	}
	vector<int>ans(n + 2, 2 * n);
	for (int i = 1; i <= n; i++) {
		int st = -1;
		int mx = 0;
		for (int j : ind[i]) {
			mx = max(mx, j - st);
			st = j;
		}
		mx = max(mx, n - st);
		ans[mx] = min(ans[mx], i);
		// cout << mx << " -> " << ans[mx] << endl;
	}

	int mn = 2 * n;
	for (int i = 1; i <= n; i++) {
		mn = min(mn, ans[i]);
		if (mn > n) {
			cout << "-1 ";
		}
		else {
			cout << mn << " ";
		}
	}
	cout << endl;

}
int main()
{
	IOS
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	tc(t)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tc(t) int t; cin >> t; while (t--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define ps(x,y)   fixed<<setprecision(y)<<x
using namespace std;
const int N = (int)3e5 + 10;
void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	vector<vector<int>>ind(n + 2);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		ind[a[i]].push_back(i);
	}
	vector<int>ans(n + 2, 2 * n);
	for (int i = 0; i < n; i++) {
		int st = -1;
		int mx = 0;
		for (int j : ind[i]) {
			mx = max(mx, j - st);
			st = j;
		}
		mx = max(mx, n - st);
		ans[mx] = min(ans[mx], i);
	}

	int mn = 2 * n;
	for (int i = 1; i <= n; i++) {
		mn = min(mn, ans[i]);
		if (mn > n) {
			cout << "-1 ";
		}
		else {
			cout << mn << " ";
		}
	}
	cout << endl;

}
int main()
{
	IOS
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	tc(t)
	{
		solve();
	}
}


#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <climits>
#include <iomanip>
#define endl "\n"
#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x  << ", " << #y << " : " << y << endl;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(0);
typedef long long ll;
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	FASTIO

	int t;
	cin >> t;

	while(t --)
	{
		int n;
		cin >> n;

		vector <int> v(n);
		map <int, int> last;
		map <int, int> dist;
		for(int i = 0; i < n; i ++)
		{
			cin >> v[i];
			if(last.find(v[i]) == last.end())
			{
				last[v[i]] = i;
				dist[v[i]] = i + 1;
			}
			else
			{
				int newDist = max({i - last[v[i]], dist[v[i]]});
				last[v[i]] = i;
				dist[v[i]] = newDist;
			}
		}

		map <int, int> final;
		for(auto it = dist.begin(); it != dist.end(); it ++)
		{
			dist[it->first] = max(dist[it->first], n - last[it->first]);
			// debug2(it->first, it->second)
			if(final.find(it->second) != final.end())
				final[it->second] = min(final[it->second], it->first);
			else
				final[it->second] = it->first;
		}

		vector <int> ans(n + 1, 0);
		for(auto it = final.begin(); it != final.end(); it ++)
		{
			// debug2(it->first, it->second)
			if(ans[it->first] == 0)
				ans[it->first] = it->second;
		}

		for(int i = 1; i <= n; i ++)
		{
			if(i == 1 && ans[i] == 0)
				ans[i] = -1;
			else if(ans[i] == 0)
				ans[i] = ans[i - 1];
			else if(i != 1 && ans[i] > ans[i - 1] && ans[i - 1] != -1)
				ans[i] = ans[i - 1];
			cout << ans[i] << " ";
		}
		cout << endl;
	}
}


#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <climits>
#include <iomanip>
#define endl "\n"
#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x  << ", " << #y << " : " << y << endl;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(0);
typedef long long ll;
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	FASTIO

	int t;
	cin >> t;

	while(t --)
	{
		int n;
		cin >> n;

		vector <int> v(n);
		map <int, int> last;
		map <int, int> dist;
		for(int i = 0; i < n; i ++)
		{
			cin >> v[i];
			if(last.find(v[i]) == last.end())
			{
				last[v[i]] = i;
				dist[v[i]] = i;
			}
			else
			{
				int newDist = max({i - last[v[i]], dist[v[i]]});
				last[v[i]] = i;
				dist[v[i]] = newDist;
			}
		}

		map <int, int> final;
		for(auto it = dist.begin(); it != dist.end(); it ++)
		{
			dist[it->first] = max(dist[it->first], n - last[it->first]);
			// debug2(it->first, it->second)
			if(final.find(it->second) != final.end())
				final[it->second] = min(final[it->second], it->first);
			else
				final[it->second] = it->first;
		}

		vector <int> ans(n + 1, 0);
		for(auto it = final.begin(); it != final.end(); it ++)
		{
			// debug2(it->first, it->second)
			if(ans[it->first + 1] == 0)
				ans[it->first] = it->second;
		}

		for(int i = 1; i <= n; i ++)
		{
			if(i == 1 && ans[i] == 0)
				ans[i] = -1;
			else if(ans[i] == 0)
				ans[i] = ans[i - 1];
			cout << ans[i] << " ";
		}
		cout << endl;
	}
}


#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <climits>
#include <iomanip>
#define endl "\n"
#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x  << ", " << #y << " : " << y << endl;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(0);
typedef long long ll;
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	FASTIO

	int t;
	cin >> t;

	while(t --)
	{
		int n;
		cin >> n;

		vector <int> v(n);
		map <int, int> last;
		map <int, int> dist;
		for(int i = 0; i < n; i ++)
		{
			cin >> v[i];
			if(last.find(v[i]) == last.end())
			{
				last[v[i]] = i;
				dist[v[i]] = i;
			}
			else
			{
				int newDist = max({i - last[v[i]], dist[v[i]]});
				last[v[i]] = i;
				dist[v[i]] = newDist;
			}
		}

		map <int, int> final;
		for(auto it = dist.begin(); it != dist.end(); it ++)
		{
			dist[it->first] = max(dist[it->first], n - last[it->first]);
			// debug2(it->first, it->second)
			if(final.find(it->second) != final.end())
				final[it->second] = min(final[it->second], it->first);
			else
				final[it->second] = it->first;
		}

		vector <int> ans(n + 1, 0);
		for(auto it = final.begin(); it != final.end(); it ++)
		{
			// debug2(it->first, it->second)
			if(ans[it->first] == 0)
				ans[it->first] = it->second;
		}

		for(int i = 1; i <= n; i ++)
		{
			if(i == 1 && ans[i] == 0)
				ans[i] = -1;
			else if(ans[i] == 0)
				ans[i] = ans[i - 1];
			cout << ans[i] << " ";
		}
		cout << endl;
	}
}


#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){

    int entrada;
    while(scanf("%d", &entrada) != EOF){
        int soma = 0;
        int leitura;
        for (int i = 0; i < entrada; i++){
            scanf("%d", &leitura);
            soma += leitura;
        }
        int tempos;
        scanf("%d",&tempos);
        int tempoa, tempob;
        int possivel = 0;
        for (int i = 0; i < tempos; i++){
            scanf("%d %d", &tempoa, &tempob);
            if (soma <= tempob){
                if (tempoa>= soma)
                printf("%d", tempoa);
                else printf("%d\n", soma);
                possivel = 1;
                break;
            }
        }
        if (possivel == 0)
            printf("-1\n");

    }

    return 0;
}
// 1498250916587


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;

           if(sum>=l && sum<=r)
           {
               ans=1;
               break;
           }

           else if(sum<=l)
           {
               sum=l;
               ans=1;
               break;
           }
       }
       if(ans==1)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;

           if(sum>=l && sum<=r)
           {
               ans=1;
               sum=l;
               break;
           }

           else if(sum<=l)
           {
               sum=l;
               ans=1;
               break;
           }
       }
       if(ans==1 || m==0)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;
           if(l<r)
            swap(l,r);

           if(sum>=l && sum<=r)
           {
               ans=1;
           }
           else if(sum<=l)
           {
               sum=l;
               ans=1;
           }
       }
       if(ans==1 || m==0)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],b[1005][2],m;
   while(cin>>n)
   {
       long long sum=0,i;
       for( i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;

       for(i=1;i<=m;i++)
       {
           cin>>b[i][0]>>b[i][1];
       }

       for(i=1;i<=m;i++)
       {
           long long l,r;
           l=b[i][0];
           r=b[i][1];

           if(sum>=l && sum<=r)
           {
               ans=1;
               break;
           }

           else if(sum<=l)
           {
               sum=l;
               ans=1;
               break;
           }
       }

       if(ans==1)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;
           if(l>r)
            swap(l,r);

           if(sum>=l && sum<=r)
           {
               ans=1;
               break;
           }
           else if(sum<=l)
           {
               sum=l;
               ans=1;
               break;
           }
       }
       if(ans==1 || m==0)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;
           if(l<r)
            swap(l,r);

           if(sum>=l && sum<=r)
           {
               ans=1;
               break;
           }
           else if(sum<=l)
           {
               sum=l;
               ans=1;
               break;
           }
       }
       if(ans==1 || m==0)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


/* I AM........ */
/* I SHALL COPY PASTE ANY PART OF MY CODE BUT SHALL REVISE && REVISE */
   /* I SHALL WRITE IT DOWN BEFORE WRITTING IT */
      /* I SHALL UNDERSTAND AND THINK THOSE INPUTS */
         /* WELL, INPUT IS << 0 ? */
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define nl printf("\n")
#define case(a,b) printf("Case %I64d: %I64d\n",a,b)
#define P(a) printf("%I64d\n",a)
#define SP(a) printf("%I64d ",a)
#define G(a) scanf("%I64d",&a)
#define GG(a,b) scanf("%I64d %I64d",&a,&b)
#define pb push_back
#define DB printf("I WAS HERE\n")
main()
{
   //freopen("a.out", "w", stdout);
   long long n,a[1005],t[1005],m;
   while(cin>>n)
   {
       long long sum=0;
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           sum+=a[i];
       }
       cin>>m;
       long long ans=-1;
       for(int i=1;i<=m;i++)
       {
           long long l,r;
           cin>>l>>r;
           if(sum>=l && sum<=r)
           {
               ans=1;
           }
       }
       if(ans==1 || m==0)
       {
           P(sum);

       }
       else
        P(ans);
   }
}


#include<cstdio>
int main() {
	int n, m;
	int sum = 0;
	int aux;
	int kek = 0;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf(" %d", &aux);
		sum += aux;
	}
	scanf("%d", &m);
	for(int i = 0; i < 2*m; i++) {
		scanf(" %d", &aux);
		if(aux >= sum) {
			kek = 1;
		}
	}
	if(kek)
		printf("%d\n", sum);
	else
		printf("-1\n");
}
// 1498763378906


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    int n, sum=0, interval, sol;
    bool moveon=true;
    cin >> n;
    for(int i=0; i<n; i++){
        int t;
        cin >> t;
        sum+=t;
    }
    cin >> interval;
    for(int i=0; i<interval; i++){
        int begin, end;
        cin >> begin >> end;
        if(sum <= end && moveon == true){
            moveon=false;
            if(sum > begin){
                sol=sum;
            }else{
                sol=begin;
            }
        }
    }
    if(moveon){
        cout << -1 << endl;
    }else{
        cout << sol << endl;
    }
    return 0;
}
// 1498244582052


#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){

    int entrada;
    while(scanf("%d", &entrada) != EOF){
        int soma = 0;
        int leitura;
        for (int i = 0; i < entrada; i++){
            scanf("%d", &leitura);
            soma += leitura;
        }
        int tempos;
        scanf("%d",&tempos);
        int tempoa, tempob;
        int possivel = 0;
        for (int i = 0; i < tempos; i++){
            scanf("%d %d", &tempoa, &tempob);
            if (soma <= tempob){
                if (tempoa> soma)
                printf("%d", tempoa);
                else printf("%d\n", soma);
                possivel = 1;
                break;
            }
        }
        if (possivel == 0)
            printf("-1\n");

    }

    return 0;
}
// 1498250778947


#include<cstdio>
#include<utility>
using namespace std;
typedef pair<int,int> ppi;

int main() {
	int n, m;
	ppi period[1000];
	int aux;
	unsigned long long sum=0;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf(" %d", &aux);
		sum += aux;
	}
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf(" %d %d", &period[i].first, &period[i].second);
	}
	if(sum <= period[m-1].second) {
		for(int i = 0; i < m; i++) {
			if(sum <= period[i].second)
				printf("%d\n", sum);
		}
	}
	else
		printf("-1\n");
}
// 1498245275507


#include<cstdio>
#include<utility>
#include<stdlib.h>
using namespace std;
typedef pair<int,int> ppi;
int compar(const void *a, const void *b) {
	return *((int*)a) - *((int*)b);
}
int main() {
	int n, m;
	int time[1000];
	ppi period[1000];
	int p = 0;
	unsigned long long sum=0;
	int som;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf(" %d", &time[i]);
		sum += time[i];
	}
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf(" %d %d", &period[i].first, &period[i].second);
	}
	qsort(time, n, sizeof(int), compar);
	for(int i = 0; i < m && p < n; i++) {
		som = 0;
		while(som <= period[i].second) {
			som += time[p++];
			if(p == n) {
				if(sum <= period[i].second && sum >= period[i].first) {
					printf("%d\n", sum);
					return 0;
				}
				else if(i+1 < m) {
					printf("%d\n", period[i+1].first);
					return 0;
				}
			}
		}
	}
	if(sum > period[m-1].second)
		printf("-1\n");
}
// 1498250553018


#include<cstdio>
int main() {
	int n, m;
	int sum = 0;
	int aux1, aux2;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf(" %d", &aux1);
		sum += aux1;
	}
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf(" %d %d", &aux1, &aux2);
		if(aux1 >= sum) {
			printf("%d\n", aux1);
			return 0;
		}
		else if(aux2 >= sum) {
			printf("%d\n", sum);
			return 0;
		}
	}
	printf("-1\n");
}
// 1498765810795


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

vector < pair<long long int, long long int> > sub;
int main(){

	int i,j;
	int n, m, aux, b,f;
	long long int soma;

	cin >> n;
	soma =0;
	for(i=0;i<n;i++){
		cin >> aux;
		soma += aux;
	}

	cin >> m;
	for(i=0;i<m;i++){
		cin >> b >> f;
		sub.push_back(make_pair(b,f));

	}

	long long int tempo = -1;
	bool found = false;
	for(i=0;i<m && !found;i++){
		if(soma>=sub[i].first && soma <= sub[i].second){
			tempo = soma;
			found = true;
		}
	}

	cout << tempo << endl;

	return 0;
}
// 1498240780254


#include <stdio.h>
#include <vector>
#include <algorithm>
#define pb push_back
using namespace std;

const int INF = 0x3f3f3f3f;

vector <pair <int, int> > y;

int main(void) {
    int n;
    int m;
    int a;
    int timer = 0;
    int l, r;
    int res = INF;

    scanf(" %d", &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &a);
        timer += a;
    }

    scanf(" %d", &m);
    for (int i = 0; i < m; i++) {
        scanf(" %d %d", &l, &r);
        y.pb(make_pair(l, r));
    }

    for (int i = 0; i < m; i++) {
        if (y[i].second >= timer) {
            res = min(res, max(timer, y[i].first));
        }
    }

    if (res < INF) {
        printf("%d\n", res);
    } else {
        printf("-1\n");
    }
    return 0;
}

// 1498239951044


#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){

    int entrada;
    scanf("%d", &entrada);
        int soma = 0;
        int leitura;
        for (int i = 0; i < entrada; i++){
            scanf("%d", &leitura);
            soma += leitura;
        }
        int tempos;
        scanf("%d",&tempos);
        int tempoa, tempob;
        int possivel = 0;
        for (int i = 0; i < tempos; i++){
            scanf("%d %d", &tempoa, &tempob);
            if (soma <= tempob){
                if (tempoa>= soma)
                    printf("%d\n", tempoa);
                else printf("%d\n", soma);
                possivel = 1;
                break;
            }
        }
        if (possivel == 0)
            printf("-1\n");

    return 0;
}
// 1498251999214


#include<cstdio>
#include<utility>
#include<stdlib.h>
using namespace std;
typedef pair<int,int> ppi;
//>tfw to inteligent
int main() {
	int n, m;
	int time[1000];
	ppi period[1000];
	int p = 0;
	unsigned long long sum=0;
	int som;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf(" %d", &time[i]);
		sum += time[i];
	}
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf(" %d %d", &period[i].first, &period[i].second);
	}
	for(int i = 0; i < m; i++) {
		som = 0;
		while(som <= period[i].second) {
			som += time[p++];
			if(p == n) {
				if(som <= period[i].second && som >= period[i].first)
					printf("%d\n", som);
				else if(i+1 < m)
					printf("%d\n", period[i+1].first);
			}
		}
	}
	if(sum > period[m-1].second)
		printf("-1\n");
}
// 1498249823371


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

int main(){
	int  n,m,x,start, end,sum;
	std::vector< pair <int,int> > works;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> x;
		sum+=x;	// in sum units of time (minimum) he would finish all problems and submit
	}
	cin >> m;
	for (int i = 0; i < m; ++i)
	{
		cin >> start >> end;
		works.push_back(make_pair(start, end));
	}
	if (end < sum)	// end is max hours to deliver
	{
		cout << -1 << endl;
	}else{
		for (int i = 0; i < m; ++i)
		{
			if (sum <= works[i].first)
			{
				cout << works[i].first << endl;
				break;
			}else if (works[i].first < sum && sum <= works[i].second)
			{
				cout << sum << endl;
				break;
			}
		}
	}
return 0;
}
// 1498247311911


#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int tmax=1010;
int a[tmax];
int n,m;
long long sum=0;
pair<int ,int>M[tmax];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x;
		int y;
		scanf("%d%d",&x,&y);
		M[i]=make_pair(x,y);
	}
	if(m==0)
	{
		printf("-1\n");
		return 0;
	}
	else
	{
		if(sum<=M[1].first)
		{
			cout<<M[1].first<<endl;
		}
		else
		{
			for(int i=1;i<=m;i++)
			{
				if(sum>=M[i].first&&sum<=M[i].second)
				{
					cout<<sum<<endl;
					return 0;
				}
				if(i+1<=m&&sum>=M[i].second&&sum<=M[i+1].first)
				{
					cout<<M[i+1].first<<endl;
					return 0;
				}
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int tmax=1010;
int a[tmax];
int n,m;
long long sum;
pair<int ,int>M[1000];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x;
		int y;
		scanf("%d%d",&x,&y);
		M[i]=make_pair(x,y);
	}
	if(m==0)
	{
		printf("-1\n");
		return 0;
	}
	else
	{
		if(sum<=M[1].first)
		{
			cout<<M[1].first<<endl;
		}
		else
		{
			for(int i=1;i<=m;i++)
			{
				if(sum>=M[i].first&&sum<=M[i].second)
				{
					cout<<sum<<endl;
					return 0;
				}
				if(i+1<=m&&sum>M[i].second&&sum<M[i+1].first)
				{
					cout<<M[i+1].first<<endl;
					return 0;
				}
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int tmax=1010;
int a[tmax];
int n,m;
long long sum=0;
pair<int ,int>M[tmax];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x;
		int y;
		scanf("%d%d",&x,&y);
		M[i]=make_pair(x,y);
	}
	if(m==0)
	{
		printf("-1\n");
		return 0;
	}
	else
	{
		if(sum<=M[1].first)
		{
			cout<<M[1].first<<endl;
			return 0;
		}
		else
		{
			for(int i=1;i<=m;i++)
			{
				if(sum>=M[i].first&&sum<=M[i].second)
				{
					cout<<sum<<endl;
					return 0;
				}
				if(i+1<=m&&sum>=M[i].second&&sum<=M[i+1].first)
				{
					cout<<M[i+1].first<<endl;
					return 0;
				}
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int MAXR = 1e5 + 5;

int ok[MAXR];

int main() {
    ios::sync_with_stdio(false);
    ll n, m, i, l, r, a;
    ll minl = INF, maxr = -1;
    ll sum = 0;
    cin >> n;
    for(i = 0; i < n; i++) {
        cin >> a;
        sum += a;
    }
    cin >> m;
    for(i = 0; i < m; i++) {
        cin >> l >> r;
        ok[l] ++;
        ok[r + 1] --;
    }
    for(i = 0; i < MAXR; i++) {
        ok[i] += ok[i - 1];
    }
    for(i = sum; i < MAXR; i++) {
        if(ok[i])
            break;
    }
    if(i != MAXR)
        cout << i << endl;
    else
        cout << -1 << endl;
    return 0;
}
/*2018-07-24 19:56:57.048*/


#include<stdio.h>
int main()
{
    long long int n,templ,tempr,i,sum=0,flag=1,ans;
    long long int a[50000]={0};
    scanf("%I64d",&n);
    for(i=0;i<n;i++)
        {
            scanf("%I64d",a+i);
            sum+=a[i];
        }
    long long int m;
    scanf("%I64d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%I64d %I64d",&templ,&tempr);
        if(sum<templ&&flag)
        {
            ans=templ;
            flag=0;
        }
        else if(templ<=sum&&tempr>=sum&&flag)
        {
            ans=sum;
            flag=0;
        }
    }
    if(flag==1)
        printf("-1\n");
    else
        printf("%I64d\n",ans);
}
/*2018-07-24 20:25:49.307*/


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
typedef long long ll;
using namespace std;
int l[1005];
int r[1005];
int main()
{
    int i,j,k,m,n;
    scanf("%d",&n);
    int sum=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&k);
        sum+=k;
    }
    scanf("%d",&m);
    if(m==0)
    {
        printf("-1");
        return 0;
    }
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
    }
    if(sum<l[0])
    {
        printf("%d\n",l[0]);
        return 0;
    }
    for(i=0;i<m;i++)
    {
        if(sum>=l[i]&&sum<=r[i])
        {
            printf("%d\n",sum);
            return 0;
        }
        if(i+1<m&&sum>r[i]&&sum<l[i+1])
        {
            printf("%d\n",l[i+1]);
            return 0;
        }
    }
    printf("-1");
    return 0;

}
/*2018-07-24 20:13:00.177*/


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int MAXR = 1e5 + 5;

int ok[MAXR];

int main() {
    ios::sync_with_stdio(false);
    ll n, m, i, l, r, a;
    ll minl = INF, maxr = -1;
    ll sum = 0;
    cin >> n;
    for(i = 0; i < n; i++) {
        cin >> a;
        sum += a;
    }
    cin >> m;
    for(i = 0; i < m; i++) {
        cin >> l >> r;
        ok[l] ++;
        ok[r + 1] --;
    }
    for(i = 0; i < MAXR; i++) {
        ok[i] += ok[i - 1];
    }
    for(i = sum; i < MAXR; i++) {
        if(ok[i])
            break;
    }
    if(i < MAXR)
        cout << i << endl;
    else
        cout << -1 << endl;
    return 0;
}
/*2018-07-24 20:01:22.516*/


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int a[1005];
int main()
{
    int n,m,x,y,b,temp=1,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a,a+n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        q.push(y-x+1);
    }
    for(int i=0; i<n; i++)
    {
        if(temp==0)
            break;
        while(1)
        {
            if(q.empty())
            {
                temp=0;
                break;
            }
            b=q.top();
            if(a[i]<b)
            {
                q.pop();
                q.push(b-a[i]);
            }
            else if(a[i]==b)
            {
                q.pop();
                break;
            }
            else
                q.pop();
        }

    }
    if(temp==0)
        printf("-1\n");
    else
        printf("%d\n",sum);
}
/*2018-07-24 19:48:09.554*/


#include<cstdio>
#include<algorithm>
using namespace std;
int time[500005];
int main()
{
    int n,a[10005],m,sum=0,x,y;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    scanf("%d",&m);
    int maxx=0;
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        for(int j=x; j<=y; j++)
                time[j]=1;
         maxx=y;
    }
    if(sum>maxx)
        puts("-1");
    else
    {
        if(time[sum])
            printf("%d\n",sum);
        else
        {
            for(int i=sum; ; i++)
            {
                if(time[i])
                {
                    printf("%d\n",i);
                    break;
                }
            }
        }
    }
    return 0;
}
/*2018-07-24 20:24:18.676*/


#include<cstdio>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
    int n,m,a[5000];
    int t1[5000],t2[5000];
    scanf("%d",&n);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&t1[i],&t2[i]);
        i++;
    }
        int j;
        int flag=1;
        for( j=0;j<m;j++)
        {
            if(sum<=t2[j])
            {
                if(sum>=t1[j])
                    printf("%d\n",sum);
                else
                printf("%d\n",t2[j]);
                return 0;
            }

        }
        if(flag==1)
            printf("-1\n");

    return 0;
}
/*2018-07-24 20:28:44.861*/


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,m;
    int i,j,k;
    int a[1009];
    int l[1009],r[1009];
    while(~scanf("%d",&n))
    {
        int sum=0;
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]),sum+=a[i];
        scanf("%d",&m);
        for(i=1;i<=m;i++)
            scanf("%d%d",&l[i],&r[i]);
        sort(l+1,l+m+1);
        int ans=-1;
        for(i=1;i<=m;i++)
        {
            if(l[i]==sum)
                ans=sum;
            else if(r[i]>=sum)
                ans=sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*2018-07-24 19:54:13.67*/


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int a[1006];
int c[1100];
int cmp(int x,int y)
{
    return x>y;
}
int main()
{

    int n;
    scanf("%d",&n);
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        ans+=a[i];
    }
    sort(a+1,a+n+1,cmp);
    int x;
    scanf("%d",&x);
    int x1=x;
    int top=0;
    while(x--)
    {
        int s,d;
        scanf("%d%d",&s,&d);
        c[top++]=d-s+1;
        //printf("%d\n",c[top-1]);
    }
    sort(c,c+top,cmp);
    int num=0;
    int j=1;
    int flag=0;
    for(int i=0; i<top; i++)
    {
      //  printf("c %d a %d\n",c[i],a[j]);
        while(c[i]>=a[j])
        {

            c[i]=(c[i]-a[j]);
           if(j==n)
           {
                flag=1;
                break;
           }
                 j++;
        }
        if(flag)
            break;
    }
    if(j==n&&flag)
        printf("%d\n",ans);
    else
        puts("-1");

    return 0;
}
/*2018-07-24 20:04:44.022*/


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int MAXR = 1e5 + 5;

int ok[MAXR];

int main() {
    ios::sync_with_stdio(false);
    ll n, m, i, l, r, a;
    ll minl = INF, maxr = -1;
    ll sum = 0;
    cin >> n;
    for(i = 0; i < n; i++) {
        cin >> a;
        sum += a;
    }
    cin >> m;
    for(i = 0; i < m; i++) {
        cin >> l >> r;
        ok[l] ++;
        ok[r + 1] --;
    }
    for(i = 0; i < MAXR; i++) {
        ok[i] += ok[i - 1];
    }
    for(i = sum; i < MAXR; i++) {
        if(ok[i])
            break;
    }
    if(i != MAXR)
        cout << i << endl;
    else
        cout << -1 << endl;
    return 0;
}
/*2018-07-24 19:58:38.729*/


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int a[1006];
int c[1100];
int cmp(int x,int y)
{
    return x>y;
}
int main()
{

    int n;
    scanf("%d",&n);
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        ans+=a[i];
    }
    sort(a+1,a+n+1,cmp);
    int x;
    scanf("%d",&x);
    int x1=x;
    int top=0;
    while(x--)
    {
        int s,d;
        scanf("%d%d",&s,&d);
        c[top++]=d-s+1;
        //printf("%d\n",c[top-1]);
    }
    sort(c,c+top,cmp);
    int num=0;
    int j=1;
    int flag=0;
    for(int i=0; i<top; i++)
    {
        //printf("c %d a %d\n",c[i],a[j]);
        while(c[i]>=a[j])
        {

            c[i]=(c[i]-a[j]);
           if(j==n)
           {
                break;
                flag=1;
           }
                 j++;
        }
        if(flag)
            break;
    }

    if(j==n&&flag)
        printf("%d\n",ans);
    else
        puts("-1");

    return 0;
}
/*2018-07-24 20:02:21.906*/


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<bitset>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int t, n, a, f, e, ans, sum, flag;
    flag = 0;
    sum = 0;
    scanf("%d", &t);
    for(int i = 1; i <= t; i++)
    {
        scanf("%d", &a);
        sum += a;
    }
//    printf("sum = %d\n", sum);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d %d", &f, &e);
        if(f <= sum && e >= sum && flag == 0)
        {
            ans = max(sum, e);
            flag = 1;
        }
    }
    if(flag == 0)
    {
        puts("-1");
        return 0;
    }
    printf("%d\n", ans);
}

/*2018-07-24 19:44:53.056*/


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[2000],v[2000],w[2000];
int main()
{
    int n,k;
    int sum=0;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    scanf("%d",&k);
    for(int i=1; i<=k; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        for(int j=a; j<=b; j++)
        {
            v[j]=1;
        }
    }
    int check=0;
  //  printf("sum %d\n",sum);
    for(int i=sum; i<=1005; i++)
    {
        if(v[i]==1)
        {
            printf("%d\n",i);
            check=1;
            break;
        }
    }
    if(check==0)
        puts("-1");

    return 0;
}
/*2018-07-24 19:29:30.139*/


#include<iostream>
#include<queue>
#include<stdio.h>
#define ll long long
using namespace std;
struct node{
	int x,y;
}b[1010];
int a[1010],c[101000];
int cmp(node a,node b)
{
	return a.x<b.x;
}
int main()
{
	int n,m,i=0,j,k=1,x,y;
	scanf("%d",&n);
	int sum=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}

	scanf("%d",&m);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&b[i].x,&b[i].y);
		if(k&&b[i].y>=sum&&b[i].x<=sum)
		{
			k=0;
		}
		else if(k&&b[i].x>=sum)
		{
			k=0;
			sum=b[i].x;
		}
	}
	if(k==0)
	printf("%d\n",sum);
	else
	printf("-1\n");

}/*2018-07-24 19:43:50.843*/


#include <cstdio>
#include <algorithm>
using namespace std;
int a[1005],ans[100005];
int main()
{
    int n,x,l,r,maxn;
    scanf("%d",&n);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",a+i);
        sum=sum+a[i];
    }
    sort(a,a+n);
    scanf("%d",&x);
    for(int i=0;i<x;i++)
    {
        scanf("%d%d",&l,&r);
        for(int j=l;j<=r;j++)
            ans[j]=1;
        maxn=r;
    }
    if(sum>maxn)
        puts("-1");
    else
    {
        int k=0;
        for(int i=0;i<n;i++)
        {
                k=k+a[i];
        }
        if(ans[k])
            printf("%d\n",k);
        else
        {
            for(int i=k;;i++)
            {
                if(ans[i])
                {
                    printf("%d\n",i);
                    break;
                }
            }
        }
    }

    return 0;
}
/*2018-07-24 19:50:31.773*/


#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=3e5+5;
int x[1005];
struct node
{
    int l,r;
} a[1005];
bool cmp(node a,node b)
{
    if(a.r==b.r)
        return a.l<b.l;
    return a.r<b.r;
}
int main()
{
    int n,m;
    ll sum=0;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&x[i]),sum+=x[i];
        scanf("%d",&m);
        for(int i=0; i<m; i++)
            scanf("%d%d",&a[i].l,&a[i].r);
        sort(a,a+m,cmp);
        int flag=1;
        for(int i=0; i<m; i++)
        {
            if(a[i].l<=sum&&a[i].r>=sum)
            {
                printf("%I64d\n",sum);
                flag=0;
                break;
            }
            else if(a[i].l>sum)
            {
                flag=0;
                printf("%d\n",a[i].l);
                break;
            }
        }
        if(flag)
            puts("-1");
    }

}

/*2018-07-24 19:24:47.119*/


#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=3e5+5;
int x[1005];
struct node
{
    int l,r;
} a[1005];
bool cmp(node a,node b)
{
    if(a.r==b.r)
        return a.l<b.l;
    return a.r<b.r;
}
int main()
{
    int n,m;
    ll sum=0;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&x[i]),sum+=x[i];
        scanf("%d",&m);
        for(int i=0; i<m; i++)
            scanf("%d%d",&a[i].l,&a[i].r);
        sort(a,a+m,cmp);
        for(int i=0; i<m; i++)
        {
            if(a[i].l<=sum&&a[i].r>=sum)
            {
                printf("%I64d\n",sum);
                break;
            }
            else if(a[i].l>sum)
            {
                printf("%d\n",a[i].l);
                break;
            }
        }
    }

}

/*2018-07-24 19:23:34.227*/


#include<cstdio>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
    int n,m,a[5000];
    int t1[5000],t2[5000];
    scanf("%d",&n);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&t1[i],&t2[i]);

    }
        int j;
        int flag=1;
        for( j=0;j<m;j++)
        {
            if(sum<=t1[j])
            {
               if(sum<=t2[j-1])
                    if(sum>t1[j-2])
                    printf("%d\n",sum);
               else
                printf("%d\n",t2[j-1]);
               else
                printf("%d\n",t1[j]);
                return 0;
            }

        }
        if(flag==1)
            printf("-1\n");

    return 0;
}
/*2018-07-24 20:40:12.327*/


#include <iostream>

using namespace std;

int n, m;

int main() {

	//freopen("in", "r", stdin);

	cin >> n;
	int s = 0;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		s += a;
	}

	cin >> m;
	int ans = -1;
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r;
		if (s <= r) {
			ans = s;
			break;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>

using namespace std;

int n, m;

int main() {

	//freopen("in", "r", stdin);

	cin >> n;
	int s = 0;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		s += a;
	}

	cin >> m;
	int ans = -1;
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r;
		if (s <= r) {
			ans = max(s, l);
			break;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>

using namespace std;

int n, m;

int main() {

	//freopen("in", "r", stdin);

	cin >> n;
	int s = 0;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		s += a;
	}

	cin >> m;
	int ans = -1;
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r;
		if (s >= l && s <= r) {
			ans = s;
			break;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int index[500005];
int main()
{
    int n;
    cin>>n;
    int i,t;
    int s=0;
    for(i=0;i<n;i++)
    {
        cin>>t;
        s+=t;
    }
    int m;
    cin>>m;
    int l,r,j,ans=-1;
    for(i=0;i<m;i++)
    {
        cin>>l>>r;
        index[l]++;
        index[r+1]=-1;
    }
    for(i=2;i<=1e5;i++)
    {
        index[i]+=index[i-1];
    }
    for(i=s;i<=1e5;i++)
    {
        if(index[i]==1)
        {
            ans=i;
            break;
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        cin>>t;
        a.push_back(t);
    }
    sort(a.begin(),a.end());
    int m;
    cin>>m;
    int l,r;
    vector<int> b;
    for(i=0;i<m;i++)
    {
        cin>>l>>r;
        b.push_back(r-l+1);
    }
    sort(b.begin(),b.end());
    int j=0;
    int ans=0;
    for(i=0;i<n;i++)
    {
        if(j<m)
        {
            if(b[j]>=a[i])
            {
                b[j]-=a[i];
                ans+=a[i];
            }
            else
            {
                ans+=b[j];
                j++;
                i--;
            }
        }
        else
        {
            cout<<-1;
            return 0;
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        cin>>t;
        a.push_back(t);
    }
    sort(a.begin(),a.end());
    int m;
    cin>>m;
    int l,r;
    vector<int> b;
    for(i=0;i<m;i++)
    {
        cin>>l>>r;
        b.push_back(r-l+1);
    }
    sort(b.begin(),b.end());
    int j=0;
    int ans=0;
    for(i=0;i<n;i++)
    {
        if(j<m)
        {
            if(b[j]>=a[i])
            {
                b[j]-=a[i];
                ans+=a[i];
            }
            else
            {
                j++;
                i--;
            }
        }
        else
        {
            cout<<-1;
            return 0;
        }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>  /// G
using namespace std;
int n,m,a[1005];
struct node
{
    int l,r;
}b[1005];
int cmp(node a,node b)
{
    return a.r-a.l<b.r-b.l;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>m;
    for(int i=0;i<m;i++)
        cin>>b[i].l>>b[i].r;
    int sum=0;
    sort(a,a+n);
    sort(b,b+m,cmp);
    int t=0;
    for(int i=0;i<n;i++)
    {
        while(a[i]>(b[t].r-b[t].l)+1&&t!=m)
        {
            t++;
        }
        if(t==m)
            break;
        else
             sum+=((b[t].r-b[t].l)+1);
    }
    if(t==m)
        printf("-1\n");
    else
        printf("%d\n",sum);
    return 0;
}


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

int main()
{
    int n,m,a,l,r;
    while(~scanf("%d",&n))
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            sum+=a;
        }
        scanf("%d",&m);
        int maxn=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&l,&r);
            maxn=max(maxn,r);
        }
        if(sum<=maxn)
            printf("%d\n",sum);
        else
            printf("-1\n");
    }
    return 0;
}


#include<bits/stdc++.h>  /// G
using namespace std;
int n,m,a[1005];
struct node
{
    int l,r;
}b[1005];
int cmp(node a,node b)
{
    return a.r-a.l<b.r-b.l;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>m;
    for(int i=0;i<m;i++)
        cin>>b[i].l>>b[i].r;
    int sum=0;
    sort(a,a+n);
    sort(b,b+m,cmp);
    int t=0;
    for(int i=0;i<n;i++)
    {
        while(a[i]>(b[t].r-b[t].l)+1&&t!=m)
        {
            t++;
        }
        if(t==m)
            break;
        else
             sum+=a[i];
    }
    if(t==m)
        printf("-1\n");
    else
        printf("%d\n",sum);
    return 0;
}


#include<bits/stdc++.h>  /// G
using namespace std;
int n,m,a[1005];
struct node
{
    int l,r;
}b[1005];
int cmp(node a,node b)
{
    return a.r-a.l<b.r-b.l;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>m;
    for(int i=0;i<m;i++)
        cin>>b[i].l>>b[i].r;
    int sum=0;
    sort(a,a+n);
    sort(b,b+m,cmp);
    int t=0;
    for(int i=0;i<n;i++)
    {
        while(a[i]>(b[t].r-b[t].l)+1&&t!=m)
        {
            t++;
        }
        if(t==m)
            break;
        else
        {
            sum+=a[i];
            b[t].l+=a[i];
            if(b[t].l>=b[t].r)
                t++;
        }
    }
    if(t==m)
        printf("-1\n");
    else
        printf("%d\n",sum);
    return 0;
}


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

int main()
{
    int n,m,a,l,r;
    while(~scanf("%d",&n))
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            sum+=a;
        }
        scanf("%d",&m);
        int mixn=0x3f3f3f3f;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&l,&r);
            if(l<=sum&&sum<=r)
            {
                mixn=sum;
            }
            if(l>=sum)
            {
                mixn=min(mixn,l);
            }
        }
        if(mixn<0x3f3f3f3f)
            printf("%d\n",mixn);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
#define pr pair<long long, long long>

using namespace std;

bool cmp(const pr &a, const pr &b){
	return a.first < b.first;
}
pr p[200005];
int main()
{
	long long n, m, sum=0, l, r, maxi=-1, ans=1234435456;

	long long in;
	scanf("%lld", &n);
	while(n--){
		cin >> in;
		sum += in;
	}
	scanf("%lld", &m);
	for(int i=0; i<m; i++){
		scanf("%lld %lld", &p[i].first, &p[i].second);
		maxi = max(maxi, p[i].first);
		maxi = max(maxi, p[i].second);
	}

	sort(p, p+m, cmp);

	if(sum > maxi) cout << -1;
	else{
		for(int i=0; i<m; i++){
			if(p[i].first >= sum){
				ans = min(sum, p[i].first);
			}else if(p[i].second >= sum) ans = sum;
		}

		cout << ans;
	}

	return 0;
}


#include <bits/stdc++.h>
#define pr pair<long long, long long>

using namespace std;

bool cmp(const pr &a, const pr &b){
	return a.first < b.first;
}
pr p[200005];
int main()
{
	long long n, m, sum=0, l, r, maxi=-1, ans=1234435456;

	long long in;
	scanf("%lld", &n);
	while(n--){
		cin >> in;
		sum += in;
	}
	cout << sum << endl;
	scanf("%lld", &m);
	for(int i=0; i<m; i++){
		scanf("%lld %lld", &p[i].first, &p[i].second);
		maxi = max(maxi, p[i].first);
		maxi = max(maxi, p[i].second);
	}

	sort(p, p+m, cmp);

	if(sum > maxi) cout << -1;
	else{
		for(int i=0; i<m; i++){
			if(p[i].first >= sum){
				ans = min(ans, p[i].first);
			}else if(p[i].second >= sum) ans = sum;
		}

		cout << ans;
	}

	return 0;
}


#include <bits/stdc++.h>
#define pr pair<long long, long long>

using namespace std;

bool cmp(const pr &a, const pr &b){
	return a.first < b.first;
}
pr p[200005];
int main()
{
	long long n, m, sum=0, l, r, maxi=-1, ans=1234435456;

	long long in;
	scanf("%lld", &n);
	while(n--){
		cin >> in;
		sum += in;
	}
	scanf("%lld", &m);
	for(int i=0; i<m; i++){
		scanf("%lld %lld", &p[i].first, &p[i].second);
		maxi = max(maxi, p[i].first);
		maxi = max(maxi, p[i].second);
	}

	sort(p, p+m, cmp);

	if(sum > maxi) cout << -1;
	else{
		for(int i=0; i<m; i++){
			if(p[i].first >= sum){
				ans = min(ans, p[i].first);
			}else if(p[i].second >= sum) ans = sum;
		}

		cout << ans;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	long long n, m, sum=0, l, r, maximum=-1, min_l=123456789;
	long long in;
	scanf("%lld", &n);
	while(n--){
		cin >> in;
		sum += in;
	}
	scanf("%lld", &m);
	while(m--){
		scanf("%lld %lld", &l, &r);
		maximum = max(maximum, l);
		maximum = max(maximum, r);
		if(l >= sum) min_l = min(min_l, l);
	}

	if(sum > maximum) cout << -1;
	else{
		if(min_l == 123456789) cout << sum;
		else cout << min_l;
	}

	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
	long long int sum = 0;
	int n;
	cin >> n;
	int a;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		sum += a;
	}
	int m;
	cin >> m;
	int k;
	int max = 0;
	int boof; int mmax;
	for (int i = 0; i < 2*m; i++)
	{
		boof = k;
		cin >> k;
		if ( k > max && i % 2 == 1 ){
		 	max = k;
		 	mmax = boof;
		 }
	}
	if ( sum <= max && sum >= mmax)
	{
		cout << sum;
	} else if ( sum <= mmax ) cout << mmax; else cout << -1;
}

/*
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> a[i][j];
*/


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
	long long int sum = 0;
	int n;
	cin >> n;
	int a;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		sum += a;
	}
	int m;
	cin >> m;
	int k, g;
	int max = 0;
	int boof; int mmax;
	for (int i = 0; i < m; i++)
	{
		cin >> k >> g;
		if ( k > max ){
		 	max = g;
		 	mmax = k;
		 }
	}
	if ( sum <= max && sum >= mmax)
	{
		cout << sum;
	} else if ( sum <= mmax ) cout << mmax; else cout << -1;
}

/*
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> a[i][j];
*/