/* C++14 (Clang 3.8.0) for AtCoder */

#include <iostream>
#include <vector>
#include <string>
// #include <cmath>
// #include <math.h>
#include <map>
// #include <algorithm>

namespace AtCoder {
	using Str = std::vector<std::string>;
	using Num = std::vector<int>;
	using BigNum = std::vector<double>;

	class Ans {
	public:
		void showResult(void);
		void increment(void);
		void increment(double);
		void decrement(void);
		void decrement(double);
		double getResult(void);
	private:
		double result;
	};
	void Ans::showResult(void) {
		printf("%.0lf\n", this->result);
	}
	void AtCoder::Ans::increment(void) {
		this->result++;
	}
	void AtCoder::Ans::increment(double value) {
		this->result += value;
	}
	void AtCoder::Ans::decrement(void) {
		this->result--;
	}
	void AtCoder::Ans::decrement(double value) {
		this->result -= value;
	}
	double AtCoder::Ans::getResult(void) {
		return this->result;
	}

	using DefMap = std::map<std::string, std::string>;

	class Map {
	public:
		void set(std::string, std::string);
		std::string get(std::string);
		void remove(std::string);
	private:
		DefMap m;
	};
	void Map::set(std::string key, std::string value) {
		try {
			this->m.at(key);
			this->remove(key);
			this->m.insert(std::make_pair(key, value));
		} catch (std::exception & e) {
			this->m.insert(std::make_pair(key, value));
		}
	}
	std::string Map::get(std::string key) {
		try {
			return this->m.at(key);
		} catch (std::exception & e) {
			return e.what();	// "invalid map<K, T> key"
		}
	}
	void Map::remove(std::string key) {
		this->m.erase(key);
	}

	void input(Str* str) {
		str->clear();
		std::string buf = "", row = "";
		std::getline(std::cin, buf);
		for (unsigned int i = 0; i < buf.size(); i++) {
			if (' ' == buf[i]) {
				str->push_back(row);
				row = "";
			} else {
				row += buf[i];
			}
		}
		str->push_back(row);
	}
	void input(Num* num) {
		num->clear();
		std::string buf = "", row = "";
		std::getline(std::cin, buf);
		for (unsigned int i = 0; i < buf.size(); i++) {
			if (' ' == buf[i]) {
				num->push_back(std::atoi(row.c_str()));
				row = "";
			} else {
				row += buf[i];
			}
		}
		num->push_back(std::atoi(row.c_str()));
	}
	void input(BigNum* num) {
		num->clear();
		std::string buf = "", row = "";
		std::getline(std::cin, buf);
		for (unsigned int i = 0; i < buf.size(); i++) {
			if (' ' == buf[i]) {
				num->push_back(std::atof(row.c_str()));
				row = "";
			} else {
				row += buf[i];
			}
		}
		num->push_back(std::atof(row.c_str()));
	}
	void exp(std::string* str,  Str* newStr) {
		std::string pre = *str;
		newStr->clear();
		for (unsigned int i = 0; i < pre.length(); i++) {
			newStr->push_back(pre.substr(i, 1));
		}
	}
}

int main(void) {

	AtCoder::Num n;
	AtCoder::Str s, st;
	AtCoder::Map* map = new AtCoder::Map();
	AtCoder::input(&n);

	const int max = n[0];

	for (int i = 0; i < max; i++) {
		AtCoder::input(&s);
		map->set(s[0], s[1]);
		st.push_back(s[0]);
	}

	AtCoder::input(&s);

	const std::string last = s[0];

	AtCoder::Ans* ans = new AtCoder::Ans();

	bool incrementf = false;
		
	for (int i = 0; i < max; i++) {
		const std::string title = st[i];
		if (incrementf) {
			ans->increment(std::atof(map->get(st[i]).c_str()));
		}
		if (last == title) {
			incrementf = true;
		}
	}

	ans->showResult();

	delete(ans);
	delete(map);

	return 0;
}

Measured memory_bytes: 888