#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5;
string s;
string in[N + 1];
long long rem[10];
long long exLen[10];
int main() {
  cin >> s;
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> in[i];
  }
  for (long long i = 0; i <= 9; i++) {
    rem[i] = i;
    exLen[i] = 10;
  }
  for (long long i = n; i >= 1; i--) {
    long long total = 0, exSize = 1;
    for (long long j = in[i].size() - 1; j >= 3; j--) {
      total = (total + rem[in[i][j] - '0'] * exSize) % 1000000007LL;
      exSize = (exSize * exLen[in[i][j] - '0']) % 1000000007LL;
    }
    rem[in[i][0] - '0'] = total;
    exLen[in[i][0] - '0'] = exSize;
  }
  long long total = 0, exSize = 1;
  for (long long j = s.size() - 1; j >= 0; j--) {
    total = (total + rem[s[j] - '0'] * exSize) % 1000000007LL;
    exSize = (exSize * exLen[s[j] - '0']) % 1000000007LL;
  }
  cout << total << '\n';
}
