import std;
void main(){
  int n = readln.chomp.to!int;
  long[] as = readln.chomp.split.to!(long[]);
  int[long] ac;
  long ans;
  foreach(a; as){
    if(a !in ac) ac[a] = 0;
    ac[a] += 1;
    ans += a;
  }
  
  
  int q = readln.chomp.to!int;
  foreach(_; 0 .. q){
    long[] bc = readln.chomp.split.to!(long[]);
    long b = bc[0], c = bc[1];
    if(b !in ac) ac[b] = 0;
    if(c !in ac) ac[c] = 0;
    ac[c] += ac[b];
    ans += (c - b) * ac[b];
    ac[b] = 0;
    ans.writeln;
  }
}