#include <bits/stdc++.h>
using namespace std;
long long powe(long long x, long long y) {
  if (y == 0) return 1;
  long long ans = powe(x, y / 2);
  ans *= ans;
  if (ans >= 1000000007) {
    ans %= 1000000007;
  }
  if (y & 1) {
    ans *= x;
  }
  ans %= 1000000007;
  return ans;
}
string a[1000010];
long long val[11];
long long siz[11];
long long update(long long x, long long st) {
  long long tmp = 0;
  long long ans = 0, num = 1;
  for (long long i = a[st].size() - 1; i >= 3; i--) {
    ans = ans + (val[a[st][i] - '0'] * num);
    num *= siz[a[st][i] - '0'];
    num %= 1000000007;
    ans %= 1000000007;
  }
  siz[x] = num;
  val[x] = ans;
}
int main() {
  string s;
  cin >> s;
  long long q;
  cin >> q;
  for (int i = 0; i < 10; i++) {
    siz[i] = 10;
    val[i] = i;
  }
  for (int i = 0; i < q; i++) {
    cin >> a[i];
  }
  for (int i = q - 1; i >= 0; i--) {
    update(a[i][0] - '0', i);
  }
  long long num = 1, ans = 0;
  for (long long i = s.length() - 1; i >= 0; i--) {
    ans = ans + val[s[i] - '0'] * num;
    num *= siz[s[i] - '0'];
    num %= 1000000007;
    ans %= 1000000007;
  }
  cout << ans << endl;
}
