#include <bits/stdc++.h>
using namespace std;
struct _ {
  ios_base::Init i;
  _() {
    cin.sync_with_stdio(0);
    cin.tie(0);
  }
} _;
int n, m;
vector<int> g(10);
vector<int> d(10);
int main() {
  string s;
  cin >> s;
  cin >> n;
  for (int i = 0; i < 10; ++i) {
    d[i] = 10;
    g[i] = i;
  }
  const long long M = 1e9 + 7;
  long long dec = 1;
  vector<string> vecRules(n);
  for (int i = 0; i < n; ++i) cin >> vecRules[i];
  string rule;
  for (int i = 1; i < n + 1; ++i) {
    int x = vecRules[n - i][0] - '0';
    rule = vecRules[n - i].substr(3);
    ;
    ;
    long long result = 0;
    int k = rule.size() - 1;
    dec = 1;
    while (k >= 0) {
      int idx = rule[k--] - '0';
      result = (result + (g[idx] * dec) % M) % M;
      dec = (dec * d[idx]) % M;
    };
    d[x] = dec;
    g[x] = result;
    ;
  }
  int k = s.size() - 1;
  long long res = 0;
  dec = 1;
  for (int i = 0; i < s.size(); ++i) {
    int x = s[s.size() - 1 - i] - '0';
    res = (res + (g[x] * dec) % M) % M;
    dec = (dec * d[x]) % M;
    ;
    ;
  }
  cout << res << endl;
  return 0;
}
