#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const int MAXN = 100005;
long long int d[MAXN];
vector<long long int> t[MAXN];
long long int Val[12], Pw[12];
int main() {
  string s;
  cin >> s;
  string s1;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s1;
    d[i] = s1[0] - '0';
    for (int j = 3; j < s1.length(); j++) {
      t[i].push_back(s1[j] - '0');
    }
  }
  for (int i = 0; i <= 10; i++) {
    Val[i] = i;
    Pw[i] = 10;
  }
  for (int i = n; i >= 1; i--) {
    long long int v = 0, pp = 1;
    for (int j = 0; j < t[i].size(); j++) {
      pp = (pp * Pw[t[i][j]]) % MOD;
      v = (v * Pw[t[i][j]]) % MOD;
      v = (v + Val[t[i][j]]) % MOD;
    }
    Val[d[i]] = v, Pw[d[i]] = pp;
  }
  long long int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans = (ans * Pw[s[i] - '0']) % MOD;
    ans = (ans + Val[s[i] - '0']) % MOD;
  }
  cout << ans << endl;
}
