#include <bits/stdc++.h>
#include <bitset>
#include <chrono>
#include <iomanip>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long double ld;
#define prq priority_queue<ll>;

ll MOD=1000000007;
#define PI 3.1415926535897932384626433832795
#define llMAX 1000000000000000008
#define llMIN -1000000000000000008

#define forx(i,j,n) for(int i=j;i<n;i++)
#define fory(i,j,n) for(int i=j;i>=n;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false)
#define testcase int t; cin>>t
#define MEM(xx,yy) memset(xx,yy,sizeof(xx))
#define vbs(xx,x) binary_search(all(xx),x)
#define vlb(xx,x) lower_bound(all(xx),x)
#define vub(xx,x) upper_bound(all(xx),x)

#define inpi(xx) scanf("%d",&xx)
#define inpl(xx) scanf("%lld",&xx)
#define inpc(xx) scanf("%c",&xx)
#define inps(xx) scanf("%s",&xx)
#define pri(xx) printf("%d",xx)
#define prl(xx) printf("%lld",xx)
#define prls(xx) printf("%lld ",xx)
#define prle(xx) printf("%lld\n",xx)
#define prc(xx) printf("%c",xx)
#define prs printf(" ")
#define pre printf("\n")
#define flush fflush(stdout)

ll mul(ll mul_1,ll mul_2){ ll mul_t=((mul_1%MOD)*(mul_2%MOD)+MOD)%MOD; return mul_t; }
ll mul3(ll mul_1,ll mul_2,ll mul_3){ ll mul_t=mul(mul(mul_1,mul_2),mul_3)%MOD; return mul_t; }
ll add(ll add_1,ll add_2){ ll add_t=((add_1%MOD)+(add_2%MOD)+MOD+MOD)%MOD; return add_t; }
ll sub(ll sub_1,ll sub_2){ ll sub_r=1e15; ll sub_t=(((sub_1+sub_r)%MOD)-((sub_2+sub_r)%MOD)+sub_r)%MOD; return sub_t; }
ll power(ll x,ll y,ll p){ ll res=1; x=x%p; while(y>0){ if(y&1) res=(res*x)%p; y=y/2; x=(x*x)%p; } return res; }
ll gcd(ll num1,ll num2){ return (num2?gcd(num2,num1%num2):num1); }
ll lcm(ll num1,ll num2){ return 1LL*((num1*num2)/__gcd(num1,num2)); }
ll countbit(ll num){ return 1LL*(__builtin_popcountll(num)); }
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
ll zerotrail(ll num){ return 1LL*(__builtin_ctzll(num)); }
ll zerobegin(ll num){ return 1LL*(__builtin_clzll(num)); }

clock_t tim;
void showtime(){ tim=clock()-tim; cout<<" ===> "<<(ld)tim/CLOCKS_PER_SEC<<" seconds\n"; }

ll prime[1000005];
void sieve(){ //spf
    forx(i,0,1000005) prime[i]=i;
    for(ll p=2; 1LL*p*p<1000005; p++){ if(prime[p]==p) { for(ll i=1LL*p*p; i<1000005; i+=p) if(prime[i]==i) prime[i]=p; } }
    return;
}
bool isPrime(ll pri){
    if(pri==1||pri==0) return 0;
    if(prime[pri]==pri) return 1;
    return 0;
}
vl alldiv(ll num){
    vl prDiv; for(ll i=1; 1LL*i*i<=num; i++){ if(num%i==0){ prDiv.pb(i); if(i!=(num/i)) prDiv.pb(num/i); } }
    sort(all(prDiv)); return prDiv;
}
vl prdiv(ll num){
    vl prDiv; while(num!=1){ prDiv.pb(prime[num]); ll prtemp=prime[num]; while(num%prtemp==0) num/=prtemp; }
    return prDiv;
}

int main() {
    fastio; //tim=clock();
    testcase;
    //ll t=1;
    forx(T,1,1+t){
        ll x,e=0;
        cin>>x;
        ll dp[x+2];
        string str;
        cin>>str;
        vl c;
        ll a=str[0]-'0';
        ll n=str.size();
        dp[0]=n;
        forx(i,0,n) c.pb(str[i]-'0');
        forx(i,1,x+2){
            dp[i]=(dp[i-1]+((c[i-1]-1ll)*(dp[i-1]-(ll)i))%MOD)%MOD;
            if(c.size()<x+2){
                ll m=c.size();
                forx(ii,1,c[i-1]){
                    ll j=i;
                    while(j<m){
                        c.pb(c[j]);
                        j++;
                        if(c.size()==x+2) break;
                    }
                }
            }
        }
        cout<<(dp[x]+MOD)%MOD<<"\n";
    }
	return 0;
}


#include <bits/stdc++.h>
#include <bitset>
#include <chrono>
#include <iomanip>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long double ld;
#define prq priority_queue<ll>;

ll MOD=1000000007;
#define PI 3.1415926535897932384626433832795
#define llMAX 1000000000000000008
#define llMIN -1000000000000000008

#define forx(i,j,n) for(int i=j;i<n;i++)
#define fory(i,j,n) for(int i=j;i>=n;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false)
#define testcase int t; cin>>t
#define MEM(xx,yy) memset(xx,yy,sizeof(xx))
#define vbs(xx,x) binary_search(all(xx),x)
#define vlb(xx,x) lower_bound(all(xx),x)
#define vub(xx,x) upper_bound(all(xx),x)

#define inpi(xx) scanf("%d",&xx)
#define inpl(xx) scanf("%lld",&xx)
#define inpc(xx) scanf("%c",&xx)
#define inps(xx) scanf("%s",&xx)
#define pri(xx) printf("%d",xx)
#define prl(xx) printf("%lld",xx)
#define prls(xx) printf("%lld ",xx)
#define prle(xx) printf("%lld\n",xx)
#define prc(xx) printf("%c",xx)
#define prs printf(" ")
#define pre printf("\n")
#define flush fflush(stdout)

ll mul(ll mul_1,ll mul_2){ ll mul_t=((mul_1%MOD)*(mul_2%MOD)+MOD)%MOD; return mul_t; }
ll mul3(ll mul_1,ll mul_2,ll mul_3){ ll mul_t=mul(mul(mul_1,mul_2),mul_3)%MOD; return mul_t; }
ll add(ll add_1,ll add_2){ ll add_t=((add_1%MOD)+(add_2%MOD)+MOD+MOD)%MOD; return add_t; }
ll sub(ll sub_1,ll sub_2){ ll sub_r=1e15; ll sub_t=(((sub_1+sub_r)%MOD)-((sub_2+sub_r)%MOD)+sub_r)%MOD; return sub_t; }
ll power(ll x,ll y,ll p){ ll res=1; x=x%p; while(y>0){ if(y&1) res=(res*x)%p; y=y/2; x=(x*x)%p; } return res; }
ll gcd(ll num1,ll num2){ return (num2?gcd(num2,num1%num2):num1); }
ll lcm(ll num1,ll num2){ return 1LL*((num1*num2)/__gcd(num1,num2)); }
ll countbit(ll num){ return 1LL*(__builtin_popcountll(num)); }
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
ll zerotrail(ll num){ return 1LL*(__builtin_ctzll(num)); }
ll zerobegin(ll num){ return 1LL*(__builtin_clzll(num)); }

clock_t tim;
void showtime(){ tim=clock()-tim; cout<<" ===> "<<(ld)tim/CLOCKS_PER_SEC<<" seconds\n"; }

ll prime[1000005];
void sieve(){ //spf
    forx(i,0,1000005) prime[i]=i;
    for(ll p=2; 1LL*p*p<1000005; p++){ if(prime[p]==p) { for(ll i=1LL*p*p; i<1000005; i+=p) if(prime[i]==i) prime[i]=p; } }
    return;
}
bool isPrime(ll pri){
    if(pri==1||pri==0) return 0;
    if(prime[pri]==pri) return 1;
    return 0;
}
vl alldiv(ll num){
    vl prDiv; for(ll i=1; 1LL*i*i<=num; i++){ if(num%i==0){ prDiv.pb(i); if(i!=(num/i)) prDiv.pb(num/i); } }
    sort(all(prDiv)); return prDiv;
}
vl prdiv(ll num){
    vl prDiv; while(num!=1){ prDiv.pb(prime[num]); ll prtemp=prime[num]; while(num%prtemp==0) num/=prtemp; }
    return prDiv;
}

int main() {
    fastio; //tim=clock();
    testcase;
    //ll t=1;
    forx(T,1,1+t){
        ll x,e=0;
        cin>>x;
        ll dp[x+2];
        string str;
        cin>>str;
        vl c;
        ll a=str[0]-'0';
        ll n=str.size();
        dp[0]=n;
        forx(i,0,n) c.pb(str[i]-'0');
        forx(i,1,x+2){
            dp[i]=(dp[i-1]+((c[i-1]-1ll)*(dp[i-1]-(ll)i))%MOD)%MOD;
            if(c.size()<x+2){
                forx(ii,1,c[i-1]){
                    ll j=i;
                    ll m=c.size();
                    while(j<m){
                        c.pb(c[j]);
                        j++;
                        if(c.size()==x+2) break;
                    }
                }
            }
        }
        cout<<(dp[x]+MOD)%MOD<<"\n";
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
int mod=1000000007;
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=1;i<=x;i++)
        {
            ans=((shu[i-1]-'0')*(ans-i)+i+2*mod)%1000000007;
        }
        cout<<ans<<endl;;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
const int MOD=1000000007;
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=1;i<=x;i++)
        {
            ans+=((shu[i-1]-'0')*(ans-i+MOD)+i);
        }

        cout<<ans%MOD<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
int mod=1000000007;
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=0;i<x;i++)
        {
             ans=(ans+(shu[i]-'0'-1)*int(ans-i-1)+2*mod)%mod;;
        }
        cout<<ans<<endl;;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
int mod=1000000007;
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=0;i<x;i++)
        {
             ans=(ans+(shu[i]-'0'-1)*int(ans-i-1))%mod;;
        }
        cout<<ans<<endl;;
    }
    return 0;
}


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

int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;

        long long int ans=shu.length();
        guangbiao=0;
        while(guangbiao<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }

        for(int i=1;i<=x;i++)
        {
            ans=((shu[i-1]-'0')*(ans-i)+i)%1000000007;
        }
        cout<<ans<<endl;;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
char shu[1000000];
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        char shu[1000000];
        scanf("%d",&x);
        scanf("%s",shu);
        long long int ans=strlen(shu);
        guangbiao=0;
        while(strlen(shu)<50)
        {
            Len=strlen(shu);
            int i=Len;
            int nn=shu[guangbiao]-'0';
            while(--nn)
            {
                for(int j=guangbiao+1;j<Len;j++)
                {
                    shu[i++]=shu[j];
                    if(i>=50)
                    {
                        break;
                    }
                }
                //printf("%s\n",shu[T]);
            }
            guangbiao++;
        }
        //printf("%s\n",shu);
        for(int i=1;i<=x;i++)
        {
            ans=((shu[i-1]-'0')*(ans-i)+i)%1000000009;
        }
        cout<<ans<<endl;
        //memset(shu,'\0',sizeof(shu));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int x;
const int MOD=1000000007;
int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=1;i<=x;i++)
        {
            ans+=((shu[i-1]-'0')*(ans-i)+i);
        }

        cout<<ans%MOD<<endl;
    }
    return 0;
}


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

int Len;
int guangbiao;
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string shu;
        scanf("%d",&x);
        cin>>shu;
        long long int ans=shu.length();
        guangbiao=0;
        while(shu.length()<x)
        {
            Len=shu.length();
            int nn=shu[guangbiao]-'0';
            string sub=shu.substr(guangbiao+1,Len-1);
            while(--nn)
            {
                shu+=sub;

                    if(shu.length()>=x)
                    {
                        break;
                    }
            }
            guangbiao++;
        }
        for(int i=1;i<=x;i++)
        {
            ans=((shu[i-1]-'0')*(ans-i)+i)%1000000007;
        }
        cout<<ans<<endl;;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

int add(ll a, ll b) {return (a+b)%M;}
int mult(ll a, ll b) {return (a*b)%M;}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
    	int x, len = 0;
    	string s;
    	cin >> x >> s;
    	int l = 1;
    	while(x > s.size()) {
    		if(l == s.size()) break;
    		int rep = s[l-1]-'1';
    		if(rep) {
				string nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		++l;
    	}
    	len = s.size();
    	while(l <= x) {
    		if(l-1 == s.size()) break;
    		int to_add = len-l;
    		int rep = s[l-1]-'1';
    		len = add(len, mult(to_add, rep));
    		++l;
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

int add(ll a, ll b) {return (a+b)%M;}
int sub(ll a, ll b) {return (a+M-b)%M;}
int mult(ll a, ll b) {return (a*b)%M;}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
    	int x, len = 0;
    	string s;
    	cin >> x >> s;
    	int l = 0;
    	while(1) {
    		++l;
    		int rep = s[l-1]-'1';
    		if(rep) {
				string nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		if(l == x || x < s.size()) break;
    	}
    	len = s.size();
    	if(l != x) {
	    	while(1) {
	    		++l;
	    		int to_add = sub(len,l);
	    		int rep = s[l-1]-'1';
	    		len = add(len, mult(to_add, rep));
	    		if(l == x) break;
	    	}
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

int add(ll a, ll b) {return (a+b)%M;}
int mult(ll a, ll b) {return (a*b)%M;}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
    	int x, len = 0;
    	string s;
    	cin >> x >> s;
    	int l = 0;
    	while(1) {
    		++l;
    		int rep = s[l-1]-'1';
    		if(rep) {
				string nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		if(l == x || x < s.size()) break;
    	}
    	len = s.size();
    	if(l != x) {
	    	while(1) {
	    		++l;
	    		int to_add = len-l;
	    		int rep = s[l-1]-'1';
	    		len = add(len, mult(to_add, rep));
	    		if(l == x) break;
	    	}
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

struct mint {
    int x;
    mint() {}
    mint(int _x) {
        x = _x % M;
    }

    mint operator+(const mint& oth) const {return mint(x + oth.x);}
    mint operator-(const mint& oth) const {return mint(x + M - oth.x);}
    mint operator*(const mint& oth) const {return mint( ((ll)x * oth.x) % M );}
    mint operator^(int power) const {
        mint res(1);
        mint y = *this;
        while(power) {
            if(power & 1) res = res*y;
            y = y*y;
            power >>= 1;
        }
        return res;
    }
    mint operator/(mint oth) const {return (*this) * oth^(M-2);}
    bool operator==(const mint& oth) const {return x == oth.x;}
    bool operator!=(const mint& oth) const {return !(*this == oth);}
    bool operator<(const mint& oth) const {return x < oth.x;}
    bool operator<=(const mint& oth) const {return *this < oth || *this == oth;}
    bool operator>(const mint& oth) const {return !(*this <= oth);}
    bool operator>=(const mint& oth) const {return !(*this < oth);}

    friend ostream& operator<<(ostream& os, mint& x);
    friend istream& operator>>(istream& is, mint& x);
};

ostream& operator<<(ostream& os, mint& x) {
    os << x.x;
    return os;
}

istream& operator>>(istream& is, mint& x) {
    is >> x.x;
    return is;
}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
        mint x, len(0);
    	string s;
    	cin >> x >> s;
    	int l = 0;
    	while(1) {
    		++l;
    		int rep = s[l-1]-'1';
    		if(rep) {
				string nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		if(mint(l) == x || x < s.size()) break;
    	}
    	len = s.size();
    	if(mint(l) != x) {
	    	while(1) {
	    		++l;
	    		mint to_add = len - l;
	    		mint rep = s[l-1]-'1';
                len = len + (to_add * rep);
	    		if(mint(l) == x) break;
	    	}
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

int add(ll a, ll b) {return (a+b)%M;}
int mult(ll a, ll b) {return (a*b)%M;}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
    	int x, len = 0;
    	string s;
    	cin >> x >> s;
    	int l = 1;
    	while(x > s.size()) {
    		if(l == s.size()) break;
    		int rep = s[l-1]-'1';
    		if(rep) {
				string nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		++l;
    	}
    	len = s.size();
    	while(l <= x) {
    		if(l == s.size()) break;
    		int to_add = len-l;
    		int rep = s[l-1]-'1';
    		len = add(len, mult(to_add, rep));
    		++l;
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;

/////////////////////////////////////////////////////////////////////

const int M = 1e9+7;

int add(ll a, ll b) {return (a+b)%M;}
int mult(ll a, ll b) {return (a*b)%M;}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    int t;
    cin >> t;
    while(t--) {
    	int x, len = 0;
    	string s, nxt;
    	cin >> x >> s;
    	int l = 1;
    	while(x > s.size()) {
    		int rep = s[l-1]-'1';
    		if(rep) {
	    		nxt = s.substr(l);
	    		REP(_, rep) s += nxt;
    		}
    		++l;
    	}
    	len = s.size();
    	while(l <= x) {
    		int to_add = len-l;
    		int rep = s[l-1]-'1';
    		len = add(len, mult(to_add, rep));
    		++l;
    	}
    	cout << len << endl;
    }

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function (WA)


#include<stdio.h>
#include<math.h>
#include<string.h>
char t[1000005];
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f,len,m;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{
  char s[505];

scanf("%lld",&x);
   scanf("%s",s);
   len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];
    k=len;
    m=k;
   for(l=0;l<x&&k<=x;l++)
   {
     if(t[l]=='2')
     { for(y=l+1;y<m;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<m;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<m;y++)
          {t[k]=t[y];k++;}
        }
        m=k;

   }

   //printf("%s\n",t);
   z=strlen(s);

   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      z= ((z%mod)+(z%mod-j)%mod)%mod;
      if(t[j-1]=='3')
      z=(z%mod+(((z%mod-j)%mod)*2)%mod)%mod;

   }
   if(z<=0)printf("%lld\n",mod+z);
   else printf("%lld\n",z);
}
return 0;}


#include<stdio.h>
#include<math.h>
#include<string.h>
char t[2000005];
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f,len,m;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{
  char s[505];

scanf("%lld",&x);
   scanf("%s",s);
   len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];
    k=len;
    m=k;
   for(l=0;l<x&&k<=x;l++)
   {
     if(t[l]=='2')
     { for(y=l+1;y<m;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<m;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<m;y++)
          {t[k]=t[y];k++;}
        }
        m=k;

   }

   //printf("%s\n",t);
   z=strlen(s);

   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      z= ((z%mod)+(z%mod-j)%mod)%mod;
      if(t[j-1]=='3')
      z=(z%mod+(((z%mod-j)%mod)*2)%mod)%mod;

   }
   if(z<=0)printf("%lld\n",mod+z);
   else printf("%lld\n",z);
}
return 0;}


#include<stdio.h>
#include<math.h>
#include<string.h>
char s[505];
char t[1000005];
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  scanf("%lld",&x);
   scanf("%s",s);
   long long len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=1000000;l++)
   {  k=0;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[len+k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[len+k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[len+k]=t[y];k++;}

       }
       len=strlen(t);

   }
   ans=1;

   len=strlen(s);
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   printf("%lld\n",ans);
}
return 0;}


#include<iostream>
#include<string.h>

long long mod=1e9+7;
using namespace std;
string s;
string t;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<1000000;l++)
   {  k=len;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[k]=t[y];k++;}

       }
       len=t.size();

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<stdio.h>
#include<math.h>
#include<string.h>
char t[10000000];
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f,len,m;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{
  char s[505];

scanf("%lld",&x);
   scanf("%s",s);
   len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];
    k=len;
    m=k;
   for(l=0;l<x&&k<=x;l++)
   {
     if(t[l]=='2')
     { for(y=l+1;y<m;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<m;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<m;y++)
          {t[k]=t[y];k++;}
        }
        m=k;

   }

   //printf("%s\n",t);
   z=strlen(s);

   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      z= ((z%mod)+(z%mod-j)%mod)%mod;
      if(t[j-1]=='3')
      z=(z%mod+(((z%mod-j)%mod)*2)%mod)%mod;

   }
   if(z<=0)printf("%lld\n",mod+z);
   else printf("%lld\n",z);
}
return 0;}


#include<iostream>
#include<string.h>

long long mod=1e9+7;
using namespace std;
string s;
string t;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=1000000;l++)
   {  k=0;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[len+k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[len+k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[len+k]=t[y];k++;}

       }
       len=strlen(t.c_str());

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<iostream>
#include<string>

string s;
string t;

long long mod=1e9+7;
using namespace std;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=1000000;l++)
   {  k=0;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[len+k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[len+k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[len+k]=t[y];k++;}

       }
       len=strlen(t.c_str());

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<iostream>
#include<string.h>

long long mod=1e9+7;
using namespace std;
string s;
string t;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=5000;l++)
   {  k=len;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[k]=t[y];k++;}

       }
       len=strlen(t.c_str());

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<iostream>
#include<string.h>

long long mod=1e9+7;
using namespace std;
string s;
string t;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=1000000;l++)
   {  k=len;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[k]=t[y];k++;}

       }
       len=strlen(t.c_str());

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<stdio.h>
#include<math.h>
#include<string.h>
char t[1000005];
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f,len,m;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{
  char s[505];

scanf("%lld",&x);
   scanf("%s",s);
   len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];
    k=len;
    m=k;
   for(l=0;l<x&&k<=x;l++)
   {
     if(t[l]=='2')
     { for(y=l+1;y<m;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<m;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<m;y++)
          {t[k]=t[y];k++;}
        }
        m=k;

   }

   //printf("%s\n",t);
   z=strlen(s);

   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      z= ((z%mod)+(z%mod-j)%mod)%mod;
      if(t[j-1]=='3')
      z=(z%mod+(((z%mod-j)%mod)*2)%mod)%mod;

   }
   printf("%lld\n",z);
}
return 0;}


#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
string t;
long long mod=1e9+7;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f,len,m;
  cin>>q;
    for(i=0;i<q;i++)
{
  string s;

    cin>>x>>s;

   len=s.size();
   z=s.size();
  for(j=0;j<len;j++)
  t[j]=s[j];
    k=len;
    m=k;
   for(l=0;l<x&&k<=x;l++)
   {
     if(t[l]=='2')
     { for(y=l+1;y<m;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<m;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<m;y++)
          {t[k]=t[y];k++;}
        }
        m=k;

   }

   //printf("%s\n",t);

   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      z= ((z%mod)+(z-j)%mod)%mod;
      if(t[j-1]=='3')
      z=(z%mod+(((z-j)%mod)*2)%mod)%mod;

   }
   cout<<z<<endl;
}
return 0;}


#include<iostream>
#include<string.h>

string s;
string t;

long long mod=1e9+7;
using namespace std;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s.c_str());
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<=1000000;l++)
   {  k=0;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[len+k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[len+k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[len+k]=t[y];k++;}

       }
       len=strlen(t.c_str());

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<iostream>
#include<string.h>

long long mod=1e9+7;
using namespace std;
string s;
string t;
int main()
{ long long count,n,q,i,j,k,x,y,z,l,ans,max,f;
  scanf("%lld",&q);
    for(i=0;i<q;i++)
{  cin>>x;
   cin>>s;
   long long len=strlen(s);
  for(j=0;j<len;j++)
  t[j]=s[j];

   for(l=0;l<x&&len<1000000;l++)
   {  k=len;
     if(t[l]=='2')
     { for(y=l+1;y<len;y++)
          { t[k]=t[y];k++;  }      }
      if(t[l]=='3')
      {for(y=l+1;y<len;y++)
           {t[k]=t[y];k++;}
        for(y=l+1;y<len;y++)
          {t[k]=t[y];k++;}

       }
       len=strlen(t);

   }
   ans=1;

   len=strlen(s.c_str());
   z=len;
   //printf("%s\n",t);
   for(j=1;j<=x;j++)
   {
     if(t[j-1]=='2')
      ans= z+(z-j);
      if(t[j-1]=='3')
      ans=z+(z-j)*2;

       z=ans;
      ans=ans%mod;

   }
   cout<<x<<endl;
}
return 0;}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		ll len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
			if(f[i]>3){puts("fdhjdfdw");return 0;}
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>x){
						ok=0;break;
					}
					if(i+len>1000000){
						puts("akk");return 0;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
					if(f[i]>3){puts("fdhjdfdw");return 0;}
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		int len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
			//printf("f[%d]=%d ",i,f[i]);
		}//初始化!
		while(len<=x){//计算x内的数字即可
			int inc=(len-1)*(f[pos]-1);//计算我们当前需要新添的长度
			if(inc==0){//如果没有新增长度,我们就跳过,计算下一个
				pos++;
			}
			else {//如果有新增长度
				for(int i=1;i<=inc;i++){
					if(i+len>x)break;//如果我们把x之前的f值都算了,我们的任务做完了
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
					//printf("f[%d]=%d ",i+len,f[i+len]);
				}
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		//puts("");
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			//printf("temp=%lld\n",temp);
			ans=(ans+temp)%mod;
			//printf("ans=%lld\n",ans);
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		int len=strlen(s);//起始长度
		int pos=1;//起始位置
		memset(f,0,sizeof(f));
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
			if(f[i]>3){puts("fdhjdfdw");return 0;}
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>x){
						ok=0;break;
					}
					if(i+len>1000000){
						puts("akk");return 0;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
					if(f[i]>3){puts("fdhjdfdw");return 0;}
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		ll len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>x){
						ok=0;break;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		ll len=strlen(s);//起始长度
		int pos=1;//起始位置
		memset(f,0,sizeof(f));
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>x){
						ok=0;break;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		int len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>x){
						ok=0;break;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		int len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
			//printf("f[%d]=%d ",i,f[i]);
		}//初始化!
		while(len<=x){//计算x内的数字即可
			int inc=(len-pos)*(f[pos]-1);//计算我们当前需要新添的长度
			if(inc==0){//如果没有新增长度,我们就跳过,计算下一个
				pos++;
			}
			else {//如果有新增长度
				for(int i=1;i<=inc;i++){
					if(i+len>x)break;//如果我们把x之前的f值都算了,我们的任务做完了
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
					//printf("f[%d]=%d ",i+len,f[i+len]);
				}
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		//puts("");
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			//printf("temp=%lld\n",temp);
			ans=(ans+temp)%mod;
			//printf("ans=%lld\n",ans);
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		ll len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc==0){
				pos++;
			}
			else {
				for(int i=1;i<=inc;i++){
					if(i+len>x)break;
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
				}
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include<cstring>
#include<stdio.h>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
#include<iostream>
#define FIN freopen("D://debug//in.txt","r",stdin);
#define FOUT freopen("D://debug//out.txt","w",stdout);
#define FDATA freopen("D://debug//data.txt","w",stdout);
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int f[maxn];
char s[505];
int main(){
	//FIN
	int t,x;cin>>t;
	while(t--){
		cin>>x>>s;
		ll len=strlen(s);//起始长度
		int pos=1;//起始位置
		for(int i=1;i<=len;i++){
			f[i]=s[i-1]-'0';
			if(f[i]>3){puts("fdhjdfdw");return 0;}
		}
		while(len<=x){
			int inc=(len-pos)*(f[pos]-1);
			if(inc>1000000){
				puts("nonono");return 0;
			}
			if(inc==0){
				pos++;
			}
			else {
				bool ok=1;
				for(int i=1;i<=inc;i++){
					if(i+len>1000000){
						puts("akk");return 0;
					}
					if(i+len>x){
						ok=0;break;
					}
					f[i+len]=i%(len-pos)==0?f[len]:f[pos+i%(len-pos)];
					if(f[i]>3){puts("fdhjdfdw");return 0;}
				}
				if(ok==0)break;
				len+=inc;
				pos++;
			}
		}
		ll ans=strlen(s);
		for(int i=1;i<=x;i++){
			ll temp=(((ans-i+mod)%mod)*(f[i]-1))%mod;
			ans=(ans+temp)%mod;
		}
		cout<<ans<<endl;
	}
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	sort(path.begin(), path.end());
	if (abs(path[n - 1] - p) > abs(path[0] - p))
		path.pop_back();
	else
		path.pop_front();
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	long long sum = 0;
	for (int i = 1; i < n - 1; i++)
		sum += abs(path[i] - path[i - 1]);
	cout << sum + min(abs(path[0] - p), abs(path[n - 2] - p));
	cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0,x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	sort(path.begin(), path.end());
	int del = 0;
	if (abs(path[n - 1] - p) > abs(path[0] - p))
		del = n - 1;
	if (del)
		path.pop_back();
	else
		path.pop_front();
	long long sum = 0;
	for (int i = 1; i < n - 1; i++)
		sum += abs(path[i] - path[i - 1]);
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	int pos = 0;
	long long cursum = 0;
	while (pos < n - 1 && p > path[pos])
	{
		pos++;
		if (pos > 1)
			cursum += abs(path[pos - 2] - path[pos - 1]);
	}
	if (pos != 0 && pos != n - 1)
	{
		cursum += abs(path[pos] - p);
		sum += min(sum - cursum, cursum);
	}
	cout << sum;
		cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	path.push_back(p);
	sort(path.begin(), path.end());
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	if (path[n] == p)
	{
		cout << abs(path[n] - path[1]);
		return 0;
	}
	if (path[0] == p)
	{
		cout << abs(p - path[n - 1]);
		return 0;
	}
	int m = 0;
	int size = abs(path[0] - path[n]);
	if (abs(path[n] - p) > abs(path[0] - p))
		path.pop_back();
	else
		path.pop_front();
	m = abs(path[n - 1] - p) + size;
	m = min(m, abs(path[0] - p)) + size;
	cin >> n;
	return 0;


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	sort(path.begin(), path.end());
	if (abs(path[n - 1] - p) > abs(path[0] - p))
		path.pop_back();
	else
		path.pop_front();
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	cout << path[n-2] - path[0] + min(abs(path[0] - p), abs(path[n - 2] - p));
	cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	sort(path.begin(), path.end());
	if (abs(path[n - 1] - p) > abs(path[0] - p))
		path.pop_back();
	else
		path.pop_front();
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	if (path[n - 1] == p)
	{
		cout << abs(path[n - 1] - path[1]);
		return 0;
	}
	if (path[n - 2] == p)
	{
		cout << abs(path[n - 2] - path[0]);
		return 0;
	}
	if (path[0] == p)
	{
		cout << abs(p - path[n - 2]);
		return 0;
	}
	if (path[1] == p)
	{
		cout << abs(p - path[n - 1]);
		return 0;
	}
	cout << path[n-2] - path[0] + min(abs(path[0] - p), abs(path[n - 2] - p));
	cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	path.push_back(p);
	sort(path.begin(), path.end());
	if (path[n] == p)
	{
		cout << abs(path[n] - path[1]);
		return 0;
	}
	if (path[0] == p)
	{
		cout << abs(p - path[n - 1]);
		return 0;
	}
	int m = 0;
	m = abs(path[n - 1] - p) + abs(path[n-1] - path[0]);
	m = min(m, abs(path[0] - p) + abs(path[0] - path[n-1]));
	m = min(m, abs(path[1] - p) + abs(path[1] - path[n]));
	m = min(m, abs(path[n] - p) + abs(path[1] - path[n]));
	cout << m;
	cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	path.push_back(p);
	sort(path.begin(), path.end());
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	if (path[n] == p)
	{
		cout << abs(path[n] - path[1]);
		return 0;
	}
	if (path[0] == p)
	{
		cout << abs(p - path[n - 1]);
		return 0;
	}
	int m = 0;
	int size = abs(path[0] - path[n]);
	if (abs(path[n] - p) > abs(path[0] - p))
		path.pop_back();
	else
		path.pop_front();
	m = abs(path[n - 1] - p) + size;
	m = min(m, abs(path[0] - p)) + size;
	cout << m;
	cin >> n;
	return 0;
}


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <deque>
#include <math.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
	deque<int>path;
	int n, p;
	cin >> n >> p;
	for (int i = 0, x; i < n; i++)
	{
		cin >> x;
		path.push_back(x);
	}
	if (path.size() == 1)
	{
		cout << 0;
		return 0;
	}
	path.push_back(p);
	sort(path.begin(), path.end());
	if (path.size() == 1)
	{
		cout << abs(path[0] - p);
		return 0;
	}
	if (path[n] == p)
	{
		cout << abs(path[n] - path[1]);
		return 0;
	}
	if (path[n - 1] == p)
	{
		cout << abs(path[n - 1] - path[0]);
		return 0;
	}
	if (path[0] == p)
	{
		cout << abs(p - path[n - 1]);
		return 0;
	}
	if (path[1] == p)
	{
		cout << abs(p - path[n]);
		return 0;
	}
	cout << path[n-2] - path[0] + min(abs(path[0] - p), abs(path[n - 2] - p));
	cin >> n;
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int mapp[100004];
int main()
{
	int n,a;
	while(scanf("%d%d",&n,&a)!=EOF)
	{

		for(int i=0;i<n;i++)
		{
			scanf("%d",&mapp[i]);
		}
		if(n==1)
		{
			printf("0\n");
			continue;
		}
		sort(mapp,mapp+n);
		int ans=INT_MAX;
		ans=min(ans,abs(a-mapp[0])+mapp[n-2]-mapp[0]);
		ans=min(ans,abs(a-mapp[n-2])+mapp[n-2]-mapp[0]);
		ans=min(ans,abs(a-mapp[n-1])+mapp[n-1]-mapp[1]);
		ans=min(ans,abs(a-mapp[1])+mapp[n-1]-mapp[1]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int mapp[100004];
int main()
{
	int n,a;
	while(scanf("%d%d",&n,&a)!=EOF)
	{
		if(n==1)
		{
			printf("0\n");
			continue;
		}
		for(int i=0;i<n;i++)
		{
			scanf("%d",&mapp[i]);
		}
		sort(mapp,mapp+n);
		int ans=INT_MAX;
		ans=min(ans,abs(a-mapp[0])+mapp[n-2]-mapp[0]);
		ans=min(ans,abs(a-mapp[n-2])+mapp[n-2]-mapp[0]);
		ans=min(ans,abs(a-mapp[n-1])+mapp[n-1]-mapp[1]);
		ans=min(ans,abs(a-mapp[1])+mapp[n-1]-mapp[1]);
		printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <math.h>
#include <stack>
#include <Windows.h>

using namespace std;

#define ll long long
#define pii pair<ll, ll>
#define endl "\n"
#define mp make_pair
#define pll pair<ll,ll>

/*
#include <fstream>
ifstream in("input.txt");
ofstream out("output.txt");

#define cin in
#define cout out
/**/

int main()
{
	ios_base::sync_with_stdio(0);
	cout.precision(9);
	cout << fixed;

	int n, a;
	cin >> n >> a;

	vector<int> aa(n);
	for (int i = 0; i < n; ++i)
		cin >> aa[i];

	sort(aa.begin(), aa.end());
	if (n == 1)
	{
		cout << 0;
		return 0;
	}

	if (n == 2)
	{
		cout << min(abs(a - aa[0]), abs(a - aa[1]));
		return 0;
	}

	ll ans = 1e18;
	if (a >= aa[1] && a <= aa[n-1])
		ans = min(ans, aa[n-1] - aa[1] + min(max(a - aa[1], 0), max(aa[n-1] - a, 0)));
	else if (a < aa[1])
		ans = min(ans, abs(aa[n-1] - a));
	else
		ans = min(ans, abs(a - aa[1]));
	if (a >= aa[0] && a <= aa[n-2])
		ans = min(ans, aa[n-2] - aa[0] + min(max(a - aa[0], 0), max(aa[n-2] - a, 0)));
	else if (a < aa[0])
		ans = min(ans, abs(aa[n-2] - a));
	else
		ans = min(ans, abs(a - aa[0]));
	cout << ans;

	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cmath>
#include <map>

using namespace std;

#define VT vector
typedef VT<int> VI;
typedef VT<VI> VVI;
typedef VT<string> VS;
typedef VT<double> VD;
typedef pair<int, int> PII;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define ALL(c) c.begin(),c.end()
#define PB push_back
#define MP make_pair
#define FS first
#define SC second
#define SZ size()

long long abs_(long long a)
{
	if (a > 0)
		return a;
	return -a;
}

// BEGIN CUT HERE
int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	VI x;
	REP(i, n)
	{
		long long c = 0;
		cin >> c;
		x.push_back(c);
	}

	if (n == 1)
	{
		cout << 0;
		return 0;
	}

	sort(ALL(x));

	long long r = abs_(x[0] - a) + abs_(x[0] - x.back());

	r = min<long long>(r, abs_(x[0] - a) + abs_(x[0] - x[n - 2]));
	r = min<long long>(r, abs_(x[n - 2] - a) + abs_(x[0] - x[n - 2]));

	r = min<long long>(r, abs_(x[1] - a) + abs_(x[1] - x[n - 1]));
	r = min<long long>(r, abs_(x[n - 1] - a) + abs_(x[1] - x[n - 1]));

	cout << r;

}
// END CUT HERE


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cmath>
#include <map>

using namespace std;

#define VT vector
typedef VT<int> VI;
typedef VT<VI> VVI;
typedef VT<string> VS;
typedef VT<double> VD;
typedef pair<int, int> PII;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define ALL(c) c.begin(),c.end()
#define PB push_back
#define MP make_pair
#define FS first
#define SC second
#define SZ size()

long long abs_(long long a)
{
	if (a > 0)
		return a;
	return -a;
}

// BEGIN CUT HERE
int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	VI x;
	REP(i, n)
	{
		long long c = 0;
		cin >> c;
		x.push_back(c);
	}

	sort(ALL(x));

	long long r = abs_(x[0] - a) + abs_(x[0] - x.back());

	r = min<long long>(r, abs_(x[0] - a) + abs_(x[0] - x[n - 2]));
	r = min<long long>(r, abs_(x[n-2] - a) + abs_(x[0] - x[n - 2]));

	r = min<long long>(r, abs_(x[1] - a) + abs_(x[1] - x[n - 1]));
	r = min<long long>(r, abs_(x[n - 1] - a) + abs_(x[1] - x[n - 1]));

	cout << r;

}
// END CUT HERE


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cmath>
#include <map>

using namespace std;

#define VT vector
typedef VT<int> VI;
typedef VT<VI> VVI;
typedef VT<string> VS;
typedef VT<double> VD;
typedef pair<int, int> PII;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define ALL(c) c.begin(),c.end()
#define PB push_back
#define MP make_pair
#define FS first
#define SC second
#define SZ size()

// BEGIN CUT HERE
int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	VI x;
	REP(i, n)
	{
		int c = 0;
		cin >> c;
		x.push_back(c);
	}

	sort(ALL(x));

	long long r = abs(x[0] - a) + abs(x[0] - x.back());

	r = min<long long>(r, abs(x[0] - a) + abs(x[0] - x[n - 2]));
	r = min<long long>(r, abs(x[n-2] - a) + abs(x[0] - x[n - 2]));

	r = min<long long>(r, abs(x[1] - a) + abs(x[1] - x[n - 1]));
	r = min<long long>(r, abs(x[n - 1] - a) + abs(x[1] - x[n - 1]));

	cout << r;

}
// END CUT HERE


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0,a5=100000000,a6=100000000;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(y>mx)o=2;
	if(y>mn&&y<=mn2)a5=abs(mx-y);
	if(y<mx&&y>=mx2)a6=abs(mn-y);
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,min(a4,min(a5,a6)))));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=2*mn+mx2;
	a2=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(y<mn)o=3;if(y>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(z==1)cout<<0<<endl;
	else if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;
}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0,a5=100000000,a6=100000000;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(y>mx)o=2;
	if(y>mn&&y<=mn2)a5=abs(mx-y);
	if(y<mx&&y>=mx2)a6=abs(mn-y);
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,min(a4,min(a5,a6)))));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=2*mn+mx2;
	a2=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(y<mn)o=3;if(y>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(z==1)cout<<0<<endl;
	if else(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;
}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(z<mn)o=1;if(z>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else if(x==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(z<mn)o=1;if(z>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=a2=2*mn+mx2;
	a2=a3=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));

	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(z<mn)o=3;if(z>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;

}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(z>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(y<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(z>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=a2=2*mn+mx2;
	a2=a3=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));

	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(z<mn)o=3;if(z>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;

}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0,a5=100000000,a6=100000000;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(y>mx)o=2;
	if(y>mn&&y<=mn2)a5=abs(mx-y);
	if(y<mx&&y>=mx2)a6=abs(mn-y);
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,min(a4,min(a5,a6)))));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=2*mn+mx2;
	a2=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(y<mn)o=3;if(y>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;
}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0,a5=100000000,a6=100000000;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(y>mx)o=2;
	if(y>mn&&y<=mn2)a5=abs(mx-y);
	if(y<mx&&y>=mx2)a6=abs(mn-y);
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,min(a4,min(a5,a6)))));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=2*mn+mx2;
	a2=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(y<mn)o=3;if(y>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(z==1)cout<<0<<endl;
    else if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;
}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	long long mx=-100000000,mx2=-100000000,mn2=100000000,mn=100000000,x,y,z,a1,a2,a3,a4,ans,o=0;
	cin>>z>>y;
	if(z>3){
	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn2=mn;mn=x;}
	else if(mn2>x)mn2=x;
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);mn2=abs(mn2-y);
	a1=2*mx+mn2;
	a2=2*mn+mx2;
	a3=2*mx2+mn;
	a4=2*mn2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else if(z==3){

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx2=mx;mx=x;}
	else if(mx2<x)mx2=x;
	if(mn>x){mn=x;}
	}
	if(y<mn)o=1;if(y>mx)o=2;
	mx=abs(mx-y);mn=abs(mn-y);mx2=abs(mx2-y);
	a1=2*mn+mx2;
	a2=2*mx2+mn;
	a3=2*mx+mx2;
	a4=2*mx2+mx;
	ans=min(a1,min(a2,min(a3,a4)));
	}
	else {

	for(int i=0;i<z;i++){
	cin>>x;
	if(mx<x){mx=x;}
	if(mn>x){mn=x;}
	}
	if(y<mn)o=3;if(y>mx)o=4;
	mx=abs(mx-y);mn=abs(mn-y);
	ans=min(mx,mn);
}
	if(o==0)cout<<ans<<endl;
	else if(o==1)cout<<mx2<<endl;
	else if(o==2) cout<<mn2<<endl;
	else if(o==3)cout<<mn<<endl;
	else if(o==4)cout<<mx<<endl;
}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,d[100005],k;
int main() {
	// your code goes here
	cin >> n >> k;
	for(int i = 0; i < n; i++)
		cin >> d[i];
	d[n] = k;

	std::sort(d, d + n + 1);
	int idx = lower_bound(d,d + n + 1, k) - d;
	ll rightS = 0;
	for(int i = idx + 1; i <= n; i++) {
		rightS += d[i] - d[i - 1];
	}
	ll rightS1 = 0;
	if(idx != n)
		rightS1 = rightS + d[n - 1] - d[n];
	ll leftS = 0;
	for(int i = 1; i <= idx; i++) {
		cout << d[i] << '\n';
		leftS += d[i] - d[i - 1];
	}
	ll leftS1 = 0;
	if(idx != 0)
		leftS1 = leftS + d[0] - d[1];
	cout << min(min(2 * leftS1 + rightS,leftS + 2 * rightS1),min(2 * leftS + rightS1,leftS1 + 2 * rightS));

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,d[100005],k;
int main() {
	// your code goes here
	cin >> n >> k;
	for(int i = 0; i < n; i++)
		cin >> d[i];
	d[n] = k;

	std::sort(d, d + n + 1);
	int idx = lower_bound(d,d + n + 1, k) - d;
	ll rightS = 0;
	for(int i = idx + 1; i <= n; i++) {
		rightS += d[i] - d[i - 1];
	}
	ll rightS1 = 0;
	if(idx != n)
		rightS1 = rightS + d[n - 1] - d[n];
	ll leftS = 0;
	for(int i = 1; i <= idx; i++) {
		leftS += d[i] - d[i - 1];
	}
	ll leftS1 = 0;
	if(idx != 0)
		leftS1 = leftS + d[0] - d[1];
	cout << min(min(2 * leftS1 + rightS,leftS + 2 * rightS1),min(2 * leftS + rightS1,leftS1 + 2 * rightS));

	return 0;
}


#include <queue>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <functional>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <iomanip>
#include <limits>
#include <unordered_map>
#include <set>
#include <cmath>
#include <numeric> //accumulate
#include <stack>
#include <bitset>

//#include <unordered_set>//unordered_set

#define rep(i,a) for (int i = 0; i < (a); ++i)
#define rep2(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef vector<vector<int> > vvi;
typedef const vector<int> cvi;
typedef vector<bool> vb;
typedef const vs cvs;
typedef const string cs;
//const long long INF = 1000000000000000LL;	// numeric_limits<long long>::max() ?? ?????? ??????? ?????????? ???
//const long long INF = 1LL << 60;
//const int INF = 987654321;
const double EPSILON = 1e-10;
//const double INF = 123456789;

const long long INF = 1LL << 60;
//const long long INF = (1e+18) + 10; // ????? ??? 1000000000000000000 ???? ??????.. ???? ??????????
//const long long INF = (long long)(1e+18) + 10;
//map<ll, int> my1;

int main() {
	//	freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);
	int n;
	ll a;
	cin >> n >> a;
	vector<ll> arr(n);
	rep(i, n) cin >> arr[i];
	if (n == 1) {
		cout << 0; return 0;
	}
	sort(arr.begin(), arr.end());
	ll sol = INF;
	if (a <= arr[0])
		sol = arr[arr.size() - 2] - a;
	else if (a >= arr.back())
		sol = a - arr[1];
	else {
		arr.push_back(a);
		sort(arr.begin(), arr.end());
		ll tp1 = (a - arr[1]) + (arr.back() - arr[1]);
		ll tp2 = (a - arr[0]) + (arr[arr.size() - 2] - arr[0]);
		ll tp3 = (arr[arr.size() - 2] - a) + (arr[arr.size() - 2] - arr[0]);
		ll tp4 = (arr.back() - a) + (arr.back() - arr[1]);
		sol = min(tp1, tp2);
		sol = min(sol, tp3);
		sol = min(sol, tp4);
	}
	cout << sol;
	return 0;
}


#include <iostream>

void qsort(int* x, int min_1, int max_1) {
    int mid = x[(min_1 + max_1) / 2];
    int min_2 = min_1, max_2 = max_1;

    while (min_2 <= max_2) {
        while (x[min_2] < mid) ++min_2;
        while (x[max_2] > mid) --max_2;

        if (min_2 <= max_2) {
            int tmp = x[min_2];
            x[min_2] = x[max_2];
            x[max_2] = tmp;
            ++min_2;
            --max_2;
        }
    }
    if (min_1 < max_2) qsort(x, min_1, max_2);
    if (min_2 < max_1) qsort(x, min_2, max_1);
}

int main() {
    int n, a;
    size_t min_value= 9999999999;
    std::cin >> n >> a;
    int* x = new int[n];
    bool flag = 0;

    if (n < 2) {
        std::cout << 0;
        return 0;
    }

    for (int i = 0; i < n; ++i)
        std::cin >> x[i];

    qsort(x, 0, n - 1);

    for (int i = 0; i < n; ++i) {
        if (a <= x[i]) {
            if (i > 0) {
                flag = 1;
                size_t value;
                value = abs(a - x[0]) + abs(x[n - 2] - x[0]);     if (value < min_value) min_value = value;
                value = abs(a - x[1]) + abs(x[n - 1] - x[1]);     if (value < min_value) min_value = value;
                value = abs(x[n - 1] - a) + abs(x[n-1] - x[1]);   if (value < min_value) min_value = value;
                value = abs(x[n - 2] - a) + abs(x[n - 2] - x[0]); if (value < min_value) min_value = value;
            }
            else {
                flag = 1;
                min_value = (x[n - 2] - a);
            }

            break;
        }
    }
    if (!flag) min_value = abs(a - x[1]);

    std::cout << min_value;

    //system("pause");
    return 0;
}


#include <iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
    int n,a;
    cin>>n>>a;
    int x[100000];
    int y[100000];
    int z[100000];
    int s[100000];
    int max1=-10e9;
    int d=0;
    int right=0;
    int posi=a;
    int left=0;
    int pos=0;
    long long int sum=0;
    long long int sum1=0;
    for(int i=0;i<n;i++)
    {
        cin>>x[i];
        if(a>x[i])
        {
            y[left]=x[i];
            left++;
        }
        else if(a<x[i])
        {
            z[right]=x[i];
            right++;
        }
        else
        {
            s[pos]=x[i];
            pos++;
        }
        if(max1<abs(x[i]-a))
        {
            max1=abs(x[i]-a);
            d=x[i];
        }
    }
    if(pos==n-1)
    {
        cout<<0;
        return 0;
    }
    sort(y,y+left);
    sort(z,z+right);
    posi=a;
    for(int i=0;i<pos;i++)
    {
        sum+=s[i];
        sum1+=s[i];
    }
    for(int i=0;i<right;i++)
    {
        if(z[i]!=d)
        {
        sum+=abs(z[i]-posi);
        posi=z[i];
        }
    }
    for(int i=0;i<left;i++)
    {
        if(y[i]!=d)
        {
            sum+=abs(posi-y[i]);
            posi=y[i];
        }
    }
    posi=a;
    for(int i=0;i<left;i++)
    {
        if(y[i]!=d)
        {
        sum1+=abs(y[i]-posi);
        posi=y[i];
        }
    }
    for(int i=0;i<right;i++)
    {
        if(z[i]!=d)
        {
            sum1+=abs(posi-z[i]);
            posi=z[i];
        }
    }
    if(sum1>sum)
        cout<<sum;
    else
        cout<<sum1;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a,n;
    int x[100001];
    cin>>n>>a;
    for(int i=0;i<n;i++)
    {
        cin>>x[i];
    }
    sort(x,x+n);
	cout<<min(min(abs(x[0]-a),abs(x[n-2]-a))+x[n-2]-x[0],min(abs(x[1]-a),abs(x[n-1]-a))+x[n-1]-x[1]);
}


#include <iostream>
#include<iomanip>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
    int n,a;
    cin>>n>>a;
    int x[100000];
    int y[100000];
    int z[100000];
    int s[100000];
    int max1=-10e9;
    int d=0;
    int right=0;
    int posi=a;
    int left=0;
    int pos=0;
    long long int sum=0;
    long long int sum1=0;
    for(int i=0;i<n;i++)
    {
        cin>>x[i];
        if(a>x[i])
        {
            y[left]=x[i];
            left++;
        }
        else if(a<x[i])
        {
            z[right]=x[i];
            right++;
        }
        else
        {
            s[pos]=x[i];
            pos++;
        }
        if(max1<abs(x[i]-a))
        {
            max1=abs(x[i]-a);
            d=x[i];
        }
    }
    posi=a;
    for(int i=0;i<pos;i++)
    {
        sum+=s[i];
        sum1+=s[i];
    }
    for(int i=0;i<right;i++)
    {
        if(z[i]!=d)
        {
        sum+=abs(z[i]-posi);
        posi=z[i];
        }
    }
    for(int i=0;i<left;i++)
    {
        if(y[i]!=d)
        {
            sum+=abs(posi-y[i]);
            posi=y[i];
        }
    }
    posi=a;
    for(int i=0;i<left;i++)
    {
        if(y[i]!=d)
        {
        sum1+=abs(y[i]-posi);
        posi=y[i];
        }
    }
    for(int i=0;i<right;i++)
    {
        if(z[i]!=d)
        {
            sum1+=abs(posi-z[i]);
            posi=z[i];
        }
    }
    if(sum1>sum)
        cout<<sum;
    else
        cout<<sum1;
}


#include <iostream>
#include<algorithm>
#include<string>
#include<map>
#include <iterator>
#include<iomanip>
#include <time.h>
#include<vector>
#include<cmath>
#include <cstdio>
#include<queue>
#include<unordered_map>
#include<stack>
#include<list>
#include<stdio.h>
#include<set>
#include <functional>

#define hash_mod 2305843009213717
#define mod7 1000000007
#define mod2 30000000
#define pa(a,b) make_pair(a,b)
#define f first
#define s second
#define pb(a)  push_back(a)
#define all(a) a.begin() , a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define LL  long long int
#define oo 1e9
#define pi 3.141592653589793238
#define eps  1e-6

LL maxx(LL & a , LL & b){ return a>b ?  a : b ;}
LL minn(LL & a , LL & b){ return a>b ?  b : a ;}

using namespace std;
int dx[] = {1 , -1 , 0 , 0} ;
int dy[] = {0 , 0 , 1 , -1} ;
void boostIO()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}
int int_gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
LL lcm(LL a , LL b )
{
    return (a*b)/gcd(a , b);
}
LL poww(LL base, LL exp , LL mod){
  base %= mod;
  LL result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return result;
}
map<int , int>mp ;
#define offset 2000000
int main()
{
    int n , pos ; cin >> n >> pos ;
    pos = pos + offset ;
    vector<int> a;
    if(n == 1){
        cout << 0 << endl ;
        return 0 ;
    }
    for(int i = 0 ; i < n ; i++){
        int x; cin >> x ;
        x = x + offset ;
        a.pb(x) ; mp[x]++ ;
    }

    sort(all(a)) ;
    int ans = 1e9 ;

    if(mp[a.back()] == 1 && mp[a.front()] == 1){
        if(n == 2){
            ans = min(abs(pos - a.back()) , abs(a.front() - pos)) ;
        }
        else {
            int l = a[1] , r = a[n - 2] ;

            if(pos >= a.front() && pos <= a.back())
                ans = min(min(abs(r - pos) + r - a.front() , abs(pos - a.front()) + r - a.front())
                           , min(abs(l - pos) + a.back() - l , abs(pos - a.back()) + a.back() - l)) ;
            else if(pos < a.front()){
                ans = r - pos ;
            }
            else {
                ans = pos - l ;
            }
        }
    }
    else if(mp[a.back()] == 1 || mp[a.front()] == 1){
        if(mp[a.back()] > 1){
            int l = a[1];
            ans = min(abs(pos - l) + a.back() - l , abs(a.back() - pos) + a.back() - l) ;
        }
        else {
            int r = a[n - 2] ;
            ans = min(abs(pos - r) + a.front() - r  , abs(a.front() - pos) + a.front() - r) ;
        }
    }
    else {
        if(pos >= a.front() && pos <= a.back())
            ans = min(abs(pos - a.front()) + (a.back() - a.front()) , abs(a.back() - pos) + (a.back() - a.front())) ;
        else if(pos < a.front())
            ans = a.back() - pos ;
        else
            ans = pos - a.front() ;
    }

    cout << ans << endl ;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a,n;
    int x[100001];
    cin>>n>>a;
    if(n==1)
    {
        cout<<0;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        cin>>x[i];
    }
    sort(x,x+n);
	cout<<min(min(abs(x[0]-a),abs(x[n-2]-a))+x[n-2]-x[0],min(abs(x[1]-a),abs(x[n-1]-a))+x[n-1]-x[1]);
}


#include <iostream>
#include<algorithm>
#include<string>
#include<map>
#include <iterator>
#include<iomanip>
#include <time.h>
#include<vector>
#include<cmath>
#include <cstdio>
#include<queue>
#include<unordered_map>
#include<stack>
#include<list>
#include<stdio.h>
#include<set>
#include <functional>

#define hash_mod 2305843009213717
#define mod7 1000000007
#define mod2 30000000
#define pa(a,b) make_pair(a,b)
#define f first
#define s second
#define pb(a)  push_back(a)
#define all(a) a.begin() , a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define LL  long long int
#define oo 1e9
#define pi 3.141592653589793238
#define eps  1e-6

LL maxx(LL & a , LL & b){ return a>b ?  a : b ;}
LL minn(LL & a , LL & b){ return a>b ?  b : a ;}

using namespace std;
int dx[] = {1 , -1 , 0 , 0} ;
int dy[] = {0 , 0 , 1 , -1} ;
void boostIO()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}
int int_gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
LL lcm(LL a , LL b )
{
    return (a*b)/gcd(a , b);
}
LL poww(LL base, LL exp , LL mod){
  base %= mod;
  LL result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return result;
}
map<int , int>mp ;
#define offset 2000000
int main()
{
    int n , pos ; cin >> n >> pos ;
    pos = pos + offset ;
    vector<int> a;
    if(n == 1){
        cout << 0 << endl ;
        return 0 ;
    }
    for(int i = 0 ; i < n ; i++){
        int x; cin >> x ;
        x = x + offset ;
        a.pb(x) ; mp[x]++ ;
    }

    sort(all(a)) ;

    int ans = 1e9 ;

    if(mp[a.back()] == 1 && mp[a.front()] == 1){
        if(n == 2){
            ans = min(abs(pos - a.back()) , abs(a.front() - pos)) ;
        }
        else {
            int l = a[1] , r = a[n - 2] ;

            if(pos >= a.front() && pos <= a.back())
                ans = min(min(abs(r - pos) + r - a.front() , abs(pos - a.front()) + r - a.front())
                           , min(abs(l - pos) + a.back() - l , abs(pos - a.back()) + a.back() - l)) ;
            else if(pos < a.front()){
                ans = r - pos ;
            }
            else {
                ans = pos - l ;
            }
        }
    }
    else if(mp[a.back()] == 1 || mp[a.front()] == 1){
        if(mp[a.back()] > 1){
            int l = a[1];
            ans = min(abs(pos - l) + abs(a.back() - l) , abs(a.back() - pos) + a.back() - l) ;
        }
        else {
            int r = a[n - 2] ;
            ans = min(abs(pos - r) + abs(a.front() - r)  , abs(a.front() - pos) + r - a.front()) ;
        }
    }
    else {
        if(pos >= a.front() && pos <= a.back())
            ans = min(abs(pos - a.front()) + (a.back() - a.front()) , abs(a.back() - pos) + (a.back() - a.front())) ;
        else if(pos < a.front())
            ans = a.back() - pos ;
        else
            ans = pos - a.front() ;
    }

    cout << ans << endl ;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long double ld;
typedef long long ll;	// Use "%I64d" as placeholder with printf and scanf.
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef priority_queue<ll, vector<ll>, greater<ll>> min_heap_ll;
typedef priority_queue<ll, vector<ll>, less<ll>> max_heap_ll;

#define pi 3.141592653589793238462643383279502884197169399375105820974944592307816406286
#define f(i, n) for(ll i=0; i<n; ++i)
#define fi(i, n, j) for(ll i=j; i<n; ++i)
#define mp make_pair
#define all(container) container.begin(), container.end()
#define allr(container) container.rbegin(), container.rend()
#define tr(container, it) for (decltype(container.begin()) it = container.begin(); it != container.end(); it++)
#define trr(container, it) for (decltype(container.rbegin()) it = container.rbegin(); it != container.rend(); it++)
#define dist(a, b) sqrtl(powl(ld(a.first - b.first), 2) + powl(ld(a.second - b.second), 2))

// useful functions
ll gcd(ll a, ll b)
{
	return b ? gcd(b, a % b) : a;
}

ll my_exp(ll a, ll b, ll MOD = numeric_limits<ll>::max())
{
	a %= MOD;
	return b ? (((b % 2 == 1 ? a : 1) * my_exp(a * a, b / 2, MOD)) % MOD) : 1;
}

bool is_prime(ll n)	// O(sqrt(n)) implementation check for 0 and 1 separately
{
	if (n != 2 && n % 2 == 0) return false;
	else { for (ll i = 3; (i*i) <= n; i += 2) if (n%i == 0) return false; return true; }
}

// Program global variables

// Program classes and structs

// Program functions
ll my_mod(ll n)
{
	if (n < 0) n = -n;
	return n;
}

int main()
{
#ifdef _CODE_SOURCE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	double start_time = clock();
#endif
	// Program Code
	ll n, a; cin >> n >> a;
	vll x(n); f(i, n) cin >> x[i];
	sort(all(x));
	ll ans;
	if (n == 1)
	{
		ans = 0;
	}
	else if (n == 2)
	{
		ll p1 = my_mod(x[0] - a);
		ll p2 = my_mod(x[1] - a);
		ans = min(p1, p2);
	}
	else if (n == 3)
	{
		ll p1 = my_mod(x[0] - a) + my_mod(x[1] - x[0]);
		ll p2 = my_mod(x[1] - a) + my_mod(x[0] - x[1]);
		ll p3 = my_mod(x[1] - a) + my_mod(x[2] - x[1]);
		ll p4 = my_mod(x[2] - a) + my_mod(x[1] - x[2]);
		ans = min(min(p1, p2), min(p3, p4));
	}
	else
	{
		ll p1 = my_mod(x[0] - a) + my_mod(x[n-2] - x[0]);
		ll p2 = my_mod(x[1] - a) + my_mod(x[n-1] - x[1]);
		ll p3 = my_mod(x[n-2] - a) + my_mod(x[n-2] - x[0]);
		ll p4 = my_mod(x[n-1] - a) + my_mod(x[1] - x[n-1]);
		ans = min(min(p1, p2), min(p3, p4));
	}
	cout << ans;
	// Program Code Ends
#ifdef _CODE_SOURCE
	double end_time = clock();
	printf("\nTime = %.3f seconds\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
	return(0);
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long double ld;
typedef long long ll;	// Use "%I64d" as placeholder with printf and scanf.
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef priority_queue<ll, vector<ll>, greater<ll>> min_heap_ll;
typedef priority_queue<ll, vector<ll>, less<ll>> max_heap_ll;

#define pi 3.141592653589793238462643383279502884197169399375105820974944592307816406286
#define f(i, n) for(ll i=0; i<n; ++i)
#define fi(i, n, j) for(ll i=j; i<n; ++i)
#define mp make_pair
#define all(container) container.begin(), container.end()
#define allr(container) container.rbegin(), container.rend()
#define tr(container, it) for (decltype(container.begin()) it = container.begin(); it != container.end(); it++)
#define trr(container, it) for (decltype(container.rbegin()) it = container.rbegin(); it != container.rend(); it++)
#define dist(a, b) sqrtl(powl(ld(a.first - b.first), 2) + powl(ld(a.second - b.second), 2))

// useful functions
ll gcd(ll a, ll b)
{
	return b ? gcd(b, a % b) : a;
}

ll my_exp(ll a, ll b, ll MOD = numeric_limits<ll>::max())
{
	a %= MOD;
	return b ? (((b % 2 == 1 ? a : 1) * my_exp(a * a, b / 2, MOD)) % MOD) : 1;
}

bool is_prime(ll n)	// O(sqrt(n)) implementation check for 0 and 1 separately
{
	if (n != 2 && n % 2 == 0) return false;
	else { for (ll i = 3; (i*i) <= n; i += 2) if (n%i == 0) return false; return true; }
}

// Program global variables

// Program classes and structs

// Program functions
ll my_mod(ll n)
{
	if (n < 0) n = -n;
	return n;
}

int main()
{
#ifdef _CODE_SOURCE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	double start_time = clock();
#endif
	// Program Code
	ll n, a; cin >> n >> a;
	vll x(n); f(i, n) cin >> x[i];
	ll ans;
	if (n == 1)
	{
		ans = 0;
	}
	else if (n == 2)
	{
		ll p1 = my_mod(x[0] - a);
		ll p2 = my_mod(x[1] - a);
		ans = min(p1, p2);
	}
	else if (n == 3)
	{
		ll p1 = my_mod(x[0] - a) + my_mod(x[1] - x[0]);
		ll p2 = my_mod(x[1] - a) + my_mod(x[0] - x[1]);
		ll p3 = my_mod(x[1] - a) + my_mod(x[2] - x[1]);
		ll p4 = my_mod(x[2] - a) + my_mod(x[1] - x[2]);
		ans = min(min(p1, p2), min(p3, p4));
	}
	else
	{
		ll p1 = my_mod(x[0] - a) + my_mod(x[n-2] - x[0]);
		ll p2 = my_mod(x[1] - a) + my_mod(x[n-1] - x[1]);
		ll p3 = my_mod(x[n-2] - a) + my_mod(x[n-2] - x[0]);
		ll p4 = my_mod(x[n-1] - a) + my_mod(x[1] - x[n-1]);
		ans = min(min(p1, p2), min(p3, p4));
	}
	cout << ans;
	// Program Code Ends
#ifdef _CODE_SOURCE
	double end_time = clock();
	printf("\nTime = %.3f seconds\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
	return(0);
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int n;
int start;
int coord[100001];

int main(void)
{
	scanf("%d %d", &n, &start);
	int inp, ret;
	int r_cmp1, l_cmp1, r_cmp2, l_cmp2;

	for (int i = 0; i < n; i++) {
		scanf("%d", &inp);
		coord[i] = inp;
	}
	if (n<2)
		printf("%d\n", 0);
	else
	{
		sort(coord, coord+n);

		r_cmp1 = abs(coord[n - 1] - start);
		r_cmp2 = abs(coord[n - 2] - start);
		l_cmp1 = abs(coord[0] - start);
		l_cmp2 = abs(coord[1] - start);

		if (coord[1]<start)
			r_cmp1 += abs(coord[n - 1] - coord[1]);
		if (coord[0]<start)
			r_cmp2 += abs(coord[n - 1] - coord[0]);
		if (coord[n - 2]>start)
			l_cmp1 += abs(coord[n - 2] - coord[0]);
		if (coord[n - 1]>start)
			l_cmp2 += abs(coord[n - 1] - coord[1]);

		r_cmp1 = min(r_cmp1, r_cmp2);
		l_cmp1 = min(l_cmp1, l_cmp2);
		ret = min(r_cmp1, l_cmp1);

		printf("%d\n", ret);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int n;
int start;
int coord[100001];

int main(void)
{
	scanf("%d %d", &n, &start);
	int inp, ret;
	int r_cmp1, l_cmp1, r_cmp2, l_cmp2;

	for (int i = 0; i < n; i++) {
		scanf("%d", &inp);
		coord[i] = inp;
	}
	if (n<2)
		printf("%d\n", 0);
	else
	{
		sort(coord, coord+n);

		//오른쪽 끝점 coord[n-2] coord[n-1]
		//왼쪽 끝점 coord[0] coord[1]
		// coord[0] cord[1]...... start ...... coord[n-2] coord[n-1]

		if (coord[n - 1] >= start)
			r_cmp1 = abs(coord[n - 1] - start);
		else
			r_cmp1 = 9999999;
		if (coord[n - 2] >= start)
			r_cmp2 = abs(coord[n - 2] - start);
		else
			r_cmp2 = 9999999;
		if (start >= coord[0])
			l_cmp1 = abs(coord[0] - start);
		else
			l_cmp1 = 9999999;
		if (start >= coord[1])
			l_cmp2 = abs(coord[1] - start);
		else
			l_cmp2 = 9999999;

		if (coord[1]<start)
			r_cmp1 += abs(coord[n - 1] - coord[1]);
		if (coord[0] < start)
			r_cmp2 += abs(coord[n - 1] - coord[0]);
		else
			r_cmp2 = 99999999;
		if (coord[n - 2]>start)
			l_cmp1 += abs(coord[n - 2] - coord[0]);
		if (coord[n - 1] > start)
			l_cmp2 += abs(coord[n - 1] - coord[1]);
		else
			l_cmp2 = 99999999;

		r_cmp1 = min(r_cmp1, r_cmp2);
		l_cmp1 = min(l_cmp1, l_cmp2);
		ret = min(r_cmp1, l_cmp1);

		printf("%d\n", ret);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int n;
int start;
int coord[100001];

int main(void)
{
	scanf("%d %d", &n, &start);
	int inp, ret;
	int l_cmp1, l_cmp2, r_cmp1, r_cmp2;

	for (int i = 0; i < n; i++) {
		scanf("%d", &inp);
		coord[i] = inp;
	}
	sort(coord, coord + n);
	if (n < 2)
		ret = 0;

	//오른쪽 끝점 coord[n-2] coord[n-1]
	//왼쪽 끝점 coord[0] coord[1]
	// coord[0] cord[1]...... start ...... coord[n-2] coord[n-1]
	else
	{
		if (start == coord[0] || start == coord[n - 2])
		{
			l_cmp1 = coord[n - 2] - coord[0];
		}

		else
		{
			l_cmp1 = abs(coord[n - 2] - start) + abs(coord[n - 2] - coord[0]);
			l_cmp2 = abs(start - coord[0]) + abs(coord[n - 2] - coord[0]);
			l_cmp1 = min(l_cmp1, l_cmp2);
		}

		if (start == coord[1] || start == coord[n - 1])
		{
			r_cmp1 = coord[n - 1] - coord[1];
		}
		else
		{
			r_cmp1 = abs(coord[n - 1] - start) + abs(coord[n - 1] - coord[1]);
			r_cmp2 = abs(start - coord[1]) + abs(coord[n - 1] - coord[1]);
			r_cmp1 = min(r_cmp1, r_cmp2);
		}

		ret = min(l_cmp1, r_cmp1);
	}

	printf("%d\n", ret);

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,x;
vector<int> v;
cin>>n>>x;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
v.push_back(x);
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
if(n==1)
cout<<0<<endl;
else if(v[0]==x)
cout<<v[v.size()-2]-x<<endl;
else if(v[v.size()-1]==x)
cout<<v[v.size()-1]-v[1]<<endl;
else if((v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))>
(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x)))
cout<<(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x))<<endl;
else
cout<<(v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
int n,m;
cin>>n>>m;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end());
if(n==1)
cout<<0<<endl;
else if(m<=v[0])
cout<<v[n-2]-m<<endl;
else if(m>=v[n-1])
cout<<m-v[1]<<endl;
else
{
int arr[4];
arr[0]=abs(v[n-1]-m)+(v[n-1]-v[1]);
arr[1]=abs(m-v[1])+(v[n-1]-v[1]);
arr[2]=abs(m-v[0])+(v[n-2]-v[0]);
arr[3]=abs(v[n-2]-m)+(v[n-2]-v[0]);
cout<<*min_element(arr,arr+4)<<endl;
}
return  0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
int n,m;
cin>>n>>m;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end());
if(m<=v[0])
cout<<v[n-2]-m<<endl;
else if(m>=v[n-1])
cout<<m-v[1]<<endl;
else
{
if(v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])>m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m))
cout<<m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m)<<endl;
else
cout<<v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])<<endl;
}
return  0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
int n,m;
cin>>n>>m;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end());
if(n==1)
cout<<0<<endl;
else if(m<=v[0])
cout<<v[n-2]-m<<endl;
else if(m>=v[n-1])
cout<<m-v[1]<<endl;
else
{
if(v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])>m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m))
cout<<m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m)<<endl;
else
cout<<v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])<<endl;
}
return  0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
int n,m;
cin>>n>>m;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end());
if(n==1)
cout<<1<<endl;
else if(m<=v[0])
cout<<v[n-2]-m<<endl;
else if(m>=v[n-1])
cout<<m-v[1]<<endl;
else
{
if(v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])>m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m))
cout<<m-v[0]+(m<v[n-2])*(m-v[0]+v[n-2]-m)<<endl;
else
cout<<v[n-1]-m+(m>v[1])*(v[n-1]-m+m-v[1])<<endl;
}
return  0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,x;
vector<int> v;
cin>>n>>x;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
v.push_back(x);
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
if(v[0]==x)
cout<<v[v.size()-2]-x<<endl;
else if(v[v.size()-1]==x)
cout<<v[v.size()-1]-v[1]<<endl;
else if((v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))>
(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x)))
cout<<(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x))<<endl;
else
cout<<(v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))<<endl;
return 0;
}