#include <bits/stdc++.h>
using namespace std;
string s;
vector<vector<long long> > gr(300, vector<long long>(300));
int dp[100001][300];
int sol(int I, int l) {
  if (I == s.length()) return 0;
  if (dp[I][l] != -1) return dp[I][l];
  if (!gr[s[I]][l]) {
    int x = sol(I + 1, l) + 1;
    int y = sol(I + 1, s[I]);
    return dp[I][l] = min(x, y);
  } else {
    return dp[I][l] = sol(I + 1, l) + 1;
  }
}
void pr(int I, int l) {
  if (I == s.length()) return;
  if (!gr[s[I]][l]) {
    cout << (char)l;
    pr(I + 1, s[I]);
  } else {
    int x = 1 + sol(I + 1, l);
    int y = 1 + sol(I + 1, s[I]);
    if (x <= y) {
      pr(I + 1, l);
    } else {
      pr(I + 1, s[I]);
    }
  }
}
int main() {
  cin >> s;
  int k;
  cin >> k;
  for (long long i = 0; i < (k); i++) {
    char a, b;
    cin >> a >> b;
    gr[a][b] = 1;
    gr[b][a] = 1;
  }
  memset(dp, -1, sizeof(dp));
  cout << sol(0, 0);
  return 0;
}
