#include <bits/stdc++.h>
using namespace std;
template <typename T>
constexpr string_view gtype() {
  string_view s = __PRETTY_FUNCTION__;
  return string_view(s.data() + 45, s.find(';', 45) - 45);
}
const int MAXN = 1e3 + 9;
int cnt[26] = {0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  int k;
  cin >> k;
  map<char, char> fob;
  for (int i = 0; i < k; i++) {
    char x, y;
    cin >> x >> y;
    fob[x] = y;
    fob[y] = x;
  }
  int ans = 0;
  for (int i = 0; i < s.size();) {
    int cnt = 0;
    char prev = s[i];
    while ((s[i] == prev) and (i < n)) {
      cnt++, i++;
    };
    ;
    ;
    ;
    if ((i < n) and (s[i] == fob[prev])) {
      int cnt2 = 0;
      char prev2 = s[i];
      while (((s[i] == prev2) or (s[i] == prev)) and (i < n)) {
        if (s[i] == prev)
          cnt++;
        else
          cnt2++;
        i++;
      };
      ans += min(cnt, cnt2);
    }
  }
  cout << ans << endl;
}
