A. Cut and Paste

We start with a string ss consisting only of the digits 11, 22, or 33. The length of ss is denoted by |s||s|. For each ii from 11 to |s||s|, the ii-th character of ss is denoted by sisi.

There is one cursor. The cursor's location ℓ is denoted by an integer in {0,,|s|}{0,…,|s|}, with the following meaning:

  • If =0ℓ=0, then the cursor is located before the first character of ss.
  • If =|s|ℓ=|s|, then the cursor is located right after the last character of ss.
  • If 0<<|s|0<ℓ<|s|, then the cursor is located between ssℓ and s+1sℓ+1.

We denote by sleftsleft the string to the left of the cursor and srightsright the string to the right of the cursor.

We also have a string cc, 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 csrightc←sright, then set sslefts←sleft.
  • The Paste action. Append the value of cc to the end of the string ss. Note that this doesn't modify cc.

The cursor initially starts at =0ℓ=0. Then, we perform the following procedure:

  1. Perform the Move action once.
  2. Perform the Cut action once.
  3. Perform the Paste action ssℓ times.
  4. If =xℓ=x, stop. Otherwise, return to step 1.

You're given the initial string ss and the integer xx. What is the length of ss when the procedure stops? Since this value may be very large, only find it modulo 109+7109+7.

It is guaranteed that |s|ℓ≤|s| at any time.

Input

The first line of input contains a single integer tt (1t10001≤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 xx (1x1061≤x≤106). The second line of each test case consists of the initial string ss (1|s|5001≤|s|≤500). It is guaranteed, that ss consists of the characters "1", "2", "3".

It is guaranteed that the sum of xx in a single file is at most 106106. It is guaranteed that in each test case before the procedure will stop it will be true that |s|ℓ≤|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+7109+7.

Example
input
Copy
4
5
231
7
2323
6
333
24
133321333
output
Copy
25
1438
1101
686531475
Note

Let's illustrate what happens with the first test case. Initially, we have s=s= 231. Initially, =0ℓ=0 and c=εc=ε (the empty string). The following things happen if we follow the procedure above:

 

  • Step 1, Move once: we get =1ℓ=1.
  • Step 2, Cut once: we get s=s= 2 and c=c= 31.
  • Step 3, Paste s=sℓ= 2 times: we get s=s= 23131.
  • Step 4: =1x=5ℓ=1≠x=5, so we return to step 1.

     

  • Step 1, Move once: we get =2ℓ=2.
  • Step 2, Cut once: we get s=s= 23 and c=c= 131.
  • Step 3, Paste s=sℓ= 3 times: we get s=s= 23131131131.
  • Step 4: =2x=5ℓ=2≠x=5, so we return to step 1.

     

  • Step 1, Move once: we get =3ℓ=3.
  • Step 2, Cut once: we get s=s= 231 and c=c= 31131131.
  • Step 3, Paste s=sℓ= 1 time: we get s=s= 23131131131.
  • Step 4: =3x=5ℓ=3≠x=5, so we return to step 1.

     

  • Step 1, Move once: we get =4ℓ=4.
  • Step 2, Cut once: we get s=s= 2313 and c=c= 1131131.
  • Step 3, Paste s=sℓ= 3 times: we get s=s= 2313113113111311311131131.
  • Step 4: =4x=5ℓ=4≠x=5, so we return to step 1.

     

  • Step 1, Move once: we get =5ℓ=5.
  • Step 2, Cut once: we get s=s= 23131 and c=c= 13113111311311131131.
  • Step 3, Paste s=sℓ= 1 times: we get s=s= 2313113113111311311131131.
  • Step 4: =5=xℓ=5=x, so we stop.

At the end of the procedure, ss has length 2525.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9+7;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        ll x,l=0;
        string s;
        cin >>x>> s;
        ll res = 0;
        while (s.size() < x)
        {
            if (s[l] == '2')
                s += s.substr(l+1);
            else if (s[l] == '3')
                s += s.substr(l+1) + s.substr(l+1);
            l++;
        }
        res = s.size();
        while (l < x)
        {
            res += ((res - l-1+mod)%mod)*(s[l] - '1') % mod;
            res %= mod;
            l++;
        }
        cout << res << endl;
    }
    return 0;
}

 

posted @ 2020-05-05 17:24  DeaL57  阅读(121)  评论(0编辑  收藏  举报