import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
    readln;
    auto a = new long[10001];
    foreach (e; readln.chomp.split.to!(int[]))
        a[e] += 1;
    long ret;
    foreach (i, e; a)
        ret += i * e;
    immutable q = readln.chomp.to!size_t;
    foreach (_; 0..q)
    {
        auto buf = readln.chomp.split.to!(int[]);
        ret += a[buf[0]] * (buf[1] - buf[0]);
        a[buf[1]] += a[buf[0]];
        a[buf[0]] = 0;
        ret.writeln;
    }
}
