#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using pii = pair<int, int>;
using pll = pair<LL, LL>;
using pld = pair<LD, LD>;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  int k;
  cin >> s >> k;
  int len = s.size();
  char bro['z' + 1]{};
  while (k--) {
    string forbidden_pair;
    cin >> forbidden_pair;
    bro[forbidden_pair[0]] = forbidden_pair[1];
    bro[forbidden_pair[1]] = forbidden_pair[0];
  }
  int res = 0, cur['z' + 1]{};
  cur[s.front()] = 1;
  char chr1 = s.front(), chr2 = bro[chr1];
  for (int i = 0; i < len - 1; ++i) {
    if (s[i + 1] == s[i] || s[i + 1] == bro[s[i]]) {
      ++cur[s[i + 1]];
    } else {
      res += min(cur[chr1], cur[chr2]);
      cur[chr1] = cur[chr2] = 0;
      cur[chr1 = s[i + 1]] = 1, chr2 = bro[chr1];
    }
  }
  res += min(cur[chr1], cur[chr2]);
  cout << res;
}
