#include <bits/stdc++.h>
using namespace std;
long long p[100002][10];
long long v[100002][10];
int n;
int d[100001];
string t[100001];
const long long mod = 1e9 + 7;
int main() {
  cin >> t[0];
  d[0] = 0;
  cin >> n;
  n++;
  for (int i = 1; i < n; i++) {
    string line;
    cin >> line;
    d[i] = (line[0] - '0');
    t[i] = line.substr(3);
  }
  for (int e = 0; e < 10; e++) {
    v[e][n] = e;
    p[e][n] = 10;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int e = 0; e < 10; e++) {
      if (e != d[i]) {
        v[e][i] = v[e][i + 1];
        p[e][i] = p[e][i + 1];
      } else {
        p[e][i] = 1;
        for (int j = 0; j < t[i].size(); j++) {
          int f = (t[i][j] - '0');
          p[e][i] *= p[f][i + 1];
          p[e][i] %= mod;
        }
        v[e][i] = 0;
        long long prod = 1;
        for (int j = t[i].size() - 1; j >= 0; j--) {
          int f = (t[i][j] - '0');
          v[e][i] += (prod * v[f][i + 1]) % mod;
          v[e][i] %= mod;
          prod *= p[f][i + 1];
          prod %= mod;
        }
      }
    }
  }
  cout << v[0][0] << endl;
}
