#include <bits/stdc++.h>
int num[10000000];
int main() {
  int n;
  scanf("%d", &n);
  int max = INT_MIN;
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    num[temp]++;
    max = std::max(max, temp);
  }
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    int b, c;
    scanf("%d %d", &b, &c);
    num[c] += num[b];
    num[b] = 0;
    max = std::max(max, c);
    unsigned long long count = 0;
    for (int i = 1; i <= max; i++) {
      count += i * num[i];
    }
    printf("%llu\n", count);
  }
}
