public static void main(String args[]) { long pow = 1; for (int i = 0; i < tenPow.length; i++) { tenPow[i] = pow; pow = (pow * 10) % MOD; } String S = in.next(); int N = in.nextInt(); nodes = new Node[10]; Node root = new Node(-1); for (int i = 0; i < S.length(); i++) { root.children.add(get((int) S.charAt(i) - '0')); } for (int i = 0; i < N; i++) { String Q = in.next(); int d = (int) Q.charAt(0) - '0'; if (nodes[d] != null) { Node nd = nodes[d]; nodes[d] = null; for (int j = 3; j < Q.length(); j++) { nd.children.add(get((int) Q.charAt(j) - '0')); } if (Q.length() == 3) { nd.children.add(Node.EMPTY_LEAF); } } } out.println(root.ans()); }
long ans() { if (_ans != -3) return _ans; if (children.size() == 0) { if (this == EMPTY_LEAF) return 0; return i; } long digits = 0; long ret = 0; for (int i = children.size() - 1; i >= 0; i--) { Node node = children.get(i); ret = (ret + (node.ans() * modPow(digits)) % MOD) % MOD; digits += node.size(); digits %= MOD - 1; } _ans = ret; return ret; }