一、内容

We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by siThere is one cursor. The cursor's location ℓis denoted by an integer in {0,…,|s|}, with the following meaning: If ℓ=0, then the cursor is located before the first character of s.If ℓ=|s|, then the cursor is located right after the last character of sIf 0<ℓ<|s|, then the cursor is located between sℓ and sℓ+1We denote by sleftthe string to the left of the cursor and srightthe string to the right of the cursor.We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions: The Move action. Move the cursor one step to the right. This increments ℓonce.The Cut action. Set c←sright, then set s←sleft.The Paste action. Append the value of cto the end of the string s. Note that this doesn't modify cThe cursor initially starts at ℓ=0. Then, we perform the following procedure: Perform the Move action once.Perform the Cut action once.Perform the Paste action sℓtimes.If ℓ=x , stop. Otherwise, return to step 1.You're given the initial string s
and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 109+7.It is guaranteed that ℓ≤|s|at any time.

Input

The first line of input contains a single integer (1≤t≤1000) denoting the number of test cases. The next lines contain descriptions of the test cases.The first line of each test case contains a single integer x(1≤x≤106). The second line of each test case consists of the initial string s (1≤|s|≤500). It is guaranteed, that sconsists of the characters "1", "2", "3".It isguaranteed that the sum of xin a single file is at most 106. It is guaranteed that in each test case before the procedure will stop it will be true that ℓ≤|s|at any time.

Output

   For each test case, output a single line containing a single integer denoting the answer for that test case modulo 109+7

Input

4
5
231
7
2323
6
333
24
133321333

Output

25
1438
1101
686531475

二、思路

  • 通过字符串进行模拟就可以了, 当字符串长度超过所给的x,就不用往字符串后面添加字符了

三、代码

#include <iostream> 
using namespace std;
typedef long long ll;
const int MOD = 1e9 +7;
int t, x;
ll ans;
string s, h; 
int main() {
   	cin >> t;
   	while (t--) {
   		cin >> x >> s;
		ans = s.size();
		for (int i = 1; i <= x; i++) {
			int v = s[i - 1] - '1';
			if (s.size() < x) {
				if(v) h = s.substr(i); //避免多次截取 造成超时
				for (int j = 1; j <= v; j++) s += h;
			} 
			//可能i比ans大 所以我们的ans 应该加上一个MOD保证非负 
			ans = (ans + (ans - i) * v + MOD) % MOD;
		}	
		cout << ans % MOD << endl;
	}
    return 0;
}