// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll,ll>
#define vpii vector<pair<ll,ll> >
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a,i) memset(a,i,sizeof(a))
#define all(x) x.begin(),x.end()
#define itit(it,a) for(auto it=(a).begin(); it!=(a).end(); it++)
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define repr(i,a,b) for(ll i=a; i>b; i--)
#define reprr(i,a,b) for(ll i=a; i>=b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x,ll y,ll z) {return max(max(x,y),z);}
ll min3(ll x,ll y,ll z) {return min(min(x,y),z);}
const ll N=1e5+10,M=2e5+10,M2=1e6+10,mod=1e9+7,inf=1e17+10;
const int INF=1e9+7;
void add(int& a, int b) {a+=b;if(a>=mod){a-=mod;}}

int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
//assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
ll power(ll x,ll n){ll result=1;while(n>0){if(n%2==1)result=(result*x)%mod; x=((x%mod)*(x%mod))%mod;n=n/2;}return result;}

void solve(){
    int n,m;
    cin >> n >> m;
    int c[n+1];
    rep(i,1,n+1) cin >> c[i];
    int a[n+1];
    rep(i,1,m+1) cin >> a[i];
    int cur=1;
    int ans=0;
    for(int i=1; i<=n; i++){
        if(cur>m) break;
        if(a[cur]>=c[i]){
            ans++;
            cur++;
        }
    }
    cout << ans;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll,ll>
#define vpii vector<pair<ll,ll> >
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a,i) memset(a,i,sizeof(a))
#define all(x) x.begin(),x.end()
#define itit(it,a) for(auto it=(a).begin(); it!=(a).end(); it++)
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define repr(i,a,b) for(ll i=a; i>b; i--)
#define reprr(i,a,b) for(ll i=a; i>=b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x,ll y,ll z) {return max(max(x,y),z);}
ll min3(ll x,ll y,ll z) {return min(min(x,y),z);}
const ll N=1e5+10,M=2e5+10,M2=1e6+10,mod=1e9+7,inf=1e17+10;
const int INF=1e9+7;
void add(int& a, int b) {a+=b;if(a>=mod){a-=mod;}}

int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
//assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
ll power(ll x,ll n){ll result=1;while(n>0){if(n%2==1)result=(result*x)%mod; x=((x%mod)*(x%mod))%mod;n=n/2;}return result;}

void solve(){
    int n,m;
    cin >> n >> m;
    int c[n+1];
    rep(i,1,n+1) cin >> c[i];
    int a[n+1];
    rep(i,1,m+1) cin >> a[i];
    int cur=1;
    int ans=0;
    for(int i=1; i<=n; i++){
        if(cur>m) break;
        if(a[cur]>c[i]){
            ans++;
            cur++;
        }
    }
    cout << ans;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if(a[i+j]<b[j])
				c++;
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if((i+j)<n)
			{
				if(a[i+j]<b[j])
					{
						c++;
					}
				else
					break;
			}
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if(a[i+j]<b[j]&&((i+j)<n))
				c++;
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	int ans=0;
   int k=0;
   for(int i=0;i<n && k<m;i++)
   {
     if(a[i]<=b[k])
       {
         ans++;
         k++;
       }
   }
   cout << ans;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if((i+j)<n)
			{if(a[i+j]<b[j])
							c++;}
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	int n,m;
	cin>>n>>m;
	int a[n];
	int b[m];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<m;i++)
	{
		cin>>b[i];
	}
	int max=0;
	for(int i=0;i<n;i++)
	{
		int c=0;
		for(int j=0;j<m;j++)
		{
			if(a[i+j]<b[j])
				c++;
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if((i+j)<n)
			{if(a[i+j]<b[j]&&)
							c++;}
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll b[m];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(ll i=0;i<m;i++)
	{
		cin>>b[i];
	}
	ll max=0;
	for(ll i=0;i<n;i++)
	{
		ll c=0;
		for(ll j=0;j<m;j++)
		{
			if((i+j)<n)
			{
				if(a[i+j]<=b[j])
					{
						c++;
					}
				else
					break;
			}
		}
		if(max<c)
			max=c;
	}
	cout<<max<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int minn=min(m,n);
	for (int i=0;i<minn;i++)
	{
		if (c[i]==0)
		{
			continue;
		}
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (i==0 && ans==0)
		{
			break;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (i==0 && ans==0)
		{
			break;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int minn=min(m,n);
	int ans1=0;
	for (int i=0;i<minn;i++)
	{
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (ans==ans1)
		{
			break;
		}
		else
		{
			ans1++;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int minn=min(m,n);
	int ans1=0;
	for (int i=0;i<minn;i++)
	{
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (ans==ans1)
		{
			break;
		}
		else
		{
			ans1++;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int ans1=0;
	for (int i=0;i<n;i++)
	{
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (ans==ans1)
		{
			break;
		}
		else
		{
			ans1++;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

    for (int i = 0,j=0; i < n && j < m; i++, j++)
    {
        while (j < m && a[i] < c[j]) j++;
        if (j >= m) break;
        ans = i+1;
    }
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if (c[i]==0)
		{
			continue;
		}
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (i==0 && ans==0)
		{
			break;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
using namespace std;

int c[1001];
int a[1001];

int main()
{
	int m,n;
	int ans=0;
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++)
	{
		scanf("%d",&c[i]);
	}
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int minn=min(m,n);
	int ans1=0;
	for (int i=0;i<minn;i++)
	{
		if (c[i]==0)
		{
			continue;
		}
		for (int i1=i;i1<m;i1++)
		{
			if (c[i1]==0)
			{
				continue;
			}
			else
			{
				if (a[i]>=c[i1])
				{
					c[i1]=0;
					ans++;
					break;
				}
			}
		}
		if (ans==ans1)
		{
			break;
		}
		else
		{
			ans1++;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
	int s=0,n,m;
	cin>>n>>m;
	deque<int> d(m);
	int num[n];
	for(int i=0;i<n;i++)
		cin>>num[i];
	for(int j=0;j<m;j++)
		cin>>d[j];
	for(int i=0;i<n;i++){
		if(d.size() && d[0]>=num[i]){
			s++;
			cout<<"I: "<<i<<endl;
			d.pop_front();
		}
	}
	cout<<s;
	return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
	int s=0,n,m;
	cin>>n>>m;
	deque<int> d(m);
	int num[n];
	for(int i=0;i<n;i++)
		cin>>num[i];
	for(int j=0;j<m;j++)
		cin>>d[j];
	for(int i=0;i<n;i++){
		if(d.size() || d[0]>=num[i]){
			s++;
			d.pop_front();
		}
	}
	cout<<s;
	return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
	int s=0,n,m;
	cin>>n>>m;
	deque<int> d(m);
	int num[n];
	for(int i=0;i<n;i++)
		cin>>num[i];
	for(int j=0;j<m;j++)
		cin>>d[j];
	for(int i=0;i<n;i++){
		if(d.size() && d[0]>=num[i]){
			s++;
			d.pop_front();
		}
	}
	cout<<s;
	return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
	int s=0,n,m;
	cin>>n>>m;
	deque<int> d(m);
	int num[n];
	for(int i=0;i<n;i++)
		cin>>num[i];
	for(int j=0;j<m;j++)
		cin>>d[j];
	for(int i=0;i<n;i++){
		if(d.size() || d[0]<num[i])
		else{
			s++;
			d.pop_front();
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
    int n,m,cost[1010]={0};
    queue<int> money;
    cin>>n>>m;
    int i,j=0;
    for(i=0;i<n;i++){
        scanf("%d",&cost[i]);
    }
    for(i=0;i<m;i++){
        int temp;
        scanf("%d",&temp);
        money.push(temp);
    }
    //sort(cost,cost+n);
    //sort(money,money+m);
    for(i=0;i<max(n, m);i++){
        if(cost[i]<=money.front()&&!money.empty()){
            money.pop();
            j++;
        }
        else{
            continue;
        }
    }
    cout<<j<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
    int n,m,cost[1010]={0};
    queue<int> money;
    cin>>n>>m;
    int i,j=0;
    for(i=0;i<n;i++){
        scanf("%d",&cost[i]);
    }
    for(i=0;i<m;i++){
        int temp;
        scanf("%d",&temp);
        money.push(temp);
    }
    //sort(cost,cost+n);
    //sort(money,money+m);
    for(i=0;i<n;i++){
        if(cost[i]<=money.front()){
            money.pop();
            j++;
        }
        else{
            continue;
        }
    }
    cout<<j<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
    int n,m,cost[1010]={0};
    queue<int> money;
    cin>>n>>m;
    int i,j=0;
    for(i=0;i<n;i++){
        scanf("%d",&cost[i]);
    }
    for(i=0;i<m;i++){
        int temp;
        scanf("%d",&temp);
        money.push(temp);
    }
    for(i=0;i<n;i++){
        if(!money.empty()&&cost[i]<=money.front()){
            money.pop();
            j++;
        }
    }
    cout<<j<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
    int n,m,cost[1010]={0};
    queue<int> money;
    cin>>n>>m;
    int i,j=0;
    for(i=0;i<n;i++){
        scanf("%d",&cost[i]);
    }
    for(i=0;i<m;i++){
        int temp;
        scanf("%d",&temp);
        money.push(temp);
    }
    //sort(cost,cost+n);
    //sort(money,money+m);
    for(i=0;i<min(n,m);i++){
        if(cost[i]<=money.front()){
            money.pop();
            j++;
        }
        else{
            continue;
        }
    }
    cout<<j<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
    int n,m,cost[1010]={0},money[1010]={0};
    cin>>n>>m;
    int i,j=0;
    for(i=0;i<n;i++){
        scanf("%d",&cost[i]);
    }
    for(i=0;i<m;i++){
        scanf("%d",&money[i]);
    }
    for(i=0;i<min(n,m);i++){
        if(money[i]>=cost[i]){
            j++;
        }
    }
    cout<<j<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define forn(i, a, n) for(int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
//#define f first
//#define sex second

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <set>
#include <ostream>
#include <queue>
#include <functional>
#include <sstream>
#include <map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#pragma comment(linker, "/STACK:228228000")
const ll mod = 998244353228;

ll n, m, res;
ll c[1007], a[1007];
int main()
{
	/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	*/

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

	cin >> n >> m;
	forn(i, 0, n)
	{
		cin >> c[i];
	}
	forn(i, 0, n)
	{
		cin >> a[i];
	}

	forn(i, 0, m)
	{
		if (i > res)
			break;
		forn(j, 0, n)
		{
			if (c[j] <= a[i])
			{
				c[i] = mod;
				res++;
				break;
			}
		}
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define forn(i, a, n) for(int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
//#define f first
//#define sex second

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <set>
#include <ostream>
#include <queue>
#include <functional>
#include <sstream>
#include <map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#pragma comment(linker, "/STACK:228228000")
const ll mod = 998244353228;

ll n, m, res;
ll c[1007], a[1007];
int main()
{
	/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	*/

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

	cin >> n >> m;
	forn(i, 0, n)
	{
		cin >> c[i];
	}
	forn(i, 0, n)
	{
		cin >> a[i];
	}

	forn(i, 0, m)
	{
		if (i > res)
			break;
		forn(j, 0, n)
		{
			if (c[j] <= a[i])
			{
				res++;
				break;
			}
		}
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define forn(i, a, n) for(int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
//#define f first
//#define sex second

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <set>
#include <ostream>
#include <queue>
#include <functional>
#include <sstream>
#include <map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#pragma comment(linker, "/STACK:228228000")
const ll mod = 998244353228;

ll n, m, res;
ll c[1007], a[1007];
int main()
{
	/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	*/

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

	cin >> n >> m;
	forn(i, 0, n)
	{
		cin >> c[i];
	}
	forn(i, 0, n)
	{
		cin >> a[i];
	}

	ll i = 0, j = 0;
	while(i < n && j < m)
	{
		if (c[i] <= a[j])
		{
			res++;
			j++;
		}
		i++;
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define forn(i, a, n) for(int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
//#define f first
//#define sex second

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <set>
#include <ostream>
#include <queue>
#include <functional>
#include <sstream>
#include <map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#pragma comment(linker, "/STACK:228228000")
const ll mod = 998244353228;

ll n, m, res;
ll c[1007], a[1007];
int main()
{
	/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	*/

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

	cin >> n >> m;
	forn(i, 0, n)
	{
		cin >> c[i];
	}
	forn(i, 0, n)
	{
		cin >> a[i];
	}

	forn(i, 0, m)
	{
		if (i > res)
			break;
		forn(j, 0, n)
		{
			if (c[j] <= a[i])
			{
				a[i] = mod;
				res++;
				break;
			}
		}
	}
	cout << res;
	return 0;
}


#include <iostream>
using namespace std;
int c[1001],a[1001],i,j,n,m;
main() {
	for(cin>>n>>m;i<n;i++)cin>>c[i];
	for(;cin>>a[j++];);
	for(i=j=0;j!=m&&i!=n;i++)if(a[j]>=c[i])j++;
	cout<<j;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <cmath>
#include <cstring>
#include <sstream>
#include <string.h>
#include <array>
#include <tuple>
#include <set>
#include <iterator>
#include <cstdio>
#include <string>
#include <ctime>
#include <valarray>
#include <utility>
#include <iomanip>
#include <list>
using namespace std;

# define ll long long
# define PI 3.1415926535

int main()
{
	int n,m,count=0,j=0;
	cin >> n >> m;
	int c[n],r[m];
	for(int i=0 ;i<n; i++)
		cin >> c[i];
	for(int i=0; i<m; i++)
		cin >> r[i];
	int res = r[j];
	for(int i=0; i<n; i++)
	{
       if(res>=c[i] && count<=m)
       {
       	  ++count;
       	  res = r[++j];
       }
	}
	cout << count << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <cmath>
#include <cstring>
#include <sstream>
#include <string.h>
#include <array>
#include <tuple>
#include <set>
#include <iterator>
#include <cstdio>
#include <string>
#include <ctime>
#include <valarray>
#include <utility>
#include <iomanip>
#include <list>
using namespace std;

# define ll long long
# define PI 3.1415926535

int main()
{
	int n,m,count=0,j=0;
	cin >> n >> m;
	int c[n],r[m];
	for(int i=0 ;i<n; i++)
		cin >> c[i];
	for(int i=0; i<m; i++)
		cin >> r[i];
	int res = r[j];
	for(int i=0; i<n && count<=m; i++)
	{
       if(res>=c[i])
       {
       	  ++count;
       	  res = r[++j];
       }
	}
	cout << count << endl;

	return 0;
}


#include <iostream>
using namespace std;

int main() {
    int n,m;
    cin >> n>>m;
    int c[n];
    int a[m];
    for (int i = 0; i < n;i++) {
        cin >> c[i];
    }
    for (int i = 0; i < m;i++) {
        cin >> a[i];
    }
    int j = 0;
    int count = 0;
    for (int i = 0; i < n;i++)
    {
        if (c[i] <= a[j]) {
            j++;
            count++;
        }
    }
    cout << count << endl;
}


#include <iostream>
using namespace std;

int main() {
    int n,m;
    cin >> n>>m;
    int c[n];
    int a[m];
    for (int i = 0; i < n;i++) {
        cin >> c[i];
    }
    for (int i = 0; i < m;i++) {
        cin >> a[i];
    }
    int j = 0;
    int count = 0;
    for (int i = 0; i < n;i++)
    {
        if (c[i] <= a[j]) {
            count++;
            j++;
            if (j >= m) {
                break;
            }
        }
    }
    cout << count << endl;
}


#include <iostream>
using namespace std;

int main() {
    int n,m;
    cin >> n>>m;
    int c[n];
    int a[m];
    for (int i = 0; i < n;i++) {
        cin >> c[i];
    }
    for (int i = 0; i < m;i++) {
        cin >> a[i];
    }
    int j = 0;
    int count = 0;
    for (int i = 0; i < n;i++)
    {
        if (c[i] <= a[j]) {
            j++;
            if (j >= m) {
                break;
            }
            count++;
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define what_is(x) cerr << #x << " is " << x << endl
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define endl "\n"

using namespace std;
using ii = pair<int, int>;
using ll = long long;

int main() {
  IOS;
  int n, m, x, g = 0;
  cin >> n >> m;
  vector<int> v(n);
  REP(i, 0, n - 1) cin >> v[i];
  REP(j, 0, m - 1) {
    cin >> x;
    g += (x >= v[j]);
  }

  cout << g << endl;

  return 0;
}


#include <bits/stdc++.h>

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define what_is(x) cerr << #x << " is " << x << endl
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define endl "\n"

using namespace std;
using ii = pair<int, int>;
using ll = long long;

int main() {
  IOS;
  int n, m, x, g = 0;
  cin >> n >> m;
  queue<int> qt;
  vector<int> v(n);
  REP(i, 0, n - 1) cin >> v[i];
  REP(j, 0, m - 1) {
    cin >> x;
    qt.push(x);
  }
  REP(k, 0, n - 1) {
    if (qt.size() > 0 and qt.front() >= v[k]) {
      qt.pop();
      g++;
    }
  }

  cout << g << endl;

  return 0;
}


#include <bits/stdc++.h>

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define what_is(x) cerr << #x << " is " << x << endl
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define endl "\n"

using namespace std;
using ii = pair<int, int>;
using ll = long long;

int main() {
  IOS;
  int n, m, x, g = 0, k = 0;
  cin >> n >> m;
  queue<int> qt;
  vector<int> v(n);
  REP(i, 0, n - 1) cin >> v[i];
  REP(j, 0, m - 1) {
    cin >> x;
    qt.push(x);
  }
  REP(i, 0, n - 1) {
    if (qt.front() >= v[i]) {
      qt.pop();
      g++;
    }
  }

  cout << g << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int>a;
	vector<int>b;
	int x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		a.push_back(x);
	}
	for(int j=0;j<m;j++)
	{
		cin>>y;
		b.push_back(y);
	}
	int item=0;
	for(int k=0;k<m;k++)
	{
		if(b[k]>=a[k])
		{
			item++;
		}
		else if(b[k]<a[k])
		{
			a.erase(a.begin()+k);
		}
	}
	cout<<item;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int>a;
	vector<int>b;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		a.push_back(x);
	}
	for(int j=0;j<m;j++)
	{
		int y;
		cin>>y;
		b.push_back(y);
	}
	int item=0;
	for(int k=0;k<m;)
	{
		if(b[k]>=a[k])
		{
			item++;
		}
		else if(b[k]<a[k])
		{
			a.erase(a.begin()+k);
		}
		k++;
	}
	cout<<item;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int>a;
	vector<int>b;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		a.push_back(x);
	}
	for(int j=0;j<m;j++)
	{
		int y;
		cin>>y;
		b.push_back(y);
	}
	int item=0;
	for(int k=0;k<n;)
	{
		if(b[k]>=a[k])
		{
			item++;
		}
		else if(b[k]<a[k])
		{
			a.erase(a.begin()+k);
			k--;
			n--;
		}
		k++;
	}
	cout<<item;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int>a;
	vector<int>b;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		a.push_back(x);
	}
	for(int j=0;j<m;j++)
	{
		int y;
		cin>>y;
		b.push_back(y);
	}
	for(int i=0;i<n-m;i++)
	b.push_back(0);
	int item=0;
	for(int k=0;k<n;)
	{
		if(b[k]>=a[k])
		{
			item++;
		}
		else if(b[k]<a[k])
		{
			a.erase(a.begin()+k);
			k--;
			n--;
		}
		k++;
	}
	cout<<item;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define S(x) scanf("%d",&x)
#define S1(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define Sd(x) scanf("%lf",&x)
#define Pd(x) printf("%0.10lf\n",x)
#define P1(x) printf("%lld\n",x)
#define Ps(x) printf("%d ",x)
#define P1s(x) printf("%lld ",x)
#define St(x) scanf("%s",x)
#define Pt(x) printf("%s\n",x)
#define Sa(a,n) for(int iter=0;iter<n;iter++){scanf("%d",&a[iter]);}
#define Pa(a,n) for(int iter=0;iter<n;iter++){printf("%lld ",a[iter]);}putchar('\n')
#define Y printf("YES\n")
#define N printf("NO\n")

#define mod 1000000007
#define ll int

ll power(ll b, ll e, ll m) {
	ll p = 1;
	while (e > 0) {
		if (e & 1)
			p = (p * b) % m;
		e = e >> 1;
		b = (b * b) % m;
	}
	return p;
}
ll p[1000001],a[1000001],b[1000001];
set<pair<ll,ll> >cp[3];
int main() {
	ll t, n, i, j,k,d, m = 0,q,x,y,cr,g,l,r,mid;
	S(n);
	Sa(p,n);
	Sa(a,n);
	Sa(b,n);
	for(i=0;i<n;i++){
		cp[a[i]-1].insert({p[i],i});
		cp[b[i]-1].insert({p[i],i});
	}
	S(m);
	while(m--){
		S(j);
		if(j<1||j>3){
			P(-1);
			continue;
		}
		set<pair<ll,ll> >::iterator it=cp[j-1].begin();
		if(it==cp[j-1].end()){
			P(-1);
			continue;
		}
		k=it->second;
		Ps(it->first);
		cp[j-1].erase(it);
		for(l=0;l<3;l++)
			if(cp[l].find({it->first,it->second})!=cp[l].end())
				cp[l].erase({it->first,it->second});

	}
	return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define S(x) scanf("%d",&x)
#define S1(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define Sd(x) scanf("%lf",&x)
#define Pd(x) printf("%0.10lf\n",x)
#define P1(x) printf("%lld\n",x)
#define Ps(x) printf("%d ",x)
#define P1s(x) printf("%lld ",x)
#define St(x) scanf("%s",x)
#define Pt(x) printf("%s\n",x)
#define Sa(a,n) for(int iter=0;iter<n;iter++){scanf("%d",&a[iter]);}
#define Pa(a,n) for(int iter=0;iter<n;iter++){printf("%lld ",a[iter]);}putchar('\n')
#define Y printf("YES\n")
#define N printf("NO\n")

#define mod 1000000007
#define ll int

ll power(ll b, ll e, ll m) {
	ll p = 1;
	while (e > 0) {
		if (e & 1)
			p = (p * b) % m;
		e = e >> 1;
		b = (b * b) % m;
	}
	return p;
}
ll p[200001],a[200001],b[200001];
set<pair<ll,ll> >cp[3];
int main() {
	ll t, n, i, j,k,d, m = 0,q,x,y,cr,g,l,r,mid;
	S(n);
	Sa(p,n);
	Sa(a,n);
	Sa(b,n);
	for(i=0;i<n;i++){
		cp[a[i]-1].insert({p[i],i});
		cp[b[i]-1].insert({p[i],i});
	}
	S(m);
	while(m--){
		S(j);
		set<pair<ll,ll> >::iterator it=cp[j-1].begin();
		if(it==NULL||it==cp[j-1].end()){
			P(-1);
			continue;
		}
		k=it->second;
		Ps(it->first);
		cp[j-1].erase(it);
		for(l=0;l<3;l++)
			if(cp[l].find({it->first,it->second})!=cp[l].end())
				cp[l].erase({it->first,it->second});

	}
	return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define S(x) scanf("%d",&x)
#define S1(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define Sd(x) scanf("%lf",&x)
#define Pd(x) printf("%0.10lf\n",x)
#define P1(x) printf("%lld\n",x)
#define Ps(x) printf("%d ",x)
#define P1s(x) printf("%lld ",x)
#define St(x) scanf("%s",x)
#define Pt(x) printf("%s\n",x)
#define Sa(a,n) for(int iter=0;iter<n;iter++){scanf("%d",&a[iter]);}
#define Pa(a,n) for(int iter=0;iter<n;iter++){printf("%lld ",a[iter]);}putchar('\n')
#define Y printf("YES\n")
#define N printf("NO\n")

#define mod 1000000007
#define ll int

ll power(ll b, ll e, ll m) {
	ll p = 1;
	while (e > 0) {
		if (e & 1)
			p = (p * b) % m;
		e = e >> 1;
		b = (b * b) % m;
	}
	return p;
}
ll p[200001],a[200001],b[200001];
map<ll,set<pair<ll,ll> > >cp;
int main() {
	ll t, n, i, j,k,d, m = 0,q,x,y,cr,g,l,r,mid;
	S(n);
	Sa(p,n);
	Sa(a,n);
	Sa(b,n);
	for(i=0;i<n;i++){
		cp[a[i]-1].insert({p[i],i});
		cp[b[i]-1].insert({p[i],i});
	}
	S(m);
	while(m-->0){
		S(j);
		if(cp[j-1].empty()){
			P(-1);
			continue;

		}
		set<pair<ll,ll> >::iterator it=cp[j-1].begin();
		if(it==cp[j-1].end()){
			P(-1);
			continue;
		}
		k=it->second;
		Ps(it->first);
		cp[j-1].erase(it);
		for(l=0;l<3;l++)
			if(cp[l].find({it->first,it->second})!=cp[l].end())
				cp[l].erase({it->first,it->second});

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#define ll long long
int main()
{
    int n,customer;
    cin>>n;
    vector <int> price(n);
    vector <int> fcolor(n);
    vector <int> bcolor(n);
    map <int,set<int> > m;
    for(int i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>fcolor[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>bcolor[i];
    }
    cin>>customer;
    vector <int> fav(customer);
    vector <int> cost(customer);
    for(int i=0;i<customer;i++)
    {
        cin>>fav[i];
    }
    for(int i=0;i<n;i++)
    {
        m[fcolor[i]].insert(price[i]);
        m[bcolor[i]].insert(price[i]);
    }
    set<int>::iterator it;
    for(int i=0;i<customer;i++)
    {
        if(fav[i]>3 && fav[i]<1)
            cost[i]=-1;
        else
        {
            it=m[fav[i]].begin();
            cost[i]=*it;
            if(!m[1].empty())
            m[1].erase(*it);
            if(!m[2].empty())
            m[2].erase(*it);
            if(!m[3].empty())
            m[3].erase(*it);
        }
    }
    for(int i=0;i<customer;i++)
    {
        if(cost[i]==0)
            cost[i]=-1;
        cout<<cost[i]<<" ";
    }
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,customer;
    cin>>n;
    vector <ll> price(n);
    vector <int> fcolor(n);
    vector <int> bcolor(n);
    map <int,set<ll> > m;
    for(ll i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>fcolor[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>bcolor[i];
    }
    cin>>customer;
    vector <ll> fav(customer);
    vector <ll> cost(customer);
    for(ll i=0;i<customer;i++)
    {
        cin>>fav[i];
    }
    for(ll i=0;i<n;i++)
    {
        m[fcolor[i]].insert(price[i]);
        m[bcolor[i]].insert(price[i]);
    }
    set<ll>::iterator it;
    for(ll i=0;i<customer;i++)
    {
        if(fav[i]>3 && fav[i]<1)
            cost[i]=-1;
        else
        {
            it=m[fav[i]].begin();
            cost[i]=*it;
            m[1].erase(*it);
            m[2].erase(*it);
            m[3].erase(*it);
        }
    }
    for(ll i=0;i<customer;i++)
    {
        if(cost[i]==0)
            cost[i]=-1;
        cout<<cost[i]<<" ";
    }
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,customer;
    cin>>n;
    vector <ll> price(n);
    vector <int> fcolor(n);
    vector <int> bcolor(n);
    map <int,set<ll> > m;
    for(ll i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>fcolor[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>bcolor[i];
    }
    cin>>customer;
    vector <ll> fav(customer);
    vector <ll> cost(customer);
    for(ll i=0;i<customer;i++)
    {
        cin>>fav[i];
    }
    for(ll i=0;i<n;i++)
    {
        m[fcolor[i]].insert(price[i]);
        m[bcolor[i]].insert(price[i]);
    }
    set<ll>::iterator it;
    for(ll i=0;i<customer;i++)
    {
        if(fav[i]>3 && fav[i]<1)
            cost[i]=-1;
        else
        {
            it=m[fav[i]].begin();
            cost[i]=*it;
            if(!m[1].empty())
            m[1].erase(*it);
            if(!m[2].empty())
            m[2].erase(*it);
            if(!m[3].empty())
            m[3].erase(*it);
        }
    }
    for(ll i=0;i<customer;i++)
    {
        if(cost[i]==0)
            cost[i]=-1;
        cout<<cost[i]<<" ";
    }
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int p[n];
	int front[n];
	int back[n];
	for(int i=0;i<n;i++)
	cin >> p[i];
	for(int i=0;i<n;i++)
	cin >> front[i];
	for(int i=0;i<n;i++)
	cin >> back[i];
	bool vis[n];
	memset(vis,false,sizeof(vis));
	vector< priority_queue< pair<int,int> , vector< pair<int,int> > , greater< pair<int,int> > > > v;
	priority_queue< pair<int,int> , vector< pair<int,int> > , greater< pair<int,int> > > pq1,pq2,pq3;
	for(int i=0;i<n;i++)
	{
		if(front[i]==1||back[i]==1)
		pq1.push(make_pair(p[i],i));
		if(front[i]==2||back[i]==2)
		pq2.push(make_pair(p[i],i));
		if(front[i]==3||back[i]==3)
		pq3.push(make_pair(p[i],i));
	}
	v.push_back(pq1);
	v.push_back(pq2);
	v.push_back(pq3);
//	for(int i=0;i<3;i++)
//	{
//		while(!v[i].empty())
//		{
//			cout << v[i].top().first << " ";
//			v[i].pop();
//		}
//		cout << "\n";
//	}
	int m;
	cin >> m;
	int arr[m];
	for(int i=0;i<m;i++)
	cin >> arr[i];
	vector< int>ans;
	for(int i=0;i<m;i++)
	{
		int idx;
		if(arr[i]==1)
		idx=0;
		else if(arr[i]==2)
		idx=1;
		else
		idx=2;
		int val=-1;
		while(!v[idx].empty())
		{
			pair<int,int>temp;
			temp=v[idx].top();
			if(vis[temp.second]==false)
			{
				val=temp.first;
				vis[temp.second]=true;
				v[idx].pop();
				break;
			}
			else
			v[idx].pop();
		}
		ans.push_back(val);
	}
	for(int i=0;i<m;i++)
	cout << ans[i] << " ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,customer;
    cin>>n;
    vector <ll> price(n);
    vector <int> fcolor(n);
    vector <int> bcolor(n);
    map <int,set<ll> > m;
    for(ll i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>fcolor[i];
    }
    for(ll i=0;i<n;i++)
    {
        cin>>bcolor[i];
    }
    cin>>customer;
    vector <ll> fav(customer);
    vector <ll> cost(customer);
    for(ll i=0;i<customer;i++)
    {
        cin>>fav[i];
    }
    for(ll i=0;i<n;i++)
    {
        m[fcolor[i]].insert(price[i]);
        m[bcolor[i]].insert(price[i]);
    }
    set<ll>::iterator it;
    for(ll i=0;i<customer;i++)
    {
        if(fav[i]>3 && fav[i]<1)
            cost[i]=-1;
        else
        {
            it=m[fav[i]].begin();
            cost[i]=*it;
            m[fav[0]].erase(*it);
            m[fav[1]].erase(*it);
            m[fav[2]].erase(*it);
        }
    }
    for(ll i=0;i<customer;i++)
    {
        if(cost[i]==0)
            cost[i]=-1;
        cout<<cost[i]<<" ";
    }
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
//#define ll long long
int main()
{
    int n,customer;
    cin>>n;
    vector <int> price(n);
    vector <int> fcolor(n);
    vector <int> bcolor(n);
    map <int,set<int> > m;
    for(int i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>fcolor[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>bcolor[i];
    }
    cin>>customer;
    vector <int> fav(customer);
    vector <int> cost(customer);
    for(int i=0;i<customer;i++)
    {
        cin>>fav[i];
    }
    for(int i=0;i<n;i++)
    {
        m[fcolor[i]].insert(price[i]);
        m[bcolor[i]].insert(price[i]);
    }
    set<int>::iterator it;
    for(int i=0;i<customer;i++)
    {
        if(fav[i]>3 && fav[i]<1)
            cost[i]=-1;
        else
        {
        	if(!m[fav[i]].empty())
            {
			it=m[fav[i]].begin();
            cost[i]=*it;
        	}
            if(!m[1].empty())
            m[1].erase(*it);
            if(!m[2].empty())
            m[2].erase(*it);
            if(!m[3].empty())
            m[3].erase(*it);
        }
    }
    for(int i=0;i<customer;i++)
    {
        if(cost[i]==0)
            cost[i]=-1;
        cout<<cost[i]<<" ";
    }
    return  0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	for (int i = 1;i <= min(q, n);i++)
	{
		scanf("%d", &temp);
		bool flag = true;
		for (auto it : V[temp])
		{
			if (!vis[it.idx])
			{
				vis[it.idx] = 1;
				printf("%lld ", it.cost);
				flag = false;
				break;
			}
		}
		if (flag)cout << "-1 ";
	}
	for (int i = min(q, n) + 1;i <= q;i++)
		printf("-1 ");
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)if(V[i].size())
		sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	int begin1 = 0, begin2 = 0, begin3 = 0;
	for (int i = 1;i <= min(q, n);i++)
	{
		scanf("%d", &temp);
		bool flag = false;
		int &j = begin1;
		if (temp == 1)j = begin1;
		else if (temp == 2)j = begin2;
		else j = begin3;
		int Size = V[temp].size();
		for (j;j<Size;j++)
		{
			if (!vis[V[temp][j].idx])
			{
				vis[V[temp][j].idx] = 1;
				printf("%lld ", V[temp][j].cost);
				flag = true;
				break;
			}
		}
		if (!flag)printf("-1 ");
	}
	for (int i = min(q, n) + 1;i <= q;i++)
	{
		scanf("%d", &temp);
		printf("-1 ");
	}
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	for(int i=1;i<=min(q,n);i++)
	{
		scanf("%d", &temp);
		for (auto it : V[temp])
		{
			if (!vis[it.idx])
			{
				vis[it.idx] = 1;
				printf("%lld ", it.cost);
				break;
			}
		}
	}
	for (int i = min(q, n) + 1;i <= q;i++)
		printf("-1 ");
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)if (V[i].size())
		sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	int begin1 = 0, begin2 = 0, begin3 = 0;
	int themin = min(q, n);
	for (int i = 1;i <= q;i++)
	{
		scanf("%d", &temp);
		bool flag = false;
		int Size;
		if (temp == 1)
		{
			Size = V[1].size();
			for (begin1;begin1 < Size;begin1++)
			{
				if (!vis[V[1][begin1].idx])
				{
					vis[V[1][begin1].idx] = 1;
					flag = true;
					printf("%lld ", V[1][begin1].cost);
					break;
				}
			}
		}
		else if (temp == 2)
		{
			Size = V[2].size();
			for (begin2;begin2 < Size;begin2++)
			{
				if (!vis[V[2][begin2].idx])
				{
					vis[V[2][begin2].idx] = 1;
					flag = true;
					printf("%lld ", V[2][begin2].cost);
					break;
				}
			}
		}

		else
		{
			Size = V[3].size();
			for (begin3;begin3 < Size;begin3++)
			{
				if (!vis[V[3][begin3].idx])
				{
					vis[V[3][begin3].idx] = 1;
					flag = true;
					printf("%lld ", V[3][begin3].cost);
					break;
				}
			}
		}
		if (!flag)cout << "-1 ";
	}
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)if(V[i].size())
		sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	int begin1 = 0, begin2 = 0, begin3 = 0;
	int themin = min(q, n);
	for (int i = 1;i <=themin ;i++)
	{
		scanf("%d", &temp);
		bool flag = false;
		int Size;
		if (temp == 1)
		{
			Size = V[1].size();
			for (begin1;begin1 < Size;begin1++)
			{
				if (!vis[V[1][begin1].idx])
				{
					vis[V[1][begin1].idx] = 1;
					flag = true;
					printf("%lld ", V[1][begin1].cost);
					break;
				}
			}
		}
		else if (temp == 2)
		{
			Size = V[2].size();
			for (begin2;begin2 < Size;begin2++)
			{
				if (!vis[V[2][begin2].idx])
				{
					vis[V[2][begin2].idx] = 1;
					flag = true;
					printf("%lld ", V[2][begin2].cost);
					break;
				}
			}
		}

		else
		{
			Size = V[3].size();
			for (begin3;begin3 < Size;begin3++)
			{
				if (!vis[V[3][begin3].idx])
				{
					vis[V[3][begin3].idx] = 1;
					flag = true;
					printf("%lld ", V[3][begin3].cost);
					break;
				}
			}
		}
		if (!flag)cout << "-1 ";
	}
	for (int i = min(q, n) + 1;i <= q;i++)
	{
		scanf("%d", &temp);
		printf("-1 ");
	}
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	for (int i = 1;i <= min(q, n);i++)
	{
		scanf("%d", &temp);
		bool flag = true;
		for (auto it : V[temp])
		{
			if (!vis[it.idx])
			{
				vis[it.idx] = 1;
				printf("%lld ", it.cost);
				flag = false;
				break;
			}
		}
		if (!flag)cout << "-1 ";
	}
	for (int i = min(q, n) + 1;i <= q;i++)
		printf("-1 ");
	return 0;
}


#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
bool vis[200005];
typedef long long ll;

struct node
{
	ll cost;
	int idx;
	bool operator<(const node &b)const
	{
		return cost < b.cost;
	}
}nodes[200005];
vector<node>V[5];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		scanf("%lld", &nodes[i].cost);
	int temp;
	for (int i = 1;i <= n;i++)
	{
		nodes[i].idx = i;
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= n;i++)
	{
		scanf("%d", &temp);
		V[temp].push_back(nodes[i]);
	}
	for (int i = 1;i <= 3;i++)sort(V[i].begin(), V[i].end());
	int q;
	cin >> q;
	int begin1 = 0, begin2 = 0, begin3 = 0;
	for(int i=1;i<=min(q,n);i++)
	{
		scanf("%d", &temp);
		bool flag = false;
		int &j=begin1;
		if (temp == 1)j = begin1;
		else if (temp == 2)j = begin2;
		else j = begin3;
		int Size = V[temp].size();
		for (j;j<Size;j++)
		{
			if (!vis[V[temp][j].idx])
			{
				vis[V[temp][j].idx] = 1;
				printf("%lld ", V[temp][j].cost);
				flag = true;
				break;
			}
		}
		if (!flag)printf("-1 ");
	}
	for (int i = min(q, n) + 1;i <= q;i++)
		printf("-1 ");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int p[maxn], a[maxn], b[maxn];
bool vis[maxn];
set<pair<int, int> >s[5];

int main()
{
	int n, m, t;
	scanf("%d", &n);
	for (int i = 0;i < n;i++)    scanf("%d", &p[i]);
	for (int i = 0;i < n;i++)    scanf("%d", &a[i]);
	for (int i = 0;i < n;i++)    scanf("%d", &b[i]);
	for (int i = 0;i < n;i++)
	{
		s[a[i]].insert(make_pair(p[i], i));
		s[b[i]].insert(make_pair(p[i], i));
		vis[i] = true;
	}
	scanf("%d", &m);
	while (m--)
	{
		int ans = -1;
		scanf("%d", &t);
		while (!s[t].empty())
		{
			int pos = (*(s[t].begin())).second;
			s[t].erase(s[t].begin());
			if (!vis[pos])  continue;
			ans = p[pos];
			vis[pos] = false;
			break;
		}
		printf("%d ", ans);
	}
	puts(" ");
	return 0;
}


#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	//pair<int, int>p;
	vector< pair<long long int, pair<int, int> > >v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.second;
	}
	sort(v.begin(), v.end());
	int nb;
	cin >> nb;
	int a[nb];

	for (int i = 0; i < nb; i++) {
		cin >> a[i];
		for (int l = 0; l < n; l++) {
			if (v[l].second.first == a[i] || v[l].second.second == a[i]) {
				cout << v[l].first<<" ";
				v.erase(v.begin()+l);
				n--;
				break;
			}

			else if(l==n-1){
			cout << "-1"<<" ";
			}

		}

	}

}


#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	//pair<int, int>p;
	vector< pair<long long int, pair<int, int> > >v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.second;
	}
	sort(v.begin(), v.end());
	int nb;
	cin >> nb;
	int a[nb];

	for (int i = 0; i < nb; i++) {
		cin >> a[i];
		for (int l = 0; l < n; l++) {
			if (v[l].second.first == a[i] || v[l].second.second == a[i]) {
				cout << v[l].first<<" ";
				v[l].second.first = 0;
				v[l].second.second = 0;
				//v.erase(v.begin()+l);
				//n--;
				break;
			}

			else if(l==n-1){
			cout << "-1"<<" ";
			}

		}

	}

}


#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	//pair<int, int>p;
	vector< pair<long long int, pair<int, int> > >v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.second;
	}
	sort(v.begin(), v.end());
	int nb;
	cin >> nb;
	int a[nb];

	for (int i = 0; i < nb; i++) {
		cin >> a[i];
		for (int l = 0; l < n; l++) {
			if (v[l].second.first == a[i] || v[l].second.second == a[i]) {
				cout << v[l].first<<" ";
				v.erase(v.begin()+l);
				n--;
				break;
			}
			else if(l==n-1)
			cout << "-1"<<" ";

		}

	}

}


#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	//pair<int, int>p;
	vector< pair<long long int, pair<int, int> > >v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.first;
	}
	for (int i = 0; i < n; i++) {
		cin >> v[i].second.second;
	}
	sort(v.begin(), v.end());
	int nb;
	cin >> nb;
	int a[nb];

	for (int i = 1; i <= nb; i++) {
		cin >> a[i];
		for (int l = 0; l < n; l++) {
			if (v[l].second.first == a[i] || v[l].second.second == a[i]) {
				cout << v[l].first<<" ";
				//v[l].second.first = 0;
				//v[l].second.second = 0;
				v.erase(v.begin()+l);
				n--;
				break;
			}

			else if(l==n-1){
			cout << "-1"<<" ";
			}

		}
		if(n==0 &&i!=nb)
			cout<<"-1"<<" ";

	}
}


#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;

int main()
{
	int n, m;
	cin >> n;
	long long int p[200000], ans;
	int c[200000], cb[200000], a[200000];

	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> c[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> cb[i];
	}

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

	set<int>s1;
	set<int>s2;
	set<int>s3;
	for (int i = 0; i < n; i++) {
		if (c[i] == 1 || cb[i] == 1) {
			s1.insert(p[i]);
		}
		if (c[i] == 2 || cb[i] == 2) {
			s2.insert(p[i]);
		}
		if (c[i] == 3 || cb[i] == 3) {
			s3.insert(p[i]);
		}
	}

	for (int i = 0; i < m; i++) {
		if (a[i] == 1 && !s1.empty()) {
			ans = *s1.begin();
			cout << ans << " ";
			s1.erase(ans);
			s2.erase(ans);
			s3.erase(ans);

		}
		else if (a[i] == 2 && !s2.empty()) {
			ans = *s2.begin();
			cout << ans << " ";
			s1.erase(ans);
			s2.erase(ans);
			s3.erase(ans);

		}
		else if (a[i] == 3 && !s3.empty()) {
			ans = *s3.begin();
			cout << ans << " ";
			s1.erase(ans);
			s2.erase(ans);
			s3.erase(ans);

		}
		else cout << -1 << " ";
	}
	return 0;
}


#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;

int main()
{
	int n, m;
	cin >> n;
	long long int p[200000], ans;
	int c,cb,a;
	set<int>s[3];
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> c;
		s[c-1].insert(p[i]);
	}
	for (int i = 0; i < n; i++) {
		cin >> cb;
		s[cb-1].insert(p[i]);
	}

	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> a;
		if(!s[a-1].empty()){
			ans = *s[a-1].begin();
			cout << ans << " ";
			s[0].erase(ans);
			s[1].erase(ans);
			s[2].erase(ans);

		}
		else{
			cout<<"-1"<<" ";
		}

	}

	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main(){
	map<int,vector<int> > mp;
	set <int> se;
	vector<int> v;
	int n,i;
	cin>>n;
	for(i=0;i<n;i++){
		int t;cin>>t;
		v.push_back(t);
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		if(mp.find(t)!=mp.end())mp[t].push_back(v[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(v[i]);
		}
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		if(mp.find(t)!=mp.end())mp[t].push_back(v[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(v[i]);
		}
	}
	map<int,vector<int> > :: iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		sort(mp[it->first].begin(),mp[it->first].end());
	}
	int cus;cin>>cus;
	while(cus--){
		int t;cin>>t;
		while(1){
			if(mp[t].size()==0)	break;
			int l=se.size();
			se.insert(mp[t][0]);
			if(se.size()==l)mp[t].erase(mp[t].begin());
			else break;
			}
		if(mp[t].size()==0)cout<<-1<<" ";
		else {
			cout<<mp[t][0]<<" ";mp[t].erase(mp[t].begin());}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(){
	int n;
	map<int,int>pr_ind;
	map <int ,vector<int> > mp;
	vector<int> bg,fg;
	vector<int> pr;
	cin>>n;
	for(int i=0;i<n;i++){
		int t;cin>>t;
		pr.push_back(t);
		pr_ind[t]=i;
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		fg.push_back(t);
		if(mp.find(t)!=mp.end())mp[t].push_back(pr[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(pr[i]);
		}
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		bg.push_back(t);
		if(mp.find(t)!=mp.end())mp[t].push_back(pr[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(pr[i]);
		}
	}
	map<int,vector<int> > :: iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		sort(mp[it->first].begin(),mp[it->first].end());
	}
	int c;
	cin>>c;
	while(c--){
		int t;cin>>t;
		if(mp[t].size()==0)cout<<-1<<" ";
		else {
			int price=mp[t][0];
			cout<<price<<" ";
			//erase
			int tar;
			int ind=pr_ind[price];
			mp[t].erase(mp[t].begin());
			if(bg[ind]==t)tar=fg[ind];
			else tar=bg[ind];
			int i=0;
			for(auto x:mp[tar]){
				if(x==price)break;
				i++;
			}
			mp[tar].erase(mp[tar].begin()+i);
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
	int n;
	map<int,int>pr_ind;
	map <int ,vector<int> > mp;
	vector<int> bg,fg;
	vector<int> pr;
	cin>>n;
	for(int i=0;i<n;i++){
		int t;cin>>t;
		pr.push_back(t);
		pr_ind[t]=i;
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		fg.push_back(t);
		if(mp.find(t)!=mp.end())mp[t].push_back(pr[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(pr[i]);
		}
	}
	for(int i=0;i<n;i++){
		int t;cin>>t;
		bg.push_back(t);
		if(mp.find(t)!=mp.end())mp[t].push_back(pr[i]);
		else {
			std::vector<int> v2;
			mp[t]=v2;
			mp[t].push_back(pr[i]);
		}
	}
	map<int,vector<int> > :: iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		sort(mp[it->first].begin(),mp[it->first].end());
	}
	int c;
	cin>>c;
	while(c--){
		int t;cin>>t;
		if(mp[t].size()==0)cout<<-1<<" ";
		else {
			int price=mp[t][0];
			cout<<price<<" ";
			//erase
			int tar;
			int ind=pr_ind[price];
			mp[t].erase(mp[t].begin());
			if(bg[ind]==t)tar=fg[ind];
			else tar=bg[ind];
			int i=0;
			for(auto x:mp[tar]){
				if(x==price)break;
				i++;
			}
			mp[tar].erase(mp[tar].begin()+i);
		}
	}
	return 0;
}


//……………………………...#GO_pro
#include<string>
#include <iostream>
#include<math.h>
#define endl '\n'
#define inf INT_MAX
#define pb push_back
#define mp make_pair
#define der(c, x) ((c).find(x) != (c).end())
#define base 999983
#define baseinv 943912055
#define mod 1000000007
#define ff first
#define ss second
#define V vector
#define L list
#define P pair
#define M map
#define S set
#define UM unordered_map
#define MM multimap
#define UMM unordered_multimap
#define MS multiset
#define US unordered_set
#define UMS unordered_multiset
#define PQ priority_queue
#define Graph list<int>*
#define tr1(x)                cerr << #x << ": " << x << endl;
#define tr2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define tr3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define tr4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define tr5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define tr6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ldbl;

int main(){
ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,m;
	cin>>n>>m;
	ll x=n<<1;
	ll a=(ll)sqrt(m);
	a--;
	ll left=n;
	while(left>0){
		// cout<<left<<" "<<a<<endl;
		if(left-a==0){a++;
			break;}
		if(a>0)left=left-a+m;
		if(left>n)left=n;
		a++;
	}
	cout<<(a-1)<<endl;
return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main(){
	vector<int> pr,col_f,col_b,mrk;
	int n,c,t;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>t;pr.push_back(t);
		mrk.push_back(0);
	}
	for(int i=0;i<n;i++){
		cin>>t;col_f.push_back(t);
	}
	for(int i=0;i<n;i++){
		cin>>t;col_b.push_back(t);
	}
	cin>>c;
	while(c--){
		cin>>t;
		int l=-1;
		for(int i=0;i<col_b.size();i++){
			if(mrk[i]==1)continue;
			if(col_b[i]==t||col_f[i]==t){
				if(l==-1)l=i;
				else if(pr[l]>pr[i])l=i;
			}
		}
		if(l==-1)cout<<-1<<" ";
		else {
			cout<<pr[l]<<" ";
			mrk[l]=1;
			// pr.erase(pr.begin()+l);
			// col_b.erase(col_b.begin()+l);
			// col_f.erase(col_f.begin()+l);
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main(){
	int n;cin>>n;
	vector<int> pr;
	set<int> s[3];
	for(int i=0,t;i<n;i++){
		cin>>t;pr.push_back(t);
	}
	for(int i=0,t;i<2*n;i++){
		cin>>t;
		s[t-1].insert(pr[i%n]);
	}
	int c;cin>>c;
	while(c--){
		int t;cin>>t;
		set<int> :: iterator it;
		it=s[t-1].begin();
		if(s[t-1].size()==0)cout<<"-1 ";
		else {
			int d=*it;
			cout<<d<<" ";
			s[0].erase(d);
			s[1].erase(d);
			s[2].erase(d);
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main(){
	vector<int> pr,col_f,col_b;
	int n,c,t;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>t;pr.push_back(t);
	}
	for(int i=0;i<n;i++){
		cin>>t;col_f.push_back(t);
	}
	for(int i=0;i<n;i++){
		cin>>t;col_b.push_back(t);
	}
	cin>>c;
	while(c--){
		cin>>t;
		int l=-1;
		for(int i=0;i<col_b.size();i++){
			if(col_b[i]==t||col_f[i]==t){
				if(l==-1)l=i;
				else if(pr[l]>pr[i])l=i;
			}
		}
		if(l==-1)cout<<-1<<" ";
		else {
			cout<<pr[l]<<" ";
			pr.erase(pr.begin()+l);
			col_b.erase(col_b.begin()+l);
			col_f.erase(col_f.begin()+l);
		}
	}
	return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
      ios::sync_with_stdio(0);
	cin.tie(0);

  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set< int>s[4];
set< int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,i,j,m,c,p[200005],y,x;
  set <long long int> s[4];
  set<long long int>::iterator it;
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);

  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
  for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
 for(i=0;i<n;i++)
  {
      cin>>x;
       s[x].insert(p[i]);

  }
  cin>>m;
 for(i=0;i<m;i++)
  {
      cin>>x;

          it=s[x].begin();
          it++;
          if(it==s[x].end())
          {
              cout<<"-1 ";
          }
         else
            {
               cout << *it<<" ";
               y=*it;

            }
             s[1].erase(y);
             s[2].erase(y);
             s[3].erase(y);
  }

}


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

int main()
{
  long long int n,i,j,m,c,p[200005],y,x;
  set<long long int>s[4];
  set<long long int>::iterator it;
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);

  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        cout<<*it<<" ";
        y=*it;
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        cout<<*it<<" ";
        y=*it;
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
   int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set< int>s[4];
set< int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
 //     ios::sync_with_stdio(0);
//	cin.tie(0);

  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y=-2,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";

      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,x,y,z,p[200005];
  set<long long int>s[4];
  set<long long int>::iterator it;
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);

  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        cout<<*it<<" ";
        y=*it;
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,i,j,m,c,p[200005],y,x;
  set <long long int> s[4];
  set<long long int>::iterator it;
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);

  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
  for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
 for(i=0;i<n;i++)
  {
      cin>>x;
       s[x].insert(p[i]);

  }
  cin>>m;
 for(i=0;i<m;i++)
  {
      cin>>x;

          it=s[x].begin();
          it++;
          if(it==s[x].end())
            cout<<-1<<" ";
         else
            {
               std::cout << *it<<" ";
               y=*it;

            }
             s[1].erase(y);
             s[2].erase(y);
             s[3].erase(y);
  }

}


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

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<int>s[4];
set<int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }
  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";

      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
      }
  }

  return 0;
}


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

typedef long long int  ll;

int main()
{
  long long int n,i,j;
  cin>>n;
  long long int p[n],a,b,y;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
  set <long long int> s[4];
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  for(i=0;i<n;i++)
  {
      cin>>a;
      s[a].insert(p[i]);

  }
 for(i=0;i<n;i++)
  {
      cin>>b;
       s[b].insert(p[i]);

  }
  long long int m,c;

  set<long long int>::iterator it;
 for(i=0;i<m;i++)
  {
      cin>>c;

          it=s[c].begin();
          it++;
          if(it==s[c].end())
            cout<<-1<<" ";
         else
            {
               std::cout << *it<<" ";
               y=*it;

            }
             s[1].erase(y);
             s[2].erase(y);
             s[3].erase(y);
  }

}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005];
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  set<long long int>s[4];
  set<long long int>::iterator it;
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);

  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        cout<<*it<<" ";
        y=*it;
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
      ios::sync_with_stdio(0);
	cin.tie(0);

  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";
      }
      s[1].erase(y);
      s[2].erase(y);
      s[3].erase(y);
  }

  return 0;
}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
        y=*it;
        cout<<y<<" ";

      }
      s[1].erase(s[1].find(y));
      s[2].erase(s[2].find(y));
      s[3].erase(s[3].find(y));
  }

  return 0;
}


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

typedef long long int  ll;

int main()
{
  long long int n,i,j;
  cin>>n;
  long long int p[n],a,b,y;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
  set <long long int> s[4];
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  for(i=0;i<n;i++)
  {
      cin>>a;
      s[a].insert(p[i]);

  }
 for(i=0;i<n;i++)
  {
      cin>>b;
       s[b].insert(p[i]);

  }
  long long int m,c;
  ll c;
  set<long long int>::iterator it;
 for(i=0;i<m;i++)
  {
      cin>>c;

          it=s[c].begin();
          it++;
          if(it==s[c].end())
            cout<<-1<<" ";
         else
            {
               std::cout << *it<<" ";
               y=*it;

            }
             s[1].erase(y);
             s[2].erase(y);
             s[3].erase(y);
  }

}


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

int main()
{
  long long int n,m,i,j,k,ans=0,sum=0,ma=-1,mi=2000000000,x,y,z,p[200005]={0};
  string st;
  set<long long int>s[4];
set<long long int>::iterator it;

  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
    for(i=0;i<n;i++)
  {
      cin>>x;
      s[x].insert(p[i]);
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
      cin>>x;
      it=s[x].begin();
      it++;
      if(it==s[x].end())
      {
          cout<<"-1 ";
      }
      else
      {
            y=*it;
            cout<<y<<" ";
            it=s[1].find(y);
            if(it!=s[1].end())
            {
            s[1].erase(it);
            }
            it=s[2].find(y);
            if(it!=s[2].end())
            {
                s[2].erase(it);
            }
            it=s[3].find(y);
            if(it!=s[3].end())
            {
                s[3].erase(it);
            }
      }
  }

  return 0;
}


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

typedef long long int  ll;

int main()
{
  long long int n,i,j;
  cin>>n;
  long long int p[n],a,b,y;
  for(i=0;i<n;i++)
  {
      cin>>p[i];
  }
  set <long long int> s[4];
  s[1].insert(-1);
  s[2].insert(-1);
  s[3].insert(-1);
  for(i=0;i<n;i++)
  {
      cin>>a;
      s[a].insert(p[i]);

  }
 for(i=0;i<n;i++)
  {
      cin>>b;
       s[b].insert(p[i]);

  }
  long long int m,c;
  cin>>m;
  set<long long int>::iterator it;
 for(i=0;i<m;i++)
  {
      cin>>c;

          it=s[c].begin();
          it++;
          if(it==s[c].end())
            cout<<-1<<" ";
         else
            {
               std::cout << *it<<" ";
               y=*it;

            }
             s[1].erase(y);
             s[2].erase(y);
             s[3].erase(y);
  }

}


#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(shuzu,value) memset(shuzu,value,sizeof shuzu)
using namespace std;
const int M=200000+100;
struct T_shirt {
    int price;
    int a,b;
    bool is_s;
    T_shirt(int pp=0,int aa=0,int bb=0,bool is=false) {
        price=pp,a=aa,b=bb,is_s=is;
    }
} all[M];
int n;
bool cmp(T_shirt t1,T_shirt t2) {
    return t1.price<t2.price;
}
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>all[i].price;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].a;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].b;
    }
    sort(all,all+n,cmp);
    int m,a1;
    vector<int>ans;
    cin>>m;
    for(int i=0; i<m; i++) {
        cin>>a1;
        // cout<<m<<" "<<a1<<endl;
        if(i)cout<<" ";
        int j;
        for( j=0; j<n; j++) {
            if(!all[j].is_s) {
                if((all[j].a==a1||all[j].b==a1)) {
                    cout<<all[j].price;
                    all[j].is_s=true;
                    break;
                }
            }

        }
        if(j>=n)cout<<(-1);
    }
    cout<<endl;
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(shuzu,value) memset(shuzu,value,sizeof shuzu)
using namespace std;
const int M=200000+100;
struct T_shirt {
    int price;
    int a,b;
    bool is_s;
    T_shirt(int pp=0,int aa=0,int bb=0,bool is=false) {
        price=pp,a=aa,b=bb,is_s=is;
    }
} ;

int n;
bool cmp(T_shirt t1,T_shirt t2) {
    return t1.price<t2.price;
}
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin>>n;
    vector<T_shirt>all;
    for(int i=0; i<n; i++) {
            T_shirt t;
        cin>>t.price;
    all.push_back(t);
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].a;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].b;
    }
    sort(all.begin(),all.end(),cmp);
    int m,a1;
    vector<int>ans;
    cin>>m;
    for(int i=0; i<m; i++) {
        cin>>a1;
        // cout<<m<<" "<<a1<<endl;
        if(i)cout<<" ";
        int j;
        vector<T_shirt>::iterator it = all.begin();
        bool is_s=0;
        for( j=0,it; j<all.size();it++, j++) {

                if((all[j].a==a1||all[j].b==a1)) {
                    cout<<all[j].price;
                    is_s=true;
                    all.erase(it);
                    break;
                }

        }
        if(!is_s)cout<<(-1);
    }
    cout<<endl;
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(shuzu,value) memset(shuzu,value,sizeof shuzu)
using namespace std;
const int M=200000+1000;
struct node {
    int price;
    int id;
    node(int p,int id):price(p),id(id) {}
    friend bool operator <(node a,node b) {
        return a.price>b.price;

    }
};

bool vis[M];

priority_queue<node> q[3];
int pric[M];
int main() {
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>pric[i];
    int c;
    for(int i=1; i<=n; i++) {
       cin>>c;
        q[c-1].push(node(pric[i],i));
    }
    for(int i=1; i<=n; i++) {
        cin>>c;
        q[c-1].push(node(pric[i],i));
    }
    cin>>m;
    while(m--) {
       cin>>c;
        bool is_s=0;
        while(!q[c-1].empty()) {
            node cur = q[c-1].top();
            q[c-1].pop();
            if(!vis[cur.id]) {
                vis[cur.id]=is_s=1;
                cout<<cur.price<<" ";
                break;
            }

        }
        if(!is_s) {
            cout<<"-1 ";
        }

    }

    return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(shuzu,value) memset(shuzu,value,sizeof shuzu)
using namespace std;
const int M=200000+100;
struct T_shirt {
    int price;
    int a,b;
    bool is_s;
    T_shirt(int pp=0,int aa=0,int bb=0,bool is=false) {
        price=pp,a=aa,b=bb,is_s=is;
    }
} all[M];
int n;
bool cmp(T_shirt t1,T_shirt t2) {
    return t1.price<t2.price;
}
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>all[i].price;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].a;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].b;
    }
    sort(all,all+n,cmp);
    int m,a1;
    vector<int>ans;
    cin>>m;
    while(m--) {
        cin>>a1;
        cout<<m<<" "<<a1<<endl;
        int i;
        for( i=0; i<n; i++) {
            if(!all[i].is_s&&(all[i].a==a1||all[i].b==a1)) {
                ans.push_back(all[i].price);
                all[i].is_s=true;
                break;
            }
        }if(i>=n)ans.push_back(-1);
    }
    for(int i=0; i<ans.size(); i++) {
        if(i)cout<<" ";
        cout<<ans[i];
    }
    cout<<endl;
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(shuzu,value) memset(shuzu,value,sizeof shuzu)
using namespace std;
const int M=200000+100;
struct T_shirt {
    int price;
    int a,b;
    bool is_s;
    T_shirt(int pp=0,int aa=0,int bb=0,bool is=false) {
        price=pp,a=aa,b=bb,is_s=is;
    }
} all[M];
int n;
bool cmp(T_shirt t1,T_shirt t2) {
    return t1.price<t2.price;
}
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>all[i].price;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].a;
    }
    for(int i=0; i<n; i++) {
        cin>>all[i].b;
    }
    sort(all,all+n,cmp);
    int m,a1;
    vector<int>ans;
    cin>>m;
    while(m--) {
        cin>>a1;
       // cout<<m<<" "<<a1<<endl;
        int i;
        for( i=0; i<n; i++) {
            if(!all[i].is_s&&(all[i].a==a1||all[i].b==a1)) {
                ans.push_back(all[i].price);
                all[i].is_s=true;
                break;
            }
        }if(i>=n)ans.push_back(-1);
    }
    for(int i=0; i<ans.size(); i++) {
        if(i)cout<<" ";
        cout<<ans[i];
    }
    cout<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
	int t;
	cin>>t;
	vector<int> shirt;
	vector<int> fc;
	vector<int> bc;
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		shirt.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		fc.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		bc.push_back(k);
	}
	int b;
	cin>>b;
	for(int i=0;i<b;i++){
		int c;
		cin>>c;
		int lp=1000000001;
		int place=0;
		if(shirt.size()==0){
			cout<<-1<<" ";
			continue;
		}
		//cout<<"bob";
		for(int j=0;j<shirt.size();j++){
			if(fc[j]==c or bc[j]==c){
				if(lp>shirt[j]){
					lp=shirt[j];
					place=j;
				}
			}
		}
		if(lp==1000000001){
			cout<<-1<<" ";
		}
		else{
			cout<<lp<<" ";
			shirt.erase(shirt.begin()+place);
			fc.erase(fc.begin()+place);
			bc.erase(bc.begin()+place);
		}
	}
}


#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q[4];
bool used[200001];
int p[200001],a[200001],b[200001];
int main()
{
    int n,m,c,pp;
    pair<int,int> t;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&p[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    for(int i=1;i<=n;i++)
    {
        q[a[i]].push(make_pair(p[i],i));
        q[b[i]].push(make_pair(p[i],i));
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d",&c);
        pp=-1;
        while(!q[c].empty())
        {
            t=q[c].top();
            q[c].pop();
            if(used[t.second])
                continue;
            else
            {
                pp=t.first;
                used[t.second]=true;
                break;
            }
        }
        printf("%d ",pp);
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	vector<int> shirt;
	vector<int> fc;
	vector<int> bc;
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		shirt.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		fc.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		bc.push_back(k);
	}
	int b;
	cin>>b;
	for(int i=0;i<b;i++){
		int c;
		cin>>c;
		int lp=1000000001;
		int place=0;
		if(shirt.size()==0){
			cout<<-1;
			continue;
		}
		//cout<<"bob";
		for(int j=0;j<shirt.size();j++){
			if(fc[j]==c or bc[j]==c){
				if(lp>shirt[j]){
					lp=shirt[j];
					place=j;
				}
			}
		}
		if(lp==1000000001){
			cout<<-1<<" ";
		}
		else{
			cout<<lp<<" ";
			shirt.erase(shirt.begin()+place);
			fc.erase(fc.begin()+place);
			bc.erase(bc.begin()+place);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	vector<int> shirt;
	vector<int> fc;
	vector<int> bc;
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		shirt.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		fc.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		bc.push_back(k);
	}
	int b;
	cin>>b;
	for(int i=0;i<b;i++){
		int c;
		cin>>c;
		int lp=1000000001;
		int place=0;
		if(shirt.size()==0){
			cout<<-1;
			continue;
		}
		//cout<<"bob";
		for(int j=0;j<shirt.size();j++){
			if(fc[j]==c or bc[j]==c){
				if(lp>shirt[j]){
					lp=shirt[j];
					place=j;
				}
			}
		}
		if(lp==1000000001){
			cout<<-1;
		}
		else{
			cout<<lp<<" ";
			shirt.erase(shirt.begin()+place);
			fc.erase(fc.begin()+place);
			bc.erase(bc.begin()+place);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
	int t;
	cin>>t;
	vector<int> shirt;
	vector<int> fc;
	vector<int> bc;
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		shirt.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		fc.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		bc.push_back(k);
	}
	int b;
	cin>>b;
	for(int i=0;i<b;i++){
		int c;
		cin>>c;
		int lp=1000000001;
		int place=0;
		if(shirt.size()==0){
			cout<<-1;
			continue;
		}
		//cout<<"bob";
		for(int j=0;j<shirt.size();j++){
			if(fc[j]==c or bc[j]==c){
				if(lp>shirt[j]){
					lp=shirt[j];
					place=j;
				}
			}
		}
		if(lp==1000000001){
			cout<<-1<<" ";
		}
		else{
			cout<<lp<<" ";
			shirt.erase(shirt.begin()+place);
			fc.erase(fc.begin()+place);
			bc.erase(bc.begin()+place);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	vector<int> shirt;
	vector<int> fc;
	vector<int> bc;
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		shirt.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		fc.push_back(k);
	}
	for(int i=0;i<t;i++){
		int k;
		cin>>k;
		bc.push_back(k);
	}
	int b;
	cin>>b;
	for(int i=0;i<b;i++){
		int c;
		cin>>c;
		int lp=1000000001;
		int place=0;
		if(shirt.size()==0){
			cout<<-1<<" ";
			continue;
		}
		//cout<<"bob";
		for(int j=0;j<shirt.size();j++){
			if(fc[j]==c or bc[j]==c){
				if(lp>shirt[j]){
					lp=shirt[j];
					place=j;
				}
			}
		}
		if(lp==1000000001){
			cout<<-1<<" ";
		}
		else{
			cout<<lp<<" ";
			shirt.erase(shirt.begin()+place);
			fc.erase(fc.begin()+place);
			bc.erase(bc.begin()+place);
		}
	}
}


/*===================================================================================================================

                                                   // BY - ABHISHEK MALIK  //

=====================================================================================================================*/
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e9
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
//vector<int >v[100005];
//set<string >stA,stB,stC,stD;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >a,b,c,d;
set<ll >st[5];
//char arr[51][51];
ll price[200005],bcolor[200005],fcolor[200005];
//ll dp[1005][3];
//bool vis[1000005];
//map<string ,int >mp,mp2;
//map<string ,int >::iterator it,it2;
set<ll >::iterator it,its2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int main()
{
   ll n,m,k;
   inpl(n);
   for(ll i=1;i<=n;i++)
     inpl(price[i]);

   for(ll i=1;i<=n;i++)
     inpl(fcolor[i]);

   for(ll i=1;i<=n;i++)
    inpl(bcolor[i]);

   for(ll i=1;i<=n;i++)
   {
       st[fcolor[i]].insert(price[i]);
       st[bcolor[i]].insert(price[i]);
   }
   inpl(m);
   for(ll i=1;i<=m;i++)
   {
       inpl(k);
       if(st[k].size()==0)
       {
           cout<<"-1 ";
       }
       else
       {
           it=st[k].begin();
           cout<<*it<<" ";
            for(int j=1;j<=3;j++)
            st[j].erase(*it);
       }
   }
}


/*===================================================================================================================

                                                   // BY - ABHISHEK MALIK  //

=====================================================================================================================*/
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e9
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
//vector<int >v[100005];
//set<string >stA,stB,stC,stD;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >a,b,c,d;
set<ll >st[200005];
//char arr[51][51];
ll price[200005],bcolor[200005],fcolor[200005],buyer[200005];
//ll dp[1005][3];
//bool vis[1000005];
//map<string ,int >mp,mp2;
//map<string ,int >::iterator it,it2;
set<ll >::iterator it,its2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int main()
{
   ll n,m;
   inpl(n);
   for(int i=1;i<=n;i++)
     inpl(price[i]);

   for(int i=1;i<=n;i++)
     inpl(fcolor[i]);

   for(int i=1;i<=n;i++)
    inpl(bcolor[i]);

    inpl(m);
   for(int i=1;i<=m;i++)
     inpl(buyer[i]);

   for(int i=1;i<=n;i++)
   {
       st[fcolor[i]].insert(price[i]);
       st[bcolor[i]].insert(price[i]);
   }
   for(int i=1;i<=m;i++)
   {
       if(st[buyer[i]].size()==0)
       {
           cout<<"-1 ";
       }
       else
       {
           it=st[buyer[i]].begin();
           cout<<*it<<" ";
           if(st[1].find(*it)!=st[1].end())
            st[1].erase(*it);
           if(st[2].find(*it)!=st[2].end())
            st[2].erase(*it);
           if(st[3].find(*it)!=st[3].end())
            st[3].erase(*it);
       }
   }
}


/*===================================================================================================================

                                                   // BY - ABHISHEK MALIK  //

=====================================================================================================================*/
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e9
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
//vector<int >v[100005];
//set<string >stA,stB,stC,stD;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >a,b,c,d;
set<ll >st[5];
//char arr[51][51];
ll price[200005],bcolor[200005],fcolor[200005];
//ll dp[1005][3];
//bool vis[1000005];
//map<string ,int >mp,mp2;
//map<string ,int >::iterator it,it2;
set<ll >::iterator it,its2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int main()
{
   ll n,m,k,t;
   inpl(n);
   for(ll i=1;i<=n;i++)
     inpl(price[i]);

   for(ll i=1;i<=n;i++)
     inpl(fcolor[i]);

   for(ll i=1;i<=n;i++)
    inpl(bcolor[i]);

   for(ll i=1;i<=n;i++)
   {
       st[fcolor[i]].insert(price[i]);
       st[bcolor[i]].insert(price[i]);
   }
    inpl(m);
   for(ll i=1;i<=m;i++)
   {
       inpl(k);
       if(st[k].size()==0)
       {
           cout<<"-1 ";
       }
       else
       {
           t=*st[k].begin();
           cout<<t<<" ";
           for(int j=1;j<=3;j++)
            st[j].erase(t);
       }
   }
}


/*===================================================================================================================

                                                   // BY - ABHISHEK MALIK  //

=====================================================================================================================*/
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e9
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
//vector<int >v[100005];
//set<string >stA,stB,stC,stD;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >a,b,c,d;
set<ll >st[5];
//char arr[51][51];
ll price[200005],bcolor[200005],fcolor[200005];
//ll dp[1005][3];
//bool vis[1000005];
//map<string ,int >mp,mp2;
//map<string ,int >::iterator it,it2;
set<ll >::iterator it,its2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int main()
{
   ll n,m,k;
   inpl(n);
   for(ll i=1;i<=n;i++)
     inpl(price[i]);

   for(ll i=1;i<=n;i++)
     inpl(fcolor[i]);

   for(ll i=1;i<=n;i++)
    inpl(bcolor[i]);

    inpl(m);
   for(ll i=1;i<=n;i++)
   {
       st[fcolor[i]].insert(price[i]);
       st[bcolor[i]].insert(price[i]);
   }
   for(ll i=1;i<=m;i++)
   {
       inpl(k);
       if(st[k].size()==0)
       {
           cout<<"-1 ";
       }
       else
       {
           it=st[k].begin();
           cout<<*it<<" ";
            for(int j=1;j<=3;j++)
            st[j].erase(*it);
       }
   }
}