#include <bits/stdc++.h>
using namespace std;
int n, memo[100005]['z' + 5];
string inp;
bool adj['z' + 5]['z' + 5];
int call(int ind, char x) {
  int ret;
  if (ind == n) return 0;
  if (memo[ind][x] != -1) return memo[ind][x];
  if (adj[inp[ind]][x] == 0)
    ret = min(call(ind + 1, x) + 1, call(ind + 1, inp[ind]));
  else
    ret = call(ind + 1, x) + 1;
  return memo[ind][x] = ret;
}
int main() {
  int i, ans, no;
  char x, y;
  memset(memo, -1, sizeof memo);
  cin >> inp >> no;
  n = inp.size();
  for (i = 0; i < no; i++) {
    cin >> x >> y;
    adj[x][y] = 1;
    adj[y][x] = 1;
  }
  ans = call(0, 'a' - 1);
  cout << ans << endl;
  return 0;
}
