#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int q;
  cin >> q;
  vector<pair<int, int>> bc(q);
  for (int i = 0; i < q; ++i) cin >> bc[i].first >> bc[i].second;
  sort(a.begin(), a.end());
  sort(bc.begin(), bc.end());
  int j = 0;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cout << i << " " << a[i] << " " << j << endl;
    if (a[i] == bc[j].first) a[i] = bc[j].second;
    if (a[i] == bc[j + 1].first && j != q - 1) {
      sum = 0;
      for (int i = 0; i < n; ++i) {
        sum += a[i];
        cout << a[i] << " ";
      }
      cout << sum << endl;
      a[i] = bc[j + 1].second;
      j++;
    }
  }
  sum = 0;
  for (int i = 0; i < n; ++i) sum += a[i];
  cout << sum << endl;
}
