#include <bits/stdc++.h>
using namespace std;
long long int power(long long int b, long long int p) {
  long long int r = 1;
  while (p) {
    if (p & 1ll) {
      r *= b;
      if (r >= 1000000007) r %= 1000000007;
    }
    b *= b;
    if (b >= 1000000007) b %= 1000000007;
    p >>= 1ll;
  }
  return r;
}
const int N = (int)1e5 + 5;
const int Q = 301;
int dp[10][N], num[10][N];
string query[N];
string s, temp;
void solve() {
  cin >> s;
  int q, p;
  cin >> q;
  for (int i = 0; i <= q - 1; i++) {
    cin >> temp;
    query[i] = temp;
  }
  for (int i = 0; i <= 9; i++) {
    dp[i][q] = i;
    num[i][q] = 1;
  }
  for (int i = q - 1; i >= 0; i--) {
    for (int j = 0; j <= 9; j++) {
      dp[j][i] = dp[j][i + 1];
      num[j][i] = num[j][i + 1];
    }
    p = query[i][0] - '0';
    temp = query[i].substr(3, query[i].size() - 3);
    long long int pos = 0;
    dp[p][i] = 0;
    for (int k = temp.size() - 1; k >= 0; k--) {
      dp[p][i] += (dp[temp[k] - '0'][i + 1] * power(10, pos)) % 1000000007;
      if (dp[p][i] >= 1000000007) dp[p][i] -= 1000000007;
      pos += num[temp[k] - '0'][i];
      pos %= (1000000007 - 1);
    }
    num[p][i] = pos;
  }
  int ans = 0;
  int pos = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    ans += (dp[s[i] - '0'][0] * power(10, pos)) % 1000000007;
    if (ans >= 1000000007) ans -= 1000000007;
    pos += num[s[i] - '0'][0];
    pos %= (1000000007 - 1);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  for (int TC = 1; TC <= T; TC++) {
    solve();
    cout << "\n";
  }
}
