#include <bits/stdc++.h>
using namespace std;
string str;
vector<string> query;
long long output() {
  vector<long long> val(10), mul(10);
  for (int i = 0; i < 10; i++) val[i] = i, mul[i] = 10;
  long long num, temp;
  for (int i = query.size() - 1; i >= 0; i--) {
    num = query[i][0] - '0';
    long long v = 0, m = 1;
    for (int k = 3; k < query[i].size(); k++) {
      temp = query[i][k] - '0';
      v = ((v * mul[temp]) % 1000000007 + val[temp]) % 1000000007;
      m = (m * mul[temp]) % 1000000007;
    }
    val[num] = v, mul[num] = m;
  }
  long long ret = 0;
  for (int i = 0; i < str.size(); i++) {
    num = str[i] - '0';
    ret = ((ret * mul[num]) % 1000000007 + val[num]) % 1000000007;
  }
  return ret;
}
int main() {
  cin >> str;
  int n;
  cin >> n;
  query.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> query[i];
  }
  cout << output();
}
