#include <bits/stdc++.h>
using namespace std;
vector<char> V, L;
vector<pair<char, long long>> B;
vector<string> Ans;
map<int, int> M;
set<long long> S;
set<char>::iterator it;
long long n, m, a, b, c, d, e, f, x, y, z, t, l, r, ans, cnt, score;
string s, winner, temp, p;
long long arr[1001];
bool xy = 0;
char bd;
float ans1;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a, V.push_back(a);
  cin >> m;
  for (int i = 0; i < m; i++) {
    cnt = 0;
    cin >> x >> y;
    for (int j = 0; j < V.size(); j++) {
      if (V[j] == x) V[j] = y;
      cnt += V[j];
    }
    cout << cnt << endl;
  }
}
