Language: C++

#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

string S;
long long Q,A,B;

struct NODE{
	char c;
	vector<NODE*> ch;
};
#define INF (1e18+10000)

NODE* alp[128] = {};

map<NODE*,long long> hash;

long long add(long long a,long long b){
	return (long double)a+b >= INF ? INF : a+b; 
}
long long size(NODE *r){
	if( r->c != '.' ) return 1;
	if( hash.count(r) ) return hash[r];
	hash[r] = 0;
	for(int i = 0 ; i < r->ch.size() ; i++){
		hash[r] = add(hash[r],size(r->ch[i]));
	}
	return hash[r];
}

int C = 0;
int F = 0;
int dfs(NODE *r,long long cur){
	if( C == 0 && F == 1 ) return 0;
	if( r->c != '.' ){
		if( cur == 0 && F == 0 ){
			F = 1;
			cout << r->c;
			C = B-A;
		}else if( F ){
			if( C > 0 ){
				C--;
				cout << r->c;
			}
		}
	}
	for(int i = 0 ; i < r->ch.size() ; i++){
		if( F ){
			dfs(r->ch[i],cur);
		}else{
			if( size(r->ch[i]) <= cur ){
				cur -= size(r->ch[i]);
			}else{
				dfs(r->ch[i],cur);
			}
		}
	}
	
	
}

int main(){
	for(int i = 'a' ; i <= 'z' ; i++){
		alp[i] = new NODE();
		alp[i]->c = i;
	}
	NODE* root = new NODE();
	root->c = '.';
	cin >> S;
	for(int i = 0 ; i < S.size() ; i++){
		root->ch.push_back(alp[S[i]]);
	}
	cin >> Q >> A >> B;
	A--,B--;
	for(int i = 0 ; i < Q ; i++){
		char x;
		string y;
		cin >> x >> y;
		NODE *prev = alp[x];
		prev->c = '.';
		
		alp[x] = new NODE();
		alp[x]->c = x;
		
		if( y != "." ){
			for(int j = 0 ; j < y.size() ; j++){
				prev->ch.push_back(alp[y[j]]);
			}
		}
	}
	if( size(root) <= B ){
		cout << "." << endl;
		return 0;
	}
	dfs(root,A);
	cout << endl;
}

Compiler output:
a.cc: In function 'long long int size(NODE*)':
a.cc:28:13: error: reference to 'hash' is ambiguous
   28 |         if( hash.count(r) ) return hash[r];
      |             ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc:28:36: error: reference to 'hash' is ambiguous
   28 |         if( hash.count(r) ) return hash[r];
      |                                    ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc:29:9: error: reference to 'hash' is ambiguous
   29 |         hash[r] = 0;
      |         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc:31:17: error: reference to 'hash' is ambiguous
   31 |                 hash[r] = add(hash[r],size(r->ch[i]));
      |                 ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc:31:31: error: reference to 'hash' is ambiguous
   31 |                 hash[r] = add(hash[r],size(r->ch[i]));
      |                               ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc:33:16: error: reference to 'hash' is ambiguous
   33 |         return hash[r];
      |                ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:21:22: note:                 'std::map<NODE*, long long int> hash'
   21 | map<NODE*,long long> hash;
      |                      ^~~~
a.cc: In function 'int dfs(NODE*, long long int)':
a.cc:65:1: warning: control reaches end of non-void function [-Wreturn-type]
   65 | }
      | ^