C. Mark and His Unfinished Essay

One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.

More formally, the prompt is a string ss of initial length nn. Mark will perform the copy-pasting operation cc times. Each operation is described by two integers ll and rr, which means that Mark will append letters slsl+1…srslsl+1…sr to the end of string ss. Note that the length of ss increases after this operation.

Of course, Mark needs to be able to see what has been written. After copying, Mark will ask qq queries: given an integer kk, determine the kk-th letter of the final string ss.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains three integers nn, cc, and qq (1≤n≤2⋅1051≤n≤2⋅105, 1≤c≤401≤c≤40, and 1≤q≤1041≤q≤104) — the length of the initial string ss, the number of copy-pasting operations, and the number of queries, respectively.

The second line of each test case contains a single string ss of length nn. It is guaranteed that ss only contains lowercase English letters.

The following cc lines describe the copy-pasting operation. Each line contains two integers ll and rr (1≤l≤r≤10181≤l≤r≤1018). It is also guaranteed that rr does not exceed the current length of ss.

The last qq lines of each test case describe the queries. Each line contains a single integer kk (1≤k≤10181≤k≤1018). It is also guaranteed that kk does not exceed the final length of ss.

It is guaranteed that the sum of nn and qq across all test cases does not exceed 2⋅1052⋅105 and 104104, respectively.

Output

For each query, print the kk-th letter of the final string ss.

Example

input

Copy

2
4 3 3
mark
1 4
5 7
3 8
1
10
12
7 3 3
creamii
2 3
3 4
2 9
9
11
12

output

Copy

m
a
r
e
a
r

#include <climits>
#include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#define ll long long
using namespace std;
typedef pair <double , double> PII;
const int inf = 0x3f3f3f3f;
const int N = 45;
int T;
ll l[N], r[N]; 
ll len[N];
int main()
{
	ios::sync_with_stdio(false);
	cin >> T;
	while (T--)
	{

		int n, c, q;
		cin >> n >> c >> q;
		string s;
		string d=" ";
		cin >> s;
		d += s;
		len[0] = s.size();
		for (int i = 1; i <= c; i++) {  
			cin >> l[i] >> r[i];                  //记录左右区间
			len[i] = len[i - 1] + r[i] - l[i]+1;  //算出每一次复制粘贴后的长度,
		}

		while (q--)
		{
			ll k;
			cin >> k;
			for (int i = c; i > 0 ; i--)   //从后面往前推,算出上一次复制跟k字符相同的长度
			{
				if (len[i - 1] < k && k <= len[i]) {
					k = k - len[i - 1] + l[i] - 1;
				}
			}
			cout << d[k]<<endl;  //直到推到原字符串时停止,输出
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值