#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
string s;
long long p[N];
pair<long long, long long> a[11];
pair<int, string> qi[N];
pair<int, string> parse(string &a) {
  pair<int, string> ret;
  ret.first = a[0] - '0';
  for (int i = 3; i < a.size(); i++) {
    ret.second += a[i];
  }
  return ret;
}
long long Pow(long long b, long long e) {
  if (e == 0) {
    return 1;
  }
  long long ret = Pow(b, e / 2);
  ret = ret * ret % mod;
  if (e & 1) {
    ret = ret * b % mod;
  }
  return ret;
}
int main() {
  cin >> s;
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    string buff;
    cin >> buff;
    qi[i] = parse(buff);
  }
  qi[0] = make_pair(10, s);
  for (int i = 0; i < 10; i++) {
    a[i] = make_pair(1, i);
  }
  for (int j = q; j >= 0; j--) {
    string &rep = qi[j].second;
    int &idx = qi[j].first;
    long long len = 0, cur = 0;
    for (int i = (int)rep.size() - 1; i >= 0; i--) {
      cur += Pow(10, len) * a[rep[i] - '0'].second % mod;
      cur %= mod;
      len += a[rep[i] - '0'].first;
      len %= mod - 1;
    }
    a[idx] = make_pair(len, cur);
  }
  cout << a[10].second << '\n';
}
