#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int main() {
  string inp;
  cin >> inp;
  int p10[10];
  int val[10];
  for (int i = 0; i < 10; i++) {
    p10[i] = 10;
    val[i] = i;
  }
  string queries[100000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> queries[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    int src = queries[i][0] - '0';
    int pc10 = 1, vc = 0;
    for (int j = 3; j < queries[i].size(); j++) {
      int curr = queries[i][j] - '0';
      pc10 = (1ll * p10[curr] * pc10) % mod;
      vc = (1ll * vc * p10[curr] + val[curr]) % mod;
    }
    p10[src] = pc10;
    val[src] = vc;
  }
  int ans = 0;
  for (int i = 0; i < inp.size(); i++) {
    int curr = inp[i] - '0';
    ans = (1ll * ans * p10[curr] + val[curr]) % mod;
  }
  cout << ans << endl;
}
