#include <bits/stdc++.h>
using namespace std;
int GA[10][10];
int d[10];
int p[1000];
int main() {
  string a;
  int n;
  cin >> a >> n;
  int x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    GA[x][y] = 1;
  }
  for (int k = 1; k <= 9; k++)
    for (int i = 0; i <= 9; i++)
      for (int j = 1; j <= 9; j++)
        if (GA[i][k] && GA[k][j]) GA[i][j] = 1;
  for (int i = 0; i < 10; i++) {
    GA[i][i] = 1;
    for (int j = 1; j < 10; j++)
      if (GA[i][j]) d[i]++;
  }
  int c = 0;
  p[0] = 1;
  for (int i = 0; a[i]; i++) {
    c = 0;
    int x = d[a[i] - '0'];
    for (int i = 0; i < 500; i++) {
      p[i] = (p[i] * x + c);
      c = p[i] / 10;
      p[i] %= 10;
    }
  }
  int i = 500;
  while (p[i] == 0) i--;
  for (; i >= 0; i--) cout << p[i];
  cout << endl;
  return 0;
}
