#include <bits/stdc++.h>
using namespace std;
string q[100001];
long long rep[10], repw[10];
int main() {
  string s;
  ios::sync_with_stdio(false);
  cin >> s;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> q[i];
  for (int i = 0; i < 10; ++i) {
    rep[i] = i;
    repw[i] = 10;
  }
  for (int i = n; i >= 1; --i) {
    long long tmp = 0, tmpw = 1;
    for (size_t j = 3; j < q[i].size(); ++j) {
      ((tmp *= repw[q[i][j] - '0']) += rep[q[i][j] - '0']) %= 1000000007;
      (tmpw *= repw[q[i][j] - '0']) %= 1000000007;
    }
    rep[q[i][0] - '0'] = tmp;
    repw[q[i][0] - '0'] = tmpw;
  }
  long long ans = 0;
  for (size_t i = 0; i < s.size(); ++i)
    ((ans *= repw[s[i] - '0']) += rep[s[i] - '0']) %= 1000000007;
  cout << ans;
  return 0;
}
