#include <bits/stdc++.h>
using namespace std;
string A[100000 + 5], s;
int val[10], fact[10];
int n;
int main() {
  cin >> s >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < 10; ++i) {
    val[i] = i, fact[i] = 10;
  }
  for (int i = n - 1; i >= 0; i--) {
    int d = A[i][0] - '0';
    int sz = A[i].size();
    long long ac = 0, f = 1;
    for (int j = sz - 1; j >= 3; j--) {
      int cur = A[i][j] - '0';
      ac = (ac + (val[cur] * f) % 1000000007) % 1000000007;
      f = (f * fact[cur]) % 1000000007;
    }
    val[d] = ac, fact[d] = f;
  }
  int sz = s.size();
  long long ac = 0, f = 1;
  for (int i = sz - 1; i >= 0; i--) {
    int cur = s[i] - '0';
    ac = (ac + (val[cur] * f) % 1000000007) % 1000000007;
    f = (f * fact[cur]) % 1000000007;
  }
  cout << ac << endl;
}
