#include <bits/stdc++.h>
using namespace std;
long long int n;
char a, b;
string s;
map<char, char> mp;
long long int dp[(long long int)(1e5) + 5][30];
long long int rec(long long int wai, long long int last) {
  if (wai == -1) return 0;
  if (dp[wai][last] != -1) return dp[wai][last];
  if (s[wai] == mp[(last - 1 + 'a')]) {
    return dp[wai][last] = (rec(wai - 1, last) + 1);
  }
  return dp[wai][last] =
             min(rec(wai - 1, last) + 1, rec(wai - 1, s[wai] + 1 - 'a'));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> n;
  memset(dp, -1, sizeof(dp));
  for (long long int i = 0; i < n; i++) {
    cin >> a >> b;
    mp[a] = b;
    mp[b] = a;
  }
  cout << rec(s.length() - 1, 0) << '\n';
}
