#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int NMAX = 100004;
int n;
string s, q[NMAX];
int cntDigits[10], number[10];
int main() {
  cin >> s;
  cin >> n;
  cin.get();
  for (int i = 1; i <= n; ++i) {
    cin >> q[i];
  }
  for (int i = 0; i < 10; ++i) {
    cntDigits[i] = 10;
    number[i] = i;
  }
  for (int i = n; i >= 1; --i) {
    int newNumber = 0;
    int newCntDigits = 1;
    for (int j = 3; j < q[i].size(); ++j) {
      int currDigit = q[i][j] - '0';
      newCntDigits = (1LL * newCntDigits * cntDigits[currDigit]) % MOD;
      newNumber = (1LL * ((1LL * newNumber * cntDigits[currDigit]) % MOD) +
                   number[currDigit]) %
                  MOD;
    }
    cntDigits[q[i][0] - '0'] = newCntDigits;
    number[q[i][0] - '0'] = newNumber;
  }
  int rest = 0;
  for (int i = 0; i < s.size(); ++i) {
    int currDigit = s[i] - '0';
    rest = (1LL * ((1LL * rest * cntDigits[currDigit]) % MOD) +
            number[currDigit]) %
           MOD;
  }
  cout << rest << "\n";
  return 0;
}
