#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int max_n = 1e5 + 5;
string q[max_n];
int dig[10][2];
int main() {
  string s;
  cin >> s;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) cin >> q[i];
  for (int i = 0; i <= 9; i++) {
    dig[i][0] = i;
    dig[i][1] = 10;
  }
  for (int i = n; i >= 1; i--) {
    int d = q[i][0] - '0';
    int len = q[i].length();
    int sum = 0, pw = 1;
    for (int j = 3; j < len; j++) {
      int k = q[i][j] - '0';
      sum = (1LL * sum * dig[k][1] % mod + dig[k][0]) % mod;
      pw = 1LL * pw * dig[k][1] % mod;
    }
    dig[d][0] = sum;
    dig[d][1] = pw;
  }
  int len = s.length();
  int ans = 0;
  for (int i = 0; i < len; i++) {
    int d = s[i] - '0';
    ans = (1LL * ans * dig[d][1] % mod + dig[d][0]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
