#include <bits/stdc++.h>
using namespace std;
char s[100010];
char req[100010];
struct Node;
Node* digits[16];
struct Node {
  long long sz;
  long long res;
  vector<Node*> ch;
  Node(int d) : sz(1), res(d), ch(vector<Node*>()) {}
  Node(char* s) : sz(-1), res(-1), ch(vector<Node*>()) { reset(s); }
  void reset(char* s) {
    sz = -1;
    res = -1;
    ch.resize(strlen(s));
    for (int i = 0; i < int(ch.size()); ++i) {
      ch[i] = digits[s[i] - '0'];
    }
  }
  void dfs(int depth) {
    if (res != -1) return;
    if (sz == 0) {
      res = 0;
      return;
    }
    res = 0;
    sz = 0;
    for (int i = 0; i < int(ch.size()); ++i) {
      Node* sub = ch[i];
      sub->dfs(depth + 1);
      long long power = 10;
      long long prod = 1;
      for (int k = sub->sz; k != 0; k >>= 1) {
        if (k & 1) {
          prod = prod * power % 1000000007;
        }
        power = power * power % 1000000007;
      }
      res = res * prod % 1000000007;
      res = (res + sub->res) % 1000000007;
      sz = (sz + sub->sz) % (1000000007 - 1);
    }
  }
};
int main() {
  int n;
  for (int i = 0; i < 10; ++i) {
    digits[i] = new Node(i);
  }
  scanf("%s%d", s, &n);
  Node* top = new Node(s);
  for (int i = 0; i < n; ++i) {
    scanf("%s", req);
    int d = req[0] - '0';
    Node* old = digits[d];
    digits[d] = new Node(d);
    old->reset(req + 3);
  }
  top->dfs(0);
  printf("%lld\n", top->res);
  return 0;
}
