code
#include<bits/stdc++.h>
#include<bits/extc++.h>
//#define int long long
#define lowbit(x) (x&-x)
#define ls(x) st[(x)<<1]
#define rs(x) st[((x)<<1)+1]
using namespace std;
//using namespace __gnu_pbds;
//tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tr;
using ll=long long;
using ull=unsigned long long;
using i128=__int128_t;
using u128=__uint128_t;
//inline int ask_rk(int x)
//{
// return tr.order_of_key(x)+1;
//}
//inline int ask_val(int x)
//{
// return tr.find_by_order(x-1);
//}
//inline int ask_pre(int x)
//{
// return *(--tr.lower_bound(x));
//}
//inline int ask_nxt(int x)
//{
// return *(tr.upper_bound(x));
//}
//inline int power(int x,int y)
//{
// int res=1;
// while (y)
// {
// if (y&1)
// res=res*x%p;
// x=x*x%p;
// y>>=1;
// }
// return res;
//}
int n,m,a[100005];
map<string,int> mp;
multiset<int> st;
string name;
signed main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for (int i=1;i<=n;i++)
{
string x;
int t;
cin>>x>>t;
mp[x]=t;
}
cin>>m;
for (int i=1;i<=m;i++)
{
cin>>a[i];
st.insert(a[i]);
}
for (int i=1;i<=n-m;i++)
st.insert(0);
cin>>name;
mp[name]+=*(--st.end());
int txt=*(--st.end());
st.erase(--st.end());
int ans=1;
for (auto i:mp)
if (i.first!=name)
{
if (i.second+*st.begin()>mp[name]||(i.second+*st.begin()==mp[name]&&i.first<name))
ans++,st.erase(--st.end());
else if (i.first<name)
st.erase(--st.lower_bound(mp[name]-i.second));
else
st.erase(st.lower_bound(mp[name]-i.second));
}
mp[name]-=txt;
cout<<ans<<' ';
ans=n;
st.clear();
for (int i=1;i<=m;i++)
st.insert(a[i]);
for (int i=1;i<=n-m+1;i++)
st.insert(0);
mp[name]+=*st.begin();
st.erase(st.begin());
for (auto i:mp)
if (i.first!=name)
{
if (i.second+*(--st.end())<mp[name]||(i.second+*(--st.end())==mp[name]&&i.first>name))
ans--,st.erase(st.begin());
else if (i.first<name)
st.erase(st.lower_bound(mp[name]-i.second));
else
st.erase(st.upper_bound(mp[name]-i.second));
}
cout<<ans;
return 0;
}