#include <bits/stdc++.h>
using namespace std;
int NWD(int a, int b) { return (b == 0) ? (a) : (NWD(b, a % b)); }
char s[100007];
int n;
int k[100007];
vector<int> t[100007];
int m = 1000000007;
char pom[100007];
int w[11];
int l[11];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> s;
  cin >> n;
  for (int i = 0; i < (n); ++i) {
    cin >> pom;
    k[i] = pom[0] - '0';
    for (int j = 3; pom[j]; j++) t[i].push_back(pom[j] - '0');
  }
  for (int i = (0); i <= (9); ++i) l[i] = 10;
  for (int i = (0); i <= (9); ++i) w[i] = i;
  for (int i = (n - 1); i >= (0); --i) {
    long long wyn = 0;
    long long len = 1;
    for (int j = 0; j < t[i].size(); ++j) {
      len *= l[t[i][j]];
      len %= m;
      wyn *= l[t[i][j]];
      wyn += w[t[i][j]];
      wyn %= m;
    }
    w[k[i]] = wyn;
    l[k[i]] = len;
  }
  long long wynik = 0;
  for (int i = 0; s[i]; ++i) {
    wynik *= l[s[i] - '0'];
    wynik += w[s[i] - '0'];
    wynik %= m;
  }
  cout << wynik << endl;
  return 0;
}
