#include <bits/stdc++.h>
using namespace std;
const long long kMod = 1000000007;
int main(void) {
  string start;
  int n;
  cin >> start >> n;
  vector<pair<char, string> > operations;
  operations.push_back(pair<char, string>('0', start));
  for (int i = 0; i < n; ++i) {
    string line;
    cin >> line;
    operations.push_back(pair<char, string>(line[0], line.substr(3)));
  }
  long long adds[10];
  long long muls[10];
  for (int i = 0; i < 10; ++i) {
    adds[i] = i;
    muls[i] = 10;
  }
  for (int i = n; i >= 0; --i) {
    int d = operations[i].first - '0';
    const string& t = operations[i].second;
    long long add = 0;
    long long mul = 1;
    for (int j = 0; j < t.size(); ++j) {
      int k = t[j] - '0';
      add = (add * muls[k] + adds[k]) % kMod;
      mul = (mul * muls[k]) % kMod;
    }
    adds[d] = add;
    muls[d] = mul;
  }
  cout << adds[0] << "\n";
  return 0;
}
