#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string a = "";
  cin >> n;
  for (int i = 0; i < (int)(n); i++) {
    string tmp;
    cin >> tmp;
    a += tmp;
  }
  int q;
  cin >> q;
  for (int i = 0; i < (int)(q); i++) {
    int place, ans = 0;
    char b, c;
    cin >> b >> c;
    place = a.find(b);
    while (place >= 0) {
      a[place] = c;
      place = a.find(b);
    }
    for (int i = 0; i < (int)(a.length()); i++) ans += a[i] - '0';
    cout << ans << endl;
  }
  return 0;
}
