#include <bits/stdc++.h>
using namespace std;
int res, resadv, n, numval[10], numadv[10], dtosub[100000];
string init, query[100000], line;
char c, c1;
void solve(string& tosub) {
  res = 0;
  resadv = 1;
  for (int i = tosub.size() - 1; i >= 0; i--) {
    res = (res + 1LL * resadv * numval[tosub[i] - '0']) % 1000000007;
    resadv = (1LL * resadv * numadv[tosub[i] - '0']) % 1000000007;
  }
}
char p[200000];
int main() {
  scanf(" %s %d", p, &n);
  init = (string)p;
  for (int i = 0; i < 10; i++) {
    numval[i] = i;
    numadv[i] = 10;
  }
  for (int i = 0; i < n; i++) {
    scanf(" %s", p);
    line = (string)p;
    dtosub[i] = line[0] - '0';
    query[i] = line.substr(3);
  }
  for (int i = n - 1; i >= 0; i--) {
    solve(query[i]);
    numval[dtosub[i]] = res;
    numadv[dtosub[i]] = resadv;
  }
  solve(init);
  printf("%d\n", res);
  return 0;
}
