#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool debug = false;
vector<pair<string, int>> cur;
vector<int> b, b1;
string s;
int myp, mys;
bool cmp(pair<string, int> a, pair<string, int> b) {
  if (a.second == b.second) {
    return a.first < b.first;
  }
  return a.second > b.second;
}
char cs[30];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s%d", cs, &k);
    cur.push_back(make_pair(cs, k));
  }
  scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    if (i < m)
      scanf("%d", &k);
    else
      k = 0;
    b.push_back(k);
  }
  cin >> s;
  sort(cur.begin(), cur.end(), cmp);
  sort(b.begin(), b.end());
  b1 = b;
  for (int i = 0; i < n; i++) {
    if (cur[i].first == s) {
      myp = i;
      mys = cur[i].second;
      break;
    }
  }
  mys += b.back();
  b.pop_back();
  for (k = myp - 1; k >= 0; k--) {
    int cha = mys - cur[k].second - (s > cur[k].first);
    vector<int>::iterator it = upper_bound(b.begin(), b.end(), cha);
    if (it == b.begin()) break;
    it--;
    b.erase(it);
  }
  printf("%d ", k + 2);
  b = b1;
  mys -= b.back();
  mys += *b.begin();
  b.erase(b.begin());
  for (k = myp + 1; k < n; k++) {
    int cha = mys - cur[k].second + (s < cur[k].first);
    vector<int>::iterator it = lower_bound(b.begin(), b.end(), cha);
    if (it == b.end()) break;
    b.erase(it);
  }
  printf("%d\n", k);
  return 0;
}
