
- #include
- using namespace std;
-
- struct Int
- {
- #define MAXN 100000
- int a[MAXN + 10];
- Int()
- {
- *this = 0;
- }
- Int(const long long num)
- {
- *this = num;
- }
- Int(const string s)
- {
- *this = s;
- }
- Int& operator = (string s)
- {
- bool flag = false;
- if(s[0] == '-')
- {
- flag = true;
- s.erase(0,1);
- }
- a[0] = s.size();
- for(int i = 0;i < int(s.size());i++)
- {
- a[a[0] - i] = s[i] - '0';
- }
- if(flag)
- {
- for(int i = 1;i <= a[0];i++)
- {
- a[i] = -a[i];
- }
- }
- return *this;
- }
- Int& operator = (const long long num)
- {
- if(num < 0)
- {
- long long n = num;
- n = -n;
- string s = to_string(n);
- a[0] = s.size();
- for(int i = 0;i < int(s.size());i++)
- {
- a[a[0] - i] = -(s[i] - '0');
- }
- return *this;
- }
- return *this = to_string(num);
- }
- int& operator [] (int x)
- {
- return a[x];
- }
- size_t size()
- {
- return a[0];
- }
- bool operator < (Int b) const
- {
- if(this->a[0] != b[0]) return this->a[0] < b[0];
- for(int i = this->a[0];i >= 1;i--)
- {
- if(this->a[i] != b[i])
- {
- return this->a[i] < b[i];
- }
- }
- return false;
- }
- bool operator > (Int b) const
- {
- if(this->a[0] != b[0]) return this->a[0] > b[0];
- for(int i = this->a[0];i >= 1;i--)
- {
- if(this->a[i] != b[i])
- {
- return this->a[i] > b[i];
- }
- }
- return false;
- }
- bool operator == (Int b) const
- {
- if(this->a[0] != b[0]) return false;
- for(int i = this->a[0];i >= 1;i--)
- {
- if(this->a[i] != b[i])
- {
- return false;
- }
- }
- return true;
- }
- bool operator != (Int b) const
- {
- return !(*this == b);
- }
- bool operator <= (Int b) const
- {
- if(this->a[0] != b[0]) return this->a[0] <= b[0];
- for(int i = this->a[0];i >= 1;i--)
- {
- if(this->a[i] != b[i])
- {
- return this->a[i] <= b[i];
- }
- }
- return false;
- }
- bool operator >= (Int b) const
- {
- if(this->a[0] != b[0]) return this->a[0] >= b[0];
- for(int i = this->a[0];i >= 1;i--)
- {
- if(this->a[i] != b[i])
- {
- return this->a[i] >= b[i];
- }
- }
- return false;
- }
- Int operator + (Int y)
- {
- Int z;
- z[0] = 0;
- if(y[0] == 1 && y[1] == 0)
- {
- return *this;
- }
- for(int i = 1,g = 0;;i++)
- {
- if(!g && i > a[0] && i > y[0]) break;
- int t = g;
- if(i <= a[0]) t += a[i];
- if(i <= y[0]) t += y[i];
- z[++z[0]] = t % 10;
- g = t / 10;
- }
- return z;
- }
- Int operator + (long long y)
- {
- return *this + Int(y);
- }
- Int& operator += (Int y)
- {
- return *this = *this + y;
- }
- Int& operator += (long long y)
- {
- return *this = *this + Int(y);
- }
- Int operator ++ ()
- {
- return *this += 1;
- }
- Int operator - (Int y)
- {
- Int x = *this;
- bool flag = true;
- if(x < y)
- {
- swap(x,y);
- flag = false;
- }
- Int z;
- z[0] = 0;
- if(y[0] == 1 && y[1] == 0)
- {
- return *this;
- }
- for(int i = 1,g = 0;;i++)
- {
- if(!g && i > x[0] && i > y[0]) break;
- int t = g;
- if(i <= x[0]) t += x[i];
- if(i <= y[0]) t -= y[i];
- if(t >= 0)
- {
- z[++z[0]] = t;
- g = 0;
- }
- else
- {
- z[++z[0]] = t + 10;
- g = -1;
- }
- }
- while(!z[z[0]]) --z[0];
- if(z[0] <= 0)
- {
- z[0] = 1;
- }
- if(!flag)
- {
- for(int i = 1;i <= z[0];i++)
- {
- z[i] = -z[i];
- }
- }
- return z;
- }
- Int operator - (long long y)
- {
- return *this - Int(y);
- }
- Int& operator -= (Int y)
- {
- return *this = *this - y;
- }
- Int& operator -= (long long y)
- {
- return *this = *this - Int(y);
- }
- Int operator -- ()
- {
- return *this -= 1;
- }
- Int operator * (long long y)
- {
- if(y == 0) return Int("0");
- bool flag = false;
- if(y < 0)
- {
- y = -y;
- flag = true;
- }
- Int z;
- z[0] = 0;
- int u = 0;
- for(int i = 1;i <= a[0];i++)
- {
- int t = a[i] * y + u;
- z[i] = t % 10;
- z[0]++;
- u = t / 10;
- }
- while(u > 0)
- {
- z[++z[0]] = u % 10;
- u /= 10;
- }
- if(flag)
- {
- for(int i = 1;i <= z[0];i++)
- {
- z[i] = -z[i];
- }
- }
- return z;
- }
- Int operator * (Int y)
- {
- Int z;
- z[0] = 0;
- for(int i = a[0];i >= 1;i--)
- {
- z = z * 10 + y * a[i];
- }
- for(int i = MAXN;i >= 1;i--)
- {
- if(z[i] != 0)
- {
- z[0] = i;
- return z;
- }
- }
- return Int(0);
- }
- Int& operator *= (Int y)
- {
- return *this = *this * y;
- }
- Int& operator *= (long long y)
- {
- return *this = *this * y;
- }
- Int operator / (long long y)
- {
- Int z;
- z[0] = 0;
- long long r = 0;
- bool zero = true;
- for(int i = a[0];i >= 1;i--)
- {
- long long t = r * 10 + a[i];
- if(zero && t / y) zero = false;
- if(!zero) z[++z[0]] = t / y;
- r = t % y;
- }
- reverse(z.a + 1,z.a + z[0] + 1);
- while(z[z[0]] == 0 && z[0] > 1) z[0]--;
- return z;
- }
- Int operator / (Int y)
- {
- Int z;
- z[0] = 0;
-
- return z;
- }
- Int& operator /= (long long y)
- {
- return *this = *this / y;
- }
- Int operator % (long long y)
- {
- return *this - *this / y * y;
- }
- Int& operator %= (long long y)
- {
- return *this = *this % y;
- }
- Int operator % (Int y)
- {
- return *this - *this / y * y;
- }
- Int& operator %= (Int y)
- {
- return *this = *this % y;
- }
- Int operator << (int n)
- {
- return *this * int(pow(2,n));
- }
- Int operator <<= (int n)
- {
- return *this = *this * int(pow(2,n));
- }
- Int operator >> (int n)
- {
- return *this / int(pow(2,n));
- }
- Int operator >>= (int n)
- {
- return *this = *this / int(pow(2,n));
- }
- };
- Int operator ++ (Int &x,int a)
- {
- return (x += 1) - 1;
- }
- Int operator -- (Int &x,int a)
- {
- return (x -= 1) + 1;
- }
- ostream& operator << (ostream& out,Int a)
- {
- if(a[a[0]] < 0)
- {
- out << "-";
- }
- for(int i = a[0];i >= 1;i--)
- {
- out << abs(a[i]);
- }
- return out;
- }
- istream& operator >> (istream& in,Int &a)
- {
- string s;
- in >> s;
- a = s;
- return in;
- }
- int mul[50];
- string s;
- int k;
- int a[30],b[30],vis[30],flag[15];
- void dfs(int j,int n)
- {
- for(int i = 1;i <= k;i++)
- {
- if(a[i] == n && !vis[i])
- {
- vis[i] = true;
- flag[b[i]] = true;
- dfs(j,b[i]);
- vis[i] = false;
- }
- }
- }
- int main()
- {
- cin >> s >> k;
- for(int i = 1;i <= k;i++)
- {
- cin >> a[i] >> b[i];
- }
- Int ans = 1;
- for(int i = 0;i < s.size();i++)
- {
- memset(flag,false,sizeof(flag));
- dfs(i,s[i] - '0');
- int cnt = 0;
- flag[s[i] - '0'] = true;
- for(int i = 0;i <= 9;i++)
- {
- cnt += flag[i];
- }
- ans *= cnt;
- }
- cout << ans << endl;
- return 0;
- }