#include <bits/stdc++.h>
using namespace std;
void file() {}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int dx[] = {-1, 0, 0, 1, 1, 1, -1, -1};
int dy[] = {0, 1, -1, 0, 1, -1, 1, -1};
int n, m, mem[100005][26];
string s;
bool a[26][26];
int solve(int i, int last) {
  if (i == n) return 0;
  int &ret = mem[i][last];
  if (~ret) return ret;
  if (a[s[i] - 'a'][last]) return ret = 1 + solve(i + 1, last);
  return ret = min(solve(i + 1, s[i] - 'a'), 1 + solve(i + 1, last));
}
int main() {
  file();
  fast();
  cin >> s;
  n = s.size();
  cin >> m;
  while (m--) {
    char c1, c2;
    cin >> c1 >> c2;
    a[c1 - 'a'][c2 - 'a'] = a[c2 - 'a'][c1 - 'a'] = 1;
  }
  memset(mem, -1, sizeof mem);
  int mn = 1e9;
  for (int i = 0; i < n; i++) mn = min(mn, i + solve(i, s[i] - 'a'));
  cout << mn;
}
