#include <bits/stdc++.h>
using namespace std;
string str, queries[100010];
int n;
unsigned long long values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
unsigned long long length[10] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
int main() {
  cin >> str >> n;
  queries[0] = "0->" + str;
  for (int i = 0; i < n; i++) {
    cin >> queries[i + 1];
  }
  for (int i = n; i >= 0; i--) {
    unsigned long long len = 1, val = 0;
    for (int j = 3; j < queries[i].size(); ++j) {
      int d = queries[i][j] - '0';
      val = (val * length[d] + values[d]) % 1000000007;
      len = len * length[d] % 1000000007;
    }
    length[queries[i][0] - '0'] = len;
    values[queries[i][0] - '0'] = val;
  }
  cout << values[0] << endl;
  return 0;
}
