#include <bits/stdc++.h>
using namespace std;
char buf[100020];
char f[30][10];
int main() {
  while (scanf("%s", buf) != EOF) {
    string s(buf);
    int K;
    scanf("%d", &K);
    for (int i = 0; i < (int)(K); ++i) scanf("%s", f[i]);
    map<char, int> to;
    for (int i = 0; i < (int)(K); ++i) {
      to[f[i][0]] = i + 1;
      to[f[i][1]] = i + 1;
    }
    int N = (int)((s).size());
    int prev = -1, res = 0;
    map<char, int> acc;
    for (int i = 0; i < (int)(N + 1); ++i) {
      if (prev >= -1 && (to[s[i]] != prev || i == N)) {
        if ((int)((acc).size()) == 2) {
          int temp = 1 << 29;
          for (__typeof((acc).begin()) it = (acc).begin(); it != (acc).end();
               ++it)
            temp = min(temp, it->second);
          res += temp;
        }
        acc.clear();
      }
      if (i == N) break;
      if (to[s[i]] >= 1) {
        acc[s[i]]++;
        prev = to[s[i]];
      } else {
        prev = -1;
      }
    }
    printf("%d\n", res);
  }
}
