#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  map<int, int> nums;
  int a[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    nums[a[i]]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int num = 0;
    int B, C;
    int sum = 0;
    cin >> B >> C;
    num = nums[B];
    nums[B] = 0;
    nums[C] += num;
    for (auto a : nums) {
      sum += a.first * a.second;
    }
    cout << sum << endl;
  }
  for (auto a : nums) {
    cout << a.first << " " << a.second << endl;
  }
}
