#include <bits/stdc++.h>
using namespace std;
vector<char> a[128];
unordered_map<string, bool> mp;
string s;
int n, ans;
void dfs(string s) {
if (mp[s]) return;
mp[s] = true; ans++;
int len = s.size();
for (int i = 0; i < len; i++) {
string t = s;
for (char u : a[t[i]]) {
t[i] = u;
dfs(t);
}
}
return;
}
int main() {
cin >> s >> n;
for (int i = 1; i <= n; i++) {
char x, y;
cin >> x >> y;
a[x].push_back(y);
}
dfs(s);
cout << ans;
return 0;
}