import std.conv;
import std.string;
import std.algorithm;
import std.array;
import std.stdio;

void main() {
	auto n = readln.chomp.to!int;
	auto a = readln.chomp.split(' ').map!(to!long).array;

	assert(n == a.length);

	auto s = sum(a);

	auto counts = new int[100000];

	foreach (e;a) {
		++counts[e];
	}

	auto q = readln.chomp.to!int;

	foreach (i;0..q) {
		int b, c;
		readf(" %s %s", &b, &c);
		assert(b != c);

		s += (cast(long)c - b) * counts[b];
		writeln(s);
		counts[c] += counts[b];
		counts[b] = 0;
	}
}
