#include <bits/stdc++.h>
using namespace std;
int countBits(long long mask) {
  int res = 0;
  while (mask) mask &= (mask - 1), ++res;
  return res;
}
string toString(long long n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
long long toNumber(string s) {
  stringstream ss;
  long long n;
  ss << s;
  ss >> n;
  return n;
}
int from[100001];
string to[100001];
int main() {
  string number, query;
  long long mod_value[11], power_of_ten[11];
  int q;
  while (cin >> number) {
    cin >> q;
    for (int i = (int)(0), _m = (int)(q); i < _m; ++i) {
      cin >> query;
      from[i] = query[0] - '0';
      to[i] = query.substr(3);
    }
    for (int digit = (int)(0), _m = (int)(10); digit < _m; ++digit) {
      mod_value[digit] = digit;
      power_of_ten[digit] = 10;
    }
    for (int i = (int)(q - 1), _m = (int)(0); i >= _m; --i) {
      long long new_mod_value = 0;
      long long new_power_of_ten = 1;
      for (int j = (int)(0), _m = (int)(to[i].size()); j < _m; ++j) {
        int digit = to[i][j] - '0';
        new_mod_value = (new_mod_value * power_of_ten[digit]) % 1000000007;
        new_mod_value = (new_mod_value + mod_value[digit]) % 1000000007;
        new_power_of_ten =
            (new_power_of_ten * power_of_ten[digit]) % 1000000007;
      }
      mod_value[from[i]] = new_mod_value;
      power_of_ten[from[i]] = new_power_of_ten;
    }
    long long res = 0;
    for (int i = (int)(0), _m = (int)(number.size()); i < _m; ++i) {
      int digit = number[i] - '0';
      res = (res * power_of_ten[digit]) % 1000000007;
      res = (res + mod_value[digit]) % 1000000007;
    }
    cout << res << endl;
  }
}
