#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int p[N];
int main() {
  int n;
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    sum += x;
    p[x]++;
  }
  int T;
  scanf("%d", &T);
  while (T--) {
    int b, c;
    scanf("%d%d", &b, &c);
    cout << b << " " << c << " " << p[b] << endl;
    sum -= b * p[b];
    sum += c * p[b];
    p[c] += p[b];
    p[b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}
