#include <iostream>
#include <string>
using namespace std;
int main() {
string n;
int k;
cin >> n >> k;
bool reach[10][10] = {false};
for (int i = 0; i < 10; i++) {
reach[i][i] = true;
}
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
reach[x][y] = true;
}
for (int m = 0; m < 10; m++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (reach[i][m] && reach[m][j]) {
reach[i][j] = true;
}
}
}
}
long long result = 1;
for (char c : n) {
int digit = c - '0';
int count = 0;
for (int j = 0; j < 10; j++) {
if (reach[digit][j]) {
count++;
}
}
result *= count;
}
cout << result << endl;
return 0;
}
求求了