#include <bits/stdc++.h>
using namespace std;
char s[100005];
int n, m, dp[30], FS, SC, bn[30];
char readch() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
int main() {
  int i, j, k;
  scanf("%s", s);
  n = strlen(s);
  FS = 28;
  SC = 1;
  scanf("%d", &m);
  memset(bn, -1, sizeof(bn));
  for (i = 0; i < m; ++i) {
    int a = readch() - 'a', b = readch() - 'a';
    bn[a] = b;
    bn[b] = a;
  }
  for (i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    dp[c] = max(dp[c], (FS == bn[c] ? dp[SC] : dp[FS]) + 1);
    if (c == FS) {
    } else if (c == SC) {
      if (dp[c] > dp[FS]) swap(SC, FS);
    } else if (dp[c] > dp[FS]) {
      SC = FS;
      FS = c;
    } else if (dp[c] > dp[SC]) {
      SC = c;
    }
  }
  printf("%d\n", n - dp[FS]);
  return 0;
}
