#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10, MOD = 1000 * 1000 * 1000 + 7,
          INF = 2 * 1000 * 1000 * 1000;
const double EPS = 1e-9;
int val[11];
int len[11];
string queries[maxn];
int mul(int a, int b) { return ((long long)a * b) % MOD; }
int add(int a, int b) { return (a + b) % MOD; }
int fastPow(int a, int b) {
  int ret = 1;
  while (b != 0) {
    if (b % 2 == 1) {
      ret = mul(ret, a);
    }
    b /= 2;
    a = mul(a, a);
  }
  return ret % MOD;
}
int main() {
  for (int i = 0; i <= 9; i++) {
    val[i] = i;
    len[i] = 1;
  }
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  int query;
  cin >> query;
  for (int i = 0; i < query; i++) {
    cin >> queries[i];
  }
  for (int i = query - 1; i >= 0; i--) {
    int digit = queries[i][0] - '0';
    string nextStr = queries[i].substr(3);
    reverse(nextStr.begin(), nextStr.end());
    int nextVal = 0;
    int sumLen = 0;
    for (int j = 0; j < (int)((nextStr).size()); j++) {
      int curDigit = nextStr[j] - '0';
      nextVal = add(nextVal, mul(val[curDigit], fastPow(10, sumLen)));
      sumLen = (sumLen + len[curDigit]) % (MOD - 1);
    }
    val[digit] = nextVal;
    len[digit] = sumLen;
  }
  int ans = 0;
  int sumLen = 0;
  for (int i = 0; i < (int)((str).size()); i++) {
    int curDigit = str[i] - '0';
    ans = add(ans, mul(val[curDigit], fastPow(10, sumLen)));
    sumLen = (sumLen + len[curDigit]) % (MOD - 1);
  }
  cout << ans << endl;
  return 0;
}
