#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
string s, c[N + 2];
int n;
long long jzs[10];
long long vs[10];
void chu() {
  for (int i = 0; i < 10; i++) {
    jzs[i] = 10;
    vs[i] = i;
  }
}
int main() {
  while (cin >> s >> n) {
    chu();
    c[0] = "0->" + s;
    for (int i = 1; i <= n; i++) cin >> c[i];
    long long jz, v;
    for (int i = n; i >= 0; i--) {
      jz = 1, v = 0;
      int len = c[i].size();
      for (int j = 3; j < len; j++) {
        int num = c[i][j] - '0';
        v = (v * jzs[num] + vs[num]) % mod;
        jz = jz * jzs[num] % mod;
      }
      int num = c[i][0] - '0';
      jzs[num] = jz;
      vs[num] = v;
    }
    cout << vs[0] << endl;
  }
  return 0;
}
