#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 100;
string s;
pair<char, int> a[maxN];
map<char, char> ban;
int main() {
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    char x, y;
    cin >> x >> y;
    ban[x] = y;
    ban[y] = x;
  }
  int i, n, head;
  for (i = 0, head = 0; head < s.size(); i++) {
    int cnt = 0;
    while (s[head + cnt] == s[head]) cnt++;
    a[i] = make_pair(s[head], cnt);
    head += cnt;
  }
  n = i;
  int res = 0;
  for (int i = 0, head = 0; i < n;) {
    head = i;
    int bad = 0, good = 0;
    for (; i < n; i++) {
      if (ban[a[head].first] == a[i].first)
        bad += a[i].second;
      else if (a[head].first == a[i].first)
        good += a[i].second;
      else
        break;
    }
    res += min(bad, good);
  }
  cout << res << endl;
  return 0;
}
