#include <bits/stdc++.h>
using namespace std;
long long func[200] = {0};
long long len[200] = {0};
long long MOD = (long long)(1e9) + 7;
long long modpow(long long x, long long y) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    y /= 2;
  }
  return ans;
}
int main() {
  string s;
  cin >> s;
  long long n;
  cin >> n;
  for (int i = (int)('0'); i <= (int)('9'); i++) {
    func[i] = i - '0';
    len[i] = 10;
  }
  vector<string> v;
  while (n--) {
    string p;
    cin >> p;
    v.push_back(p);
  }
  reverse((v).begin(), (v).end());
  for (string q : v) {
    int lhs = q[0];
    string rhs = q.substr(3), conv;
    long long l = 1, val = 0;
    reverse((rhs).begin(), (rhs).end());
    for (int c : rhs) {
      val += (l * func[c]) % MOD;
      l = (l * len[c]) % MOD;
      val %= MOD;
    }
    len[lhs] = l;
    func[lhs] = val;
  }
  reverse((s).begin(), (s).end());
  long long ans = 0, l = 1;
  for (int c : s) {
    ans += (l * func[c]) % MOD;
    l = (l * len[c]) % MOD;
    ans %= MOD;
  }
  cout << ans;
}
