#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
string q[N];
string s;
long long mo[10], sh[10];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  for (int i = 0; i < 10; i++) {
    mo[i] = i;
    sh[i] = 10;
  }
  for (int i = n - 1; i >= 0; i--) {
    long long tsh = 1, tmo = 0;
    for (int j = 3; j < q[i].size(); j++) {
      int v = q[i][j] - '0';
      tmo *= sh[v];
      tmo += mo[v];
      tmo %= mod;
      tsh *= sh[v];
      tsh %= mod;
    }
    mo[q[i][0] - '0'] = tmo;
    sh[q[i][0] - '0'] = tsh;
  }
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    int v = s[i] - '0';
    ans *= sh[v];
    ans += mo[v];
    ans %= mod;
  }
  cout << ans << "\n";
  return 0;
}
