import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;
    
    long ans = arr.sum(0L);
    
    int[int] cnt;
    foreach (e; arr) { cnt[e] += 1; }
    
    int q;
    readf("%s", &q);
    readln;
    
    while (q--) {
        int b, c;
        readf("%s %s", &b, &c);
        readln;
        
        int diff = c - b;
        int elems = cnt.get(b, 0);
        
        long change = diff.to!long * elems;
        
        cnt[b] = 0;
        cnt[c] += elems;
        
        ans += change;
        
        ans.writeln;
    }
}