#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T t) {
  return t < 0 ? -t : t;
}
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
char s[100009];
int p[256];
int main() {
  int i, j, n, k;
  char c1, c2;
  scanf("%s", s);
  n = strlen(s);
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf(" %c %c", &c1, &c2);
    p[c1] = c2;
    p[c2] = c1;
  }
  int tot = 0;
  for (i = 0; i < n;) {
    if (!p[s[i]]) {
      i++;
      continue;
    }
    j = i + 1;
    int c[2] = {1, 0};
    while (j < n && (s[j] == s[i] || s[j] == p[s[i]])) c[s[j++] != s[i]]++;
    tot += min(c[0], c[1]);
    i = j;
  }
  printf("%d\n", tot);
}
