Language: C++

#include <bits/stdc++.h>
using namespace std;

struct st{
	st *linkk[27];
	st *up;
	int cnt;
	int belong;
	int att;
	st(){
		for (int i = 0; i < 27; i++){
			linkk[i] = NULL;
		}
		cnt = 0;
	}
};


int n;
int q;

#define MAX 400002

char buf[MAX];

vector<string> v;




st linkk2[MAX * 30];
int ord = 0;
st *neww(){
	ord++;
	return &linkk2[ord - 1];
}


st *root;
vector<st*> lis;

void ins(string &a){
	st *b = root;
	for (int i = 0; i < a.size(); i++){
		if (b->linkk[a[i] - 'a'] == NULL){
			b->linkk[a[i] - 'a'] = neww();
			b->linkk[a[i] - 'a']->up = b;
		}
		b = b->linkk[a[i] - 'a'];
	}
	lis.push_back(b);
}

inline void dfs(st *b){
	bool f = false;
	for (int i = 0; i < 26; i++){
		if (b->linkk[i] != NULL){
			f = true;
			dfs(b->linkk[i]);
			b->cnt += b->linkk[i]->cnt;
		}
	}
	if (f == false){
		b->cnt = 1;
	}
}
struct HL{
	vector<int> bit[26][26];
	st * pr;
	void resize(int b){
		for (int i = 0; i < 26; i++){
			for (int i1 = 0; i1 < 26; i1++){
				bit[i][i1].assign(b + 2, 0);
			}
		}
	}
	void add(int i, int J,int j,int k){
		bit[i][J][j] += k;
	}
	int sum(int i, int J,int j){
		return bit[i][J][j];
	}
	HL(st *pr_){
		pr = pr_;
	}
	void done(){
		for (int i = 0; i < 26; i++){
			for (int jj = 0; jj < 26; jj++){
				int siz = bit[i][jj].size();
				for (int j = 1; j < siz; j++){
					bit[i][jj][j] += bit[i][jj][j - 1];
				}
			}
		}
	}
};
vector<HL> hl;
inline void dfs2(st *b, int belong, int att){
	b->att = att;
	b->belong = belong;
	int id = -1;
	int mx = -1;
	for (int i = 0; i < 26; i++){
		if (b->linkk[i] != NULL){
			if (mx < b->linkk[i]->cnt){
				mx = b->linkk[i]->cnt;
				id = i;
			}
		}
	}
	if (mx == -1){
		hl[belong].resize(att);
		return;
	}
	for (int i = 0; i < 26; i++){
		if (b->linkk[i] != NULL){
			if (i == id){
				dfs2(b->linkk[i], belong, att + 1);
			}
			else{
				hl.push_back(HL(b));
				dfs2(b->linkk[i], hl.size() - 1, 0);
			}
		}
	}
}

inline void dfs3(st* b){
	int A;
	for (int i = 0; i < 26; i++){
		if (b->linkk[i] != NULL){
			if (b->linkk[i]->belong == b->belong){
				A = i;
			}
		}
	}
	for (int i = 0; i < 26; i++){
		if (b->linkk[i] != NULL){
			if (b->belong != b->linkk[i]->belong){
				hl[b->belong].add(A,i,b->att,b->linkk[i]->cnt);
			}
			dfs3(b->linkk[i]);
		}
	}
}

int gt(st *b){
	b = b->up;
	int r = 0;
	while (b != NULL){
		int belong = b->belong;
		int att = b->att;
		for (int i = 0; i < 26; i++){
			for (int j = 0; j <i; j++){
				r += hl[belong].sum(buf[i]-'a', buf[j]-'a',att);
			}
		}
		b = hl[belong].pr;
	}
	return r;
}

int main(){
	root = neww();
	root->up = NULL;
	cin >> n;
	for (int i = 0; i < n; i++){
		scanf("%s", buf);
		v.push_back(buf);
		ins(v.back());
	}
	dfs(root);
	hl.push_back(HL(NULL));
	dfs2(root, 0, 0);
	dfs3(root);
	for (int i = 0; i < hl.size(); i++){
		hl[i].done();
	}
	int q;
	scanf("%d", &q);
	while (q--){
		int id;
		scanf("%d", &id);
		id--;
		scanf("%s", buf);
		int ans = 0;
		ans += gt(lis[id]);
		ans++;
		printf("%d\n", ans);
	}
	return 0;
}


Compiler output:
/tmp/ccY0PNDe.o: in function `neww()':
a.cc:(.text+0x6): relocation truncated to fit: R_X86_64_PC32 against symbol `ord' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0xf): relocation truncated to fit: R_X86_64_PC32 against symbol `ord' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against symbol `ord' defined in .bss section in /tmp/ccY0PNDe.o
/tmp/ccY0PNDe.o: in function `ins(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
a.cc:(.text+0x4b): relocation truncated to fit: R_X86_64_PC32 against symbol `root' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x14e): relocation truncated to fit: R_X86_64_PC32 against symbol `lis' defined in .bss section in /tmp/ccY0PNDe.o
/tmp/ccY0PNDe.o: in function `gt(st*)':
a.cc:(.text+0x1c2): relocation truncated to fit: R_X86_64_PC32 against symbol `hl' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x22b): relocation truncated to fit: R_X86_64_PC32 against symbol `hl' defined in .bss section in /tmp/ccY0PNDe.o
/tmp/ccY0PNDe.o: in function `main':
a.cc:(.text+0x266): relocation truncated to fit: R_X86_64_PC32 against symbol `root' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x26d): relocation truncated to fit: R_X86_64_PC32 against symbol `root' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x351): relocation truncated to fit: R_X86_64_PC32 against symbol `root' defined in .bss section in /tmp/ccY0PNDe.o
a.cc:(.text+0x37e): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status