import std;

alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
    auto n = lread();
    auto a = aryread();
    auto q = lread();
    long sum_a = sum(a);
    auto num = new long[](10 ^^ 6);
    foreach (i; 0 .. n)
    {
        num[a[i]] += 1;
    }
    // writeln(num);
    foreach (_; 0 .. q)
    {
        long b, c;
        scan(b, c);
        // writeln(num);
        sum_a += (c - b) * num[b];
        num[c] += num[b];
        num[b] = 0;
        // writeln("sum_a:", sum_a);
        writeln(sum_a);

    }

}

void scan(L...)(ref L A)
{
    auto l = readln.split;
    foreach (i, T; L)
    {
        A[i] = l[i].to!T;
    }
}

void arywrite(T)(T a)
{
    a.map!text.join(' ').writeln;
}
