#include <bits/stdc++.h>
using namespace std;
const int RULE = 100005;
const int BUF = 100005;
const int MOD = 1000000007;
const int MOD2 = 1000000006;
int mul(int a, int b) { return 1LL * a * b % MOD; }
int add(int a, int b) { return (a + b) % MOD; }
string init;
int nRule;
pair<int, string> rule[RULE];
void read() {
  cin >> init;
  cin >> nRule;
  for (int i = 0; i < nRule; ++i) {
    string s;
    cin >> s;
    rule[i].first = s[0] - '0';
    rule[i].second = s.substr(3);
  }
}
int pow(int p, int n) {
  if (n == 0) return 1;
  int t = pow(p, n / 2);
  return n & 1 ? mul(p, mul(t, t)) : mul(t, t);
}
void work() {
  int val[10];
  int len[10];
  for (int i = 0; i < 10; ++i) {
    val[i] = i;
    len[i] = 1;
  }
  for (int loop = nRule - 1; loop >= 0; --loop) {
    int digit = rule[loop].first;
    string &transformed = rule[loop].second;
    int nexVal = 0;
    int nexLen = 0;
    int curPow = 0;
    for (int i = transformed.size() - 1; i >= 0; --i) {
      nexVal = add(nexVal, mul(pow(10, curPow), val[transformed[i] - '0']));
      curPow = (curPow + len[transformed[i] - '0']) % MOD2;
    }
    for (int i = 0; i < transformed.size(); ++i) {
      nexLen = (nexLen + len[transformed[i] - '0']) % MOD2;
    }
    val[digit] = nexVal;
    len[digit] = nexLen;
  }
  int ans = 0;
  int curPow = 0;
  for (int i = init.size() - 1; i >= 0; --i) {
    ans = add(ans, mul(pow(10, curPow), val[init[i] - '0']));
    curPow = (curPow + len[init[i] - '0']) % MOD2;
  }
  cout << ans << endl;
}
int main() {
  read();
  work();
  return 0;
}
