#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
  return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
  return y < x ? (x = y, 1) : 0;
}
const int maxn = 1e5 + 17, z = 256;
int n, ans, k;
string s;
bool bad[z][z];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s >> k;
  for (char x, y; k--;) cin >> x >> y, bad[x][y] = bad[y][x] = 1;
  for (int i = 0; i + 1 < s.size(); i++)
    if (bad[s[i]][s[i + 1]]) {
      int from = i, a = 0, b = 0;
      while (from >= 0 && (s[from] == s[i] || s[from] == s[i + 1]))
        (s[from--] == s[i] ? a : b)++;
      int to = i + 1;
      while (to < s.size() && (s[to] == s[i] || s[to] == s[i + 1]))
        (s[to++] == s[i] ? a : b)++;
      ans += min(a, b);
      i = to - 1;
    }
  cout << ans << '\n';
  return 0;
}
