#include <bits/stdc++.h>
int main(void) {
  long N;
  scanf("%ld", &N);
  long a[100005];
  for (long i = 0; i < 100005; i++) a[i] = 0;
  long x;
  for (long i = 0; i < N; i++) {
    scanf("%ld", &x);
    a[x]++;
  }
  long Q;
  scanf("%ld", &Q);
  long b, c, sum;
  for (long i = 0; i < Q; i++) {
    scanf("%ld %ld", &b, &c);
    a[c] += a[b];
    a[b] = 0;
    sum = 0;
    for (long j = 0; j < 100005; j++) {
      if (a[j] == 0) continue;
      sum += a[j] * j;
    }
    printf("%ld\n", sum);
  }
  return 0;
}
