#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 5;
char num[N];
char str[N];
int val[11];
int pw[11];
int main() {
  scanf(" %s", num);
  for (int i = 0; i < 10; ++i) {
    val[i] = i;
    pw[i] = 10;
  }
  int q;
  scanf("%d", &q);
  vector<pair<char, string>> vec;
  char c;
  while (q--) {
    scanf(" %s", str);
    c = str[0];
    vec.push_back({c, string(str + 3)});
  }
  reverse(vec.begin(), vec.end());
  for (auto &sub : vec) {
    int id = sub.first - '0';
    string &s = sub.second;
    reverse(s.begin(), s.end());
    int p = 1, v = 0;
    for (auto &x : s) {
      v = (v + 1ll * val[x - '0'] * p) % mod;
      p = (1ll * p * pw[x - '0']) % mod;
    }
    val[id] = v;
    pw[id] = p;
  }
  int l = strlen(num);
  reverse(num, num + l);
  int p = 1, v = 0;
  for (int i = 0; num[i]; ++i) {
    int id = num[i] - '0';
    v = (v + 1ll * val[id] * p) % mod;
    p = (1ll * p * pw[id]) % mod;
  }
  printf("%d\n", v);
}
