#include <bits/stdc++.h>
using namespace std;
string s[100005];
int n;
long long a[10], b[10];
int main() {
  string ss;
  cin >> ss >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 0; i < 10; i++) a[i] = i;
  for (int i = 0; i < 10; i++) b[i] = 10;
  for (int i = n; i >= 1; i--) {
    long long x = 0, y = 1;
    for (int j = 3; j < s[i].size(); j++) {
      x = (x * b[s[i][j] - '0'] + a[s[i][j] - '0']) % 1000000007;
      y = y * b[s[i][j] - '0'] % 1000000007;
    }
    a[s[i][0] - '0'] = x;
    b[s[i][0] - '0'] = y;
  }
  long long ans = 0;
  for (int i = 0; i < ss.size(); i++)
    ans = (ans * b[ss[i] - '0'] + a[ss[i] - '0']) % 1000000007;
  cout << ans << endl;
  return 0;
}
