import std;
void main () {
	int n;
	while (readf !(" %s") (n) > 0) {
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int [int] v;
		foreach (ref c; a) {
			v[c] += 1;
		}
		auto s = a.sum (0L);
		int q;
		readf !(" %s") (q);
		foreach (j; 0..q) {
			int b, c;
			readf !(" %s %s") (b, c);
			if (b in v) {
				s += (c - b) * 1L * v[b];
				v[c] += v[b];
				v.remove (b);
			}
			writeln (s);
		}
	}
}
