#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const double PI = 2 * asin(1);
const double EPS = 1e-8;
const int INF = 0x3fffffff;
const int MOD = 1e9 + 7;
struct Node {
  int d;
  string s;
  Node(int dd = 0, string ss = "") : d(dd), s(ss) {}
};
int g_m;
string g_s;
vector<Node> vn;
bool input() {
  cin >> g_s;
  if (cin.eof()) return false;
  vn.clear();
  cin >> g_m;
  int d;
  string s;
  for (int i = 0; i < g_m; ++i) {
    scanf("%d->", &d);
    getline(cin, s);
    vn.push_back(Node(d, s));
  }
  return true;
}
void solve() {
  LL w, v;
  vector<LL> vll_w, vll_v;
  for (int i = 0; i < 10; ++i) vll_v.push_back(i), vll_w.push_back(10);
  for (int i = g_m - 1; i >= 0; --i) {
    string &s = vn[i].s;
    int d = vn[i].d, l = s.size();
    v = 0, w = 1;
    for (int j = 0; j < l; ++j) {
      v *= vll_w[s[j] - '0'];
      v += vll_v[s[j] - '0'];
      v %= MOD;
      w *= vll_w[s[j] - '0'];
      w %= MOD;
    }
    vll_v[d] = v, vll_w[d] = w;
  }
  LL ans = 0;
  string &s = g_s;
  for (int i = 0; i < s.size(); ++i) {
    ans *= vll_w[s[i] - '0'];
    ans += vll_v[s[i] - '0'];
    ans %= MOD;
  }
  cout << ans << endl;
}
int main() {
  while (input()) {
    solve();
  }
  return 0;
}
