#include <bits/stdc++.h>
using namespace std;
long long pow_mod(long long a, long long b, long long c) {
  long long ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) {
      ans *= a;
      ans %= c;
    }
    a *= a;
    a %= c;
  }
  return ans;
}
inline int get_digit(char ch) { return ch - '0'; }
int main() {
  string str;
  int q;
  while (cin >> str >> q) {
    vector<string> arr;
    for (int i = 0; i < q; i++) {
      string tmp;
      cin >> tmp;
      arr.push_back(tmp);
    }
    long long rep[10];
    int len[10];
    for (int i = 0; i < 10; i++) {
      rep[i] = i;
      len[i] = 1;
    }
    for (int i = q - 1; i >= 0; i--) {
      long long tmp = -1;
      int length = 0;
      if ((int)arr[i].length() > 3) {
        for (int j = 3; j < (int)arr[i].length(); j++) {
          int idx = get_digit(arr[i][j]);
          if (rep[idx] >= 0) {
            if (tmp < 0) {
              tmp = 0;
            }
            tmp = rep[idx] + tmp * pow_mod(10, len[idx], 1000000007);
            tmp %= 1000000007;
            length += len[idx];
            if (length >= 1000000007 - 1) {
              length -= 1000000007 - 1;
            }
          }
        }
      }
      rep[get_digit(arr[i][0])] = tmp;
      len[get_digit(arr[i][0])] = length;
    }
    long long ans = 0;
    for (int i = 0; i < (int)str.length(); i++) {
      int idx = get_digit(str[i]);
      if (rep[idx] >= 0) {
        ans = ans * pow_mod(10, len[idx], 1000000007) + rep[idx];
        ans %= 1000000007;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
