function sum(a) {
  return a.split(',').reduce((s, v) => {
    return s + Number(v);
  }, 0)
}

function Main(input) {
  input = input.split('\n');
  const n = +input[0];
  let a = input[1].split(' ').join(',');
  const q = +input[2];
  const bc = input.slice(3);
  for (let i = 0; i < q; i++) {
    const [b, c] = bc[i].split(' ');
    a = a.replace(new RegExp(b, 'g'), c);
    console.log(sum(a));
  }
}
  
Main(require('fs').readFileSync('/dev/stdin', 'utf8'));