#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define nl cout << endl
#define pii pair <ll, ll>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846264
#define y1 abacaba
#define fn "abc"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Need_For_Speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define __lcm(a, b) (a * b) / __gcd(a, b)

using namespace std;

const long long MXN = 1e6 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

ll q;

int main () {

    cin >> q;
    while(q--) {
        ll n;
        string s;
        cin >> n >> s;
        ll ans = -1;
        for (int i = 0; i < sz(s); i++) {
            if (s[i] == '1') {
                ans = max((i + 1) * 2 * 1LL, ans);
            }
        }
        for (int i = sz(s) - 1; i >= 0; i--) {
            if (s[i] == '1') {
                ans = max((i + 1) * 2 * 1LL, ans);
            }
        }
        if (ans == -1) {
            cout << n;
        } else {
            cout << ans;
        }
        nl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define nl cout << endl
#define pii pair <ll, ll>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846264
#define y1 abacaba
#define fn "abc"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Need_For_Speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define __lcm(a, b) (a * b) / __gcd(a, b)

using namespace std;

const long long MXN = 1e6 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

ll q;

int main () {

    cin >> q;
    while(q--) {
        ll n;
        string s;
        cin >> n >> s;
        ll ans = -1;
        for (int i = 0; i < sz(s); i++) {
            if (s[i] == '1') {
                ll x = (i + 1) * 2;
                ans = max(x, ans);
            }
        }
        reverse(all(s));
        for (int i = 0; i < sz(s); i++) {
            if (s[i] == '1') {
                ll x = (i + 1) * 2;
                ans = max(x, ans);
            }
        }
        cout << ans;
        nl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define nl cout << endl
#define pii pair <ll, ll>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846264
#define y1 abacaba
#define fn "abc"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Need_For_Speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define __lcm(a, b) (a * b) / __gcd(a, b)

using namespace std;

const long long MXN = 1e6 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

ll q;

int main () {

    cin >> q;
    while(q--) {
        ll n;
        string s;
        cin >> n >> s;
        ll ans = -1;
        for (int i = 0; i < sz(s); i++) {
            if (s[i] == '1') {
                ll x = (i + 1) * 2;
                ans = max(x, ans);
            }
        }
        reverse(all(s));
        for (int i = 0; i < sz(s); i++) {
            if (s[i] == '1') {
                ll x = (i + 1) * 2;
                ans = max(x, ans);
            }
        }
        if (ans == -1) {
            cout << n;
        } else {
            cout << ans;
        }
        nl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (s[i] != s[1]) {
                cnt = i + 1;
                isDone = 1;
                break;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[1] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }

        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        int cnt1 = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != s[0]) {
                cnt1 = max(i + 1, n - i);
                cnt = max(cnt, cnt1);
                isDone = 1;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[0] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        //cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != s[0]) {
                cnt = i + 1;
                isDone = 1;
            }
        }

        cnt = max(cnt, n - cnt + 1);
        int res = cnt * 2;
        if (isDone == 0) {
            if (s[0] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        //cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        int cnt1 = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '1') {
                cnt1 = max(i + 1, n - i);
                cnt = max(cnt, cnt1);
                isDone = 1;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[0] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        //cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != s[0]) {
                cnt = i + 1;
                isDone = 1;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[0] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        //cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != s[1]) {
                cnt = i + 1;
                isDone = 1;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[1] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        //cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        int n;
        string s;
        cin >> n >> s;

        int cnt = 0;
        bool isDone = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != s[0]) {
                cnt = i + 1;
                isDone = 1;
            }
        }

        int res = cnt * 2;
        if (isDone == 0) {
            if (s[0] == '1') {
                res = n * 2;
            }
            else {
                res = n;
            }
        }
        cout << cnt << " ";
        cout << res << endl;
    }
    return 0;
}


#include<iostream>

using namespace std;

int main(){
    int test,i;
    cin>>test;
    for(i=0;i<test;i++){
        int p,j;
        cin>>p;
        string s;
        cin>>s;
        int cnt=0,x=0,y;
        for(j=0;j<p;j++){
                if(s[j]=='0'){
                    cnt++;
                }
                else{
                    cnt+=2;
                    x++;
                    y=j;
                }
            }
        if(s[0]=='1' || s[p-1]=='1'){
            cout<<2*p<<endl;
        }
        else if(x==1){
             int hi=p-y;
             y++;
             int qq=max(hi,y);
             cout<<2*qq<<endl;
        }
        else{
            cout<<cnt<<endl;
        }
        x=0;
    }
}


#include<iostream>

using namespace std;

int main(){
    int test,i;
    cin>>test;
    for(i=0;i<test;i++){
        int p,j;
        cin>>p;
        string s;
        cin>>s;
        int cnt=0,y;
        for(j=0;j<p;j++){
            if(s[j]=='1'){
                cnt++;
                y=j;
                break;
            }
        }
        if(cnt==0){
            cout<<p<<endl;
        }
        else{
             int hi=p-y;
             y++;
             int qq=max(hi,y);
             cout<<2*qq<<endl;
        }
        cnt=0;
    }
}


#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    int test,i;
    cin>>test;
    for(i=0;i<test;i++){
        int p,j;
        cin>>p;
        string s;
        cin>>s;
        int cnt=0,y;
        for(j=0;j<p;j++){
            if(s[j]=='1'){
                cnt++;
                y=j;
                break;
            }
        }
        reverse(s.begin(),s.end());
        int count=0,z;
        for(j=0;j<p;j++){
            if(s[j]=='1'){
                count++;
                z=j;
                break;
            }
        }
        if(cnt==0){
            cout<<p<<endl;
        }
        else{
             int hi=p-y;
             hi=max(hi,y);
             int hello=p-z;
             hello=max(hello,z);
             hi=max(hi,hello);
             cout<<2*hi<<endl;
        }
        cnt=0;
    }
}


#include<iostream>

using namespace std;

int main(){
    int test,i;
    cin>>test;
    for(i=0;i<test;i++){
        int p,j;
        cin>>p;
        string s;
        cin>>s;
        if(s[0]=='1' || s[p-1]=='1'){
            cout<<2*p<<endl;
        }
        else{
            int cnt=0;
            for(j=0;j<p;j++){
                if(s[j]=='0'){
                    cnt++;
                }
                else{
                    cnt+=2;
                }
            }
            cout<<cnt<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<ii> vii;
typedef map<int,int> mii;
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e9 + 7;
vector<bool> used[2];
int n;
string s;
int ans = 0;
void dfs(int h, int ind , int cur){
	used[h][ind] = true;
	if(ind>=1 && !used[h][ind-1]) dfs(h,ind-1, cur+1);
	if(ind < n-1 && !used[h][ind+1]) dfs(h,ind+1, cur+1);
	if(s[ind]=='1'){
		if(h==1 && !used[0][ind]) dfs(0,ind, cur+1);
		if(h==2 && !used[1][ind]) dfs(1,ind, cur+1);
	}
	ans = max(ans, cur);
}

void solve(){
	cin>>n;
	cin>>s;
	for(int i = 0 ; i < n; i++){
		ans = 0;
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(0,i,1);
		ans = 0;
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(1,i,1);
	}
	cout<<ans<<endl;
}

int main(){
	int T; cin>>T;
	for(int i = 0 ; i < T; i++) solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<ii> vii;
typedef map<int,int> mii;
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e9 + 7;
vector<bool> used[2];
int n;
string s;
int ans = 0;
void dfs(int h, int ind , int cur){
	used[h][ind] = true;
	if(ind>=1 && !used[h][ind-1]) dfs(h,ind-1, cur+1);
	if(ind < n-1 && !used[h][ind+1]) dfs(h,ind+1, cur+1);
	if(s[ind]=='1'){
		if(h==1 && !used[0][ind]) dfs(0,ind, cur+1);
		if(h==0 && !used[1][ind]) dfs(1,ind, cur+1);
	}
	ans = max(ans, cur);
}

void solve(){
	cin>>n;
	cin>>s;
	for(int i = 0 ; i < n; i++){
		ans = 0;
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(0,i,1);
		ans = 0;
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(1,i,1);
	}
	cout<<ans<<endl;
}

int main(){
	int T; cin>>T;
	for(int i = 0 ; i < T; i++) solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<ii> vii;
typedef map<int,int> mii;
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e9 + 7;
vector<bool> used[2];
int n;
string s;
int ans = 0;
void dfs(int h, int ind , int cur){
	used[h][ind] = true;
	if(ind>=1 && !used[h][ind-1]) dfs(h, ind-1, cur+1);
	if(ind<n-1 && !used[h][ind+1]) dfs(h, ind+1, cur+1);
	if(s[ind]=='1'){
		if(h==1 && !used[0][ind]) dfs(0,ind, cur+1);
		if(h==0 && !used[1][ind]) dfs(1,ind, cur+1);
	}
	ans = max(ans, cur);
}

void solve(){
	cin>>n;
	cin>>s;
	for(int i = 0 ; i < n; i++){
		ans = 0;
		used[0].clear();
		used[1].clear();
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(0,i,1);
		ans = 0;
		used[0].clear();
		used[1].clear();
		used[0] = vector<bool>(n,false);
		used[1] = vector<bool>(n,false);
		dfs(1,i,1);
	}
	cout<<ans<<endl;
}

int main(){
	int T; cin>>T;
	for(int i = 0 ; i < T; i++) solve();
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e9 + 5;
void solve(){
    int n; cin>>n;
    string s; cin>>s;
    int cnt = 0;
    int first=-1,last;
    for(int i = 0; i < n; i++){
        if(s[i]=='1'){
            if(first==-1) first = i;
            last = i;
        }
    }
    if(first==-1) cout<<n<<endl;
    else{

        int ans = min(first,n-last-1);
        cout<<2*(n-ans)<<endl;
    }
}
int main(){
    speed;
    int t;
    cin >> t;
    while(t--) solve();
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e9 + 5;
void solve(){
    int n; cin>>n;
    string s; cin>>s;
    int cnt = 0;
    int first=-1,last;
    for(int i = 0; i < n; i++){
        if(s[i]=='1'){
            if(first==-1) first = i;
            last = i;
        }
    }
    int ans = min(first,n-last-1);
    cout<<ans*2<<endl;
}
int main(){
    speed;
    int t;
    cin >> t;
    while(t--) solve();
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(register int i = a;i <= b;++i)
#define frn(i,a,b) for(register int i = a;i >= b;--i)
#define N 1010
using namespace std;

string s;

inline int read(){
	int x = 0,f = 1;char c = getchar();
	while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}
	while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}
	return f * x;
}

int main(){
	int t = read();
	while(t--){
		int n = read(),ans = n;
		cin>>s;
		fr(i,1,n)
			if(s[i - 1] == '1')ans = max(ans,max(i,n - i + 1) * 2);
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(register int i = a;i <= b;++i)
#define frn(i,a,b) for(register int i = a;i >= b;--i)
#define N 1010
using namespace std;

string s;

inline int read(){
	int x = 0,f = 1;char c = getchar();
	while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}
	while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}
	return f * x;
}

int main(){
	int t = read();
	while(t--){
		int n = read(),now1 = 0,now2 = 0;
		cin>>s;
		int len = s.size();
		if(s[0] == '1' || s[len - 1] == '1'){printf("%d\n",n * 2);continue;}
		fr(i,0,len - 1)if(s[i] == '1'){now1 = i + 1;break;}
		frn(i,len - 1,0)if(s[i] == '1'){now2 = i + 1;break;}
		if(!now1)printf("%d\n",n);
		else printf("%d\n",max(n,max(now1,n - now2 + 1) * 2));
	}
	return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(register int i = a;i <= b;++i)
#define frn(i,a,b) for(register int i = a;i >= b;--i)
#define N 1010
using namespace std;

string s;

inline int read(){
	int x = 0,f = 1;char c = getchar();
	while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}
	while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}
	return f * x;
}

int main(){
	int t = read();
	while(t--){
		int n = read(),now1 = 0,now2 = 0;
		cin>>s;
		int len = s.size();
		if(s[0] == '1' || s[len - 1] == '1'){printf("%d\n",n * 2);continue;}
		fr(i,0,len - 1)if(s[i] == '1'){now1 = i + 1;break;}
		frn(i,len - 1,0)if(s[i] == '1'){now2 = i + 1;break;}
		if(!now1)printf("%d\n",n);
		else printf("%d\n",max(n - now1 + 1,n - now2 + 1) * 2);
	}
	return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(register int i = a;i <= b;++i)
#define frn(i,a,b) for(register int i = a;i >= b;--i)
#define N 1010
using namespace std;

string s;

inline int read(){
	int x = 0,f = 1;char c = getchar();
	while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}
	while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}
	return f * x;
}

int main(){
	int t = read();
	while(t--){
		int n = read(),ans = n;
		cin>>s;
		fr(i,1,n)
			if(s[i - 1] == '1')ans = max(ans,max(i,n - i + 1) * 2);
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define CLR(a)              memset(a, 0, sizeof(a))
#define SET(a, x)           memset(a, x, sizeof(a))
#define SORT(v)             sort(v.begin(), v.end())
#define REV(v)              reverse(v.begin(), v.end())
#define FOR(i, n)           for (int i=0; i<n; i++)
#define FOR1(i, n)          for (int i=1; i<=n; i++)
#define ROF(i, n)           for (int i=n-1; i>=0; i--)
#define ROF1(i, n)          for (int i=n; i>=1; i--)
#define LOOP(i, x, n)       for (int i=x; i<=n; i++)
#define READ(x)             freopen(x, "r", stdin);
#define WRITE(x)            freopen(x, "w", stdout);
#define fastIO()            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI                  acos(-1.0)
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define all(v)              v.begin(), v.end()

using namespace std;

typedef long long              ll;
typedef unsigned long long     ull;
typedef vector<int>            vi;
typedef pair<int, int>         pii;
typedef pair<string, int>      psi;
typedef pair<char, int>        pci;

const double eps = 1e-9;

int fx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, -1, 1, -1, 1, -1, 1};

int kx[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky[] = {2, -2, 1, -1, 2, -2, 1, -1};

int TLx[] = {0, -1, -1, 0};    int TLy[] = {-1, -1, 0, 0};
int TRx[] = {-1, -1, 0, 0};    int TRy[] = {0, 1, 1, 0};
int BLx[] = {0, 1, 1, 0};      int BLy[] = {-1, -1, 0, 0};
int BRx[] = {0, 1, 1, 0};      int BRy[] = {1, 1, 0, 0};

int main()
{
    int T; scanf("%d", &T);
    while(T--){
        int n; scanf("%d", &n);
        string s; cin >> s;
        int mx = 0, cnt = 0;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == '1'){
                cnt++;
                mx = max(mx, max(i+1, n - i));
            }
        }
        if (cnt == 1 && mx < n) printf("%d\n", n + 1);
        else printf("%d\n", n + mx);
    }
    return 0;
}


#include<bits/stdc++.h>
#define CLR(a)              memset(a, 0, sizeof(a))
#define SET(a, x)           memset(a, x, sizeof(a))
#define SORT(v)             sort(v.begin(), v.end())
#define REV(v)              reverse(v.begin(), v.end())
#define FOR(i, n)           for (int i=0; i<n; i++)
#define FOR1(i, n)          for (int i=1; i<=n; i++)
#define ROF(i, n)           for (int i=n-1; i>=0; i--)
#define ROF1(i, n)          for (int i=n; i>=1; i--)
#define LOOP(i, x, n)       for (int i=x; i<=n; i++)
#define READ(x)             freopen(x, "r", stdin);
#define WRITE(x)            freopen(x, "w", stdout);
#define fastIO()            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI                  acos(-1.0)
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define all(v)              v.begin(), v.end()

using namespace std;

typedef long long              ll;
typedef unsigned long long     ull;
typedef vector<int>            vi;
typedef pair<int, int>         pii;
typedef pair<string, int>      psi;
typedef pair<char, int>        pci;

const double eps = 1e-9;

int fx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, -1, 1, -1, 1, -1, 1};

int kx[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky[] = {2, -2, 1, -1, 2, -2, 1, -1};

int TLx[] = {0, -1, -1, 0};    int TLy[] = {-1, -1, 0, 0};
int TRx[] = {-1, -1, 0, 0};    int TRy[] = {0, 1, 1, 0};
int BLx[] = {0, 1, 1, 0};      int BLy[] = {-1, -1, 0, 0};
int BRx[] = {0, 1, 1, 0};      int BRy[] = {1, 1, 0, 0};

int main()
{
    int T; scanf("%d", &T);
    while(T--){
        int n; scanf("%d", &n);
        string s; cin >> s;
        int mx = 0;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == '1'){
                mx = max(mx, max(i+1, n-i));
            }
        }
        printf("%d\n", max(2*mx, n));
    }
    return 0;
}


#include<bits/stdc++.h>
#define CLR(a)              memset(a, 0, sizeof(a))
#define SET(a, x)           memset(a, x, sizeof(a))
#define SORT(v)             sort(v.begin(), v.end())
#define REV(v)              reverse(v.begin(), v.end())
#define FOR(i, n)           for (int i=0; i<n; i++)
#define FOR1(i, n)          for (int i=1; i<=n; i++)
#define ROF(i, n)           for (int i=n-1; i>=0; i--)
#define ROF1(i, n)          for (int i=n; i>=1; i--)
#define LOOP(i, x, n)       for (int i=x; i<=n; i++)
#define READ(x)             freopen(x, "r", stdin);
#define WRITE(x)            freopen(x, "w", stdout);
#define fastIO()            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI                  acos(-1.0)
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define all(v)              v.begin(), v.end()

using namespace std;

typedef long long              ll;
typedef unsigned long long     ull;
typedef vector<int>            vi;
typedef pair<int, int>         pii;
typedef pair<string, int>      psi;
typedef pair<char, int>        pci;

const double eps = 1e-9;

int fx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, -1, 1, -1, 1, -1, 1};

int kx[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky[] = {2, -2, 1, -1, 2, -2, 1, -1};

int TLx[] = {0, -1, -1, 0};    int TLy[] = {-1, -1, 0, 0};
int TRx[] = {-1, -1, 0, 0};    int TRy[] = {0, 1, 1, 0};
int BLx[] = {0, 1, 1, 0};      int BLy[] = {-1, -1, 0, 0};
int BRx[] = {0, 1, 1, 0};      int BRy[] = {1, 1, 0, 0};

int main()
{
    int T; scanf("%d", &T);
    while(T--){
        int n; scanf("%d", &n);
        string s; cin >> s;
        int mx = 0;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == '1'){
                mx = max(mx, max(i+1, n-i));
            }
        }
        printf("%d\n", 2*mx);
    }
    return 0;
}


#include<bits/stdc++.h>
#define CLR(a)              memset(a, 0, sizeof(a))
#define SET(a, x)           memset(a, x, sizeof(a))
#define SORT(v)             sort(v.begin(), v.end())
#define REV(v)              reverse(v.begin(), v.end())
#define FOR(i, n)           for (int i=0; i<n; i++)
#define FOR1(i, n)          for (int i=1; i<=n; i++)
#define ROF(i, n)           for (int i=n-1; i>=0; i--)
#define ROF1(i, n)          for (int i=n; i>=1; i--)
#define LOOP(i, x, n)       for (int i=x; i<=n; i++)
#define READ(x)             freopen(x, "r", stdin);
#define WRITE(x)            freopen(x, "w", stdout);
#define fastIO()            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI                  acos(-1.0)
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define all(v)              v.begin(), v.end()

using namespace std;

typedef long long              ll;
typedef unsigned long long     ull;
typedef vector<int>            vi;
typedef pair<int, int>         pii;
typedef pair<string, int>      psi;
typedef pair<char, int>        pci;

const double eps = 1e-9;

int fx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, -1, 1, -1, 1, -1, 1};

int kx[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky[] = {2, -2, 1, -1, 2, -2, 1, -1};

int TLx[] = {0, -1, -1, 0};    int TLy[] = {-1, -1, 0, 0};
int TRx[] = {-1, -1, 0, 0};    int TRy[] = {0, 1, 1, 0};
int BLx[] = {0, 1, 1, 0};      int BLy[] = {-1, -1, 0, 0};
int BRx[] = {0, 1, 1, 0};      int BRy[] = {1, 1, 0, 0};

int main()
{
    int T; scanf("%d", &T);
    while(T--){
        int n; scanf("%d", &n);
        string s; cin >> s;
        int mx = 0, cnt = 0;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == '1'){
                cnt++;
                mx = max(mx, max(i+1, n - i));
            }
        }
        if (cnt == 1) printf("%d\n", n + 1);
        else printf("%d\n", n + mx);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;
    int min,max,tmp2;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        min = 10001;
        max = 0;
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < n;j++){
            if(str[j] == '1'){
                if(j < min){
                    min = j+1;
                }
                if(j > max){
                    max = j+1;
                }
            }
        }

        if(min == 10001){
            cout << n << endl;
            continue;
        }

        //cout << max << min << " --- " << endl;

        min = n-min+1;

        if(max <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*max;
        }

        if(min <= n/2){
            tmp2 = n+1;
        }else{
            tmp2 = 2*min;
        }

        if(tmp > tmp2){
            hasil = tmp;
        }else{
            hasil = tmp2;
        }

/*
        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            //reverse(str.begin(), str.end());
        }
*/
        cout <<  hasil <<  endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;
    int min,max,tmp2;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        min = 10001;
        max = 0;
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < n;j++){
            if(str[j] == '1'){
                if(j <= min){
                    min = j+1;
                }
                if(j >= max){
                    max = j+1;
                }
            }
        }

        if(min == 10001){
            cout << n << endl;
            continue;
        }

        //cout << max <<" -- "<< min << " --- " << endl;

        min = n-min+1;

        if(max <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*max;
        }

        if(min <= n/2){
            tmp2 = n+1;
        }else{
            tmp2 = 2*min;
        }

        if(tmp > tmp2){
            hasil = tmp;
        }else{
            hasil = tmp2;
        }

/*
        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            //reverse(str.begin(), str.end());
        }.
*/
        cout <<  hasil <<  endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;
    int min,max,tmp2;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        min = 10001;
        max = 0;
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < n;j++){
            if(str[j] == '1'){
                if(j+1 < min){
                    min = j+1;
                }
                if(j+1 > max){
                    max = j+1;
                }
            }
        }

        if(min == 10001){
            cout << n << endl;
            continue;
        }

        //cout << max <<" -- "<< min << " --- " << endl;

        min = n-min+1;

        if(max <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*max;
        }

        if(min <= n/2){
            tmp2 = n+1;
        }else{
            tmp2 = 2*min;
        }

        if(tmp > tmp2){
            hasil = tmp;
        }else{
            hasil = tmp2;
        }

/*
        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            //reverse(str.begin(), str.end());
        }.
*/
        cout <<  hasil <<  endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;
    int min,max;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        min = 10001;
        max = 0;
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < n;j++){
            if(str[j] == '1'){
                if(j < min){
                    min = j+1;
                }
                if(j > max){
                    max = j+1;
                }
            }
        }

        if(min == 10001){
            cout << n << endl;
            continue;
        }

        //cout << max << min << " --- " << endl;

        min = n-min+1;

        if(max <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*max;
        }

        if(min <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*min;
        }

/*
        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            //reverse(str.begin(), str.end());
        }
*/
        cout <<  tmp <<  endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            reverse(str.begin(), str.end());
        }

        cout <<  hasil <<  endl;
        hasil = 0;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t,n,tmp,hasil;
    int min,max,tmp2;

    string str;
    hasil = 0;

    cin >> t;

    for(int i = 0;i < t;i++){
        min = 10001;
        max = 0;
        tmp = 0;
        cin >> n;
        cin >> str;

        for(int j = 0;j < n;j++){
            if(str[j] == '1'){
                if(j < min){
                    min = j+1;
                }
                if(j > max){
                    max = j+1;
                }
            }
        }

        if(min == 10001){
            cout << n << endl;
            continue;
        }

        //cout << max << min << " --- " << endl;

        min = n-min+1;

        if(max <= n/2){
            tmp = n+1;
        }else{
            tmp = 2*max;
        }

        if(min <= n/2){
            tmp2 = n+1;
        }else{
            tmp2 = 2*min;
        }

        if(tmp > tmp2){
            hasil = tmp;
        }else{
            hasil = tmp2;
        }

/*
        for(int j = 0;j < 2;j++){
            for(int k = 0;k < n;k++){
                if(str[k] == '1'){
                    tmp = k+1;
                }
            }

            //cout << tmp << "--" << endl;
            if(tmp == 0){
                if(n > hasil){
                    hasil = n;
                }
            }else{
                if(n-tmp+1 > tmp){
                    hasil = n-tmp+1;
                }else{
                    hasil = 2*tmp;
                }
            }

            //reverse(str.begin(), str.end());
        }.
*/
        cout <<  hasil <<  endl;
    }

}


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

int main() {
  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    if (s[0] == '1' || s[n-1] == '1') {
      cout << 2*n << endl;
    }

    else {

      int c = 0;

      for (int x = 0; x < n; x++) {
        if (s[x] == '1') {
          c++;
        }
      }

      cout << n + c << endl;
    }
  }
}


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

int main() {
  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    if (s[0] == '1' || s[n-1] == '1') {
      cout << 2*n << endl;
    }

    else {

      int pL = 0, pR = 0;
      bool y = true;

      for (int x = 0; x < n; x++) {
        if (s[x] == '1') {
          pL = x;
          y = false;
          break;
        }
      }

      if (y) {
        cout << n << endl;
      }

      else {
        for (int x = n-1; x >= 0; x--) {
          if (s[x] == '1') {
            pR = x;
            y = false;
            break;
          }
        }

        cout << max(n-pL, pR) * 2 << endl;
      }
    }
  }
}


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

int main() {
  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    if (s[0] == '1' || s[n-1] == '1') {
      cout << 2*n << endl;
    }

    else {

      int p = 0;
      bool y = true;

      for (int x = 0; x < n; x++) {
        if (s[x] == '1') {
          p = x;
          y = false;
          break;
        }
      }

      if (y) {
        cout << n << endl;
      }

      else {
        cout << (p+1) * 2 << endl;
      }
    }
  }
}


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

int main() {
  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    if (s[0] == '1' || s[n-1] == '1') {
      cout << 2*n << endl;
    }

    else {

      int p = 0;
      bool y = true;

      for (int x = 0; x < n; x++) {
        if (s[x] == '1') {
          p = x;
          y = false;
          break;
        }
      }

      if (y) {
        cout << n << endl;
      }

      else {
        cout << max((p+1) * 2, (n-p) * 2) << endl;
      }
    }
  }
}


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

int main() {
  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    if (s[0] == '1' || s[n-1] == '1') {
      cout << 2*n << endl;
    }

    else {

      int pL = 0, pR = 0;
      bool y = true;

      for (int x = 0; x < n; x++) {
        if (s[x] == '1') {
          pL = x;
          y = false;
          break;
        }
      }

      if (y) {
        cout << n << endl;
      }

      else {
        for (int x = n-1; x >= 0; x--) {
          if (s[x] == '1') {
            pR = x;
            break;
          }
        }

        cout << max(n-pL, pR+1) * 2 << endl;
      }
    }
  }
}


#include <iostream>
#define yo if(a[i]==0)\
	        z++;\
	        else if(a[i]==1)\
	        y++;
#define yp a[j]=num%10;\
	        num=num/10;\
	        j++;
#define ki if(a[i]==0)\
	                s=s+1;\
	                else\
	                s=s+2;
#define size 1000
#define ops if(z==nu)\
	    cout<<nu<<"\n";\
	    else if(y==nu)\
	    cout<<2*nu<<"\n";
#define loop for(i=0;i<nu;i++)
#define ya cin>>t;\
	while(t--)
#define pados else if((a[0]==0 && a[nu-1]==1) || (a[0]==1 && a[nu-1]==0)|| (a[0]==1 && a[nu-1]==1))\
	        {\
	            cout<<2*nu<<"\n";\
	        }
#define uff if(a[0]==0 && a[nu-1]==0)
using namespace std;

int main() {
	int t,n,nu,i,s,z,y,j,num;
	int a[size];
	ya
	{
	    cin>>n;
	    nu=n;
	    cin>>num;
	    j=0;
	    while(n--)
	    {
	        yp
	    }
	    z=0,y=0;
	    loop
	    {
	        yo
	    }
	    ops
	    else
	    {
	        uff
	        {
	            s=0;
	            loop
	            {
	                ki
	            }
                cout<<s<<"\n";
	        }
	        pados
	    }
	}
	return 0;
}


#include <iostream>
#define yp scanf("%1d",&a[j]);\
if(a[j]==0)\
	        z++;\
	        else if(a[j]==1)\
	        y++;\
	        num=num/10;\
	        j++;
#define ki if(a[i]==0)\
	                s=s+1;\
	                else\
	                s=s+2;
#define size 1000
#define ops if(z==nu)\
	    cout<<nu<<"\n";\
	    else if(y==nu)\
	    cout<<2*nu<<"\n";
#define loop for(i=0;i<nu;i++)
#define ya cin>>t;\
	while(t--)
#define pados else if((a[0]==0 && a[nu-1]==1) || (a[0]==1 && a[nu-1]==0)|| (a[0]==1 && a[nu-1]==1))\
	        {\
	            cout<<2*nu<<"\n";\
	        }
#define uff if(a[0]==0 && a[nu-1]==0)
using namespace std;

int main() {
	int t,n,nu,i,s,z,y,j,num;
	int a[size];
	ya
	{
	    cin>>n;
	    nu=n;
	    j=0;
	    while(n--)
	    {
	        yp
	    }
	    z=0,y=0;

	    ops
	    else
	    {
	        uff
	        {
	            s=0;
	            loop
	            {
	                ki
	            }
                cout<<s<<"\n";
	        }
	        pados
	    }
	}
	return 0;
}


#include <iostream>
#define yp a[j]=num%10;\
if(a[i]==0)\
	        z++;\
	        else if(a[i]==1)\
	        y++;\
	        num=num/10;\
	        j++;
#define ki if(a[i]==0)\
	                s=s+1;\
	                else\
	                s=s+2;
#define size 1000
#define ops if(z==nu)\
	    cout<<nu<<"\n";\
	    else if(y==nu)\
	    cout<<2*nu<<"\n";
#define loop for(i=0;i<nu;i++)
#define ya cin>>t;\
	while(t--)
#define pados else if((a[0]==0 && a[nu-1]==1) || (a[0]==1 && a[nu-1]==0)|| (a[0]==1 && a[nu-1]==1))\
	        {\
	            cout<<2*nu<<"\n";\
	        }
#define uff if(a[0]==0 && a[nu-1]==0)
using namespace std;

int main() {
	int t,n,nu,i,s,z,y,j,num;
	int a[size];
	ya
	{
	    cin>>n;
	    nu=n;
	    cin>>num;
	    j=0;
	    while(n--)
	    {
	        yp
	    }
	    z=0,y=0;

	    ops
	    else
	    {
	        uff
	        {
	            s=0;
	            loop
	            {
	                ki
	            }
                cout<<s<<"\n";
	        }
	        pados
	    }
	}
	return 0;
}


#include <iostream>
#define yp scanf("%1d",&a[j]);\
if(a[j]==0)\
	        z++;\
	        else if(a[j]==1)\
	        y++;\
	        num=num/10;\
	        j++;

#define size 1000
#define ops if(z==nu)\
	    cout<<nu<<"\n";\
	    else if(y==nu)\
	    cout<<2*nu<<"\n";
#define loop for(i=0;i<nu;i++)
#define ya cin>>t;\
	while(t--)
#define pados else if((a[0]==0 && a[nu-1]==1) || (a[0]==1 && a[nu-1]==0)|| (a[0]==1 && a[nu-1]==1))\
	        {\
	            cout<<2*nu<<"\n";\
	        }
#define uff if(a[0]==0 && a[nu-1]==0)
using namespace std;

int main() {
	int t,n,nu,i,s,z,y,j,num,q;
	int a[size];
	ya
	{
	    cin>>n;
	    nu=n;
	    j=0;z=0,y=0;
	    while(n--)
	    {
	        yp
	    }

	    ops
	    else
	    {
	        uff
	        {
	            s=0;
	            loop
	            {
	                if(a[i]==0)
	                s=s+1;
	                else
	                break;
	            }
	            q=0;
	            for(i=nu-1; i>=0; i--)
	            {
	                if(a[i]==0)
	                q=q+1;
	                else
	                break;

	            }

	            if(s<=q)
                cout<<2*(nu-s)<<"\n";
                else
                cout<<2*(nu-q)<<"\n";
	        }
	        pados
	    }
	}
	return 0;
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {
        int firstO = 0;
        for(firstO = 0; firstO < n; firstO++) {
            if(s[firstO] ==  '0')
                break;
        }
        firstO += 1;

        int last1;
        if(firstO > n / 2) {      //if firstO is in the right
            for(last1 = 0; last1 < n; last1++) {
                if(s[last1] == '1')
                    break;
            }
        }
        else {
            for(last1 = n - 1; last1 >= 0; last1--) {
                if(s[last1] == '1')
                    break;
            }
        }
        last1 += 1;

        printf("%d %d\n", firstO, last1);

        int path = abs(last1 - firstO) + 1 + max(last1, n - last1 + 1);
        cout << path << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {

        int ans;

        int l = -1, r = -1, k;
        int i;
        for(i = n - 1; i >= 0; i--)
        {
            if(s[i] == '1')
            {
                r = i + 1;
                break;
            }
        }

        for(i = 0; i < n; i++)
        {
            if(s[i] == '1')
            {
                l = i + 1;
                break;
            }
        }

        if(l != -1)
        {
            // ans = (n - l + 1) + max(l, n - l + 1);
            // ans = max(ans, k);

            // k = (n - r + 1) + max(r, n - r + 1);
            // ans = max(ans, k);

            //start from leftmost
            ans = r + max(r, n - r + 1);

            //start from rightmost
            k = (n - l + 1) + max(l, n - l + 1);
            ans = max(ans, k);
        }
        cout << ans << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {
        int firstO = 0;
        int i, j;
        for(i = 0, j = n - 1; i < n && j >= 0; i++, j--) {
            if(s[i] == '0' || s[j] == '0')
                break;
        }
        if(s[i] == '1' && s[j] == '0')
            firstO = j;
        else if(s[i] == '0' && s[j] == '1')
            firstO = i;
        firstO += 1;

        int last1;
        if(firstO > n / 2) {      //if firstO is in the right
            for(last1 = 0; last1 < n; last1++) {
                if(s[last1] == '1')
                    break;
            }
        }
        else {
            for(last1 = n - 1; last1 >= 0; last1--) {
                if(s[last1] == '1')
                    break;
            }
        }
        last1 += 1;

        int path = abs(last1 - firstO) + 1 + max(last1, n - last1 + 1);
        cout << path << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {
        int firstO = 0;
        for(firstO = 0; firstO < n; firstO++) {
            if(s[firstO] ==  '0')
                break;
        }
        firstO += 1;

        int last1;
        if(firstO > n / 2) {      //if firstO is in the right
            for(last1 = 0; last1 < n; last1++) {
                if(s[last1] == '1')
                    break;
            }
        }
        else {
            for(last1 = n - 1; last1 >= 0; last1--) {
                if(s[last1] == '1')
                    break;
            }
        }
        last1 += 1;

        int path = abs(last1 - firstO) + 1 + max(last1, n - last1 + 1);
        cout << path << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {
        // int firstO = 0;
        // int i, j;
        // for(i = 0, j = n - 1; i < n && j >= 0; i++, j--) {
        //     if(s[i] == '0' || s[j] == '0')
        //         break;
        // }
        // if(s[i] == '1' && s[j] == '0')
        //     firstO = j;
        // else if(s[i] == '0' && s[j] == '1')
        //     firstO = i;
        // else if(s[i] == '0' && s[j] == '0')
        //     firstO = i;
        // firstO += 1;

        // int last1;
        // if(firstO > n / 2) {      //if firstO is in the right
        //     for(last1 = 0; last1 < n; last1++) {
        //         if(s[last1] == '1')
        //             break;
        //     }
        // }
        // else {
        //     for(last1 = n - 1; last1 >= 0; last1--) {
        //         if(s[last1] == '1')
        //             break;
        //     }
        // }
        // last1 += 1;

        // int path = abs(last1 - firstO) + 1 + max(last1, n - last1 + 1);
        // cout << path << endl;

        int ans = n;

        int l = -1, r = -1, k;
        int i;
        for(i = n - 1; i >= 0; i--)
        {
            if(s[i] == '1')
            {
                r = i + 1;
                break;
            }
        }

        for(i = 0; i < n; i++)
        {
            if(s[i] == '1')
            {
                l = i + 1;
                break;
            }
        }

        if(l != -1)
        {
            //if starts in the rightmost cell
            k = (n - l + 1) + max(l, n - l + 1);
            ans = max(ans, k);

            // if starts in the leftmost cell
            k = (n - r + 1) + max(r, n - r + 1);
            ans = max(ans, k);
        }
        cout << ans << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

using namespace std;

void test() {
    int n;
    string s;
    cin >> n >> s;

    int count[2] = {0};
    for(int i = 0; i < n; i++) {
        count[s[i] - '0']++;
    }

    if(count[0] == n)
        cout << n << endl;
    else if(count[1] == n)
        cout << 2 * n << endl;
    else {
        int firstO = 0;
        for(firstO = 0; firstO < n; firstO++) {
            if(s[firstO] ==  '0')
                break;
        }
        firstO += 1;

        int last1;
        if(firstO >= n / 2) {      //if firstO is in the right
            for(last1 = 0; last1 < n; last1++) {
                if(s[last1] == '1')
                    break;
            }
        }
        else {
            for(last1 = n - 1; last1 >= 0; last1--) {
                if(s[last1] == '1')
                    break;
            }
        }
        last1 += 1;

        int path = abs(last1 - firstO) + 1 + max(last1, n - last1 + 1);
        cout << path << endl;
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


 #include<bits/stdc++.h>
using namespace std;
int main(){
int q,n,r,k;
cin>>q;
vector<char>v;
for(int i=0;i<q;i++){
cin>>n;
v.resize(n);
int k=0,g=0;
for(int j=0;j<n;j++)
{cin>>v[j];
if(v[j]!='0')
{k++;
if(k>1)
{if(g>(n-j-1))
g=n-j-1;}
else
g=j;
}}
if(k!=0){
if((v[0]=='1')||(v[n-1]=='1'))
cout<<2*n<<"\n";
else
cout<<2*(n-g)<<"\n";}
else
cout<<n<<"\n";}

}


 #include<bits/stdc++.h>
using namespace std;
int main(){
int q,n,r,k;
cin>>q;
vector<char>v;
for(int i=0;i<q;i++){
cin>>n;
v.resize(n);
int k=0,g=0;
for(int j=0;j<n;j++)
{cin>>v[j];
if(v[j]!='0')
{k++;
if(k>1)
{if(g>(n-j))
g=n-j;}
else
g=j;
}}
if(k!=0){
if((v[0]=='1')||(v[n-1]=='1'))
cout<<2*n<<"\n";
else
cout<<2*(n-g)<<"\n";}
else
cout<<n<<"\n";}

}


 #include<bits/stdc++.h>
using namespace std;
int main(){
int q,n,r,k;
cin>>q;
vector<char>v;
for(int i=0;i<q;i++){
cin>>n;
v.resize(n);
int k=0,g=0;
for(int j=0;j<n;j++)
{cin>>v[j];
if(v[j]!='0')
{k++;
if(g<(n-j-1))
g=j+1;
else
g=n-j-1;}}
if(k!=0){
if((v[0]=='1')||(v[n-1]=='1'))
cout<<2*n<<"\n";
else
cout<<2*g<<"\n";}
else
cout<<n<<"\n";}

}


 #include<bits/stdc++.h>
using namespace std;
int main(){
int q,n,r,k;
cin>>q;
vector<char>v;
for(int i=0;i<q;i++){
cin>>n;
v.resize(n);
int k=0,g=0;
for(int j=0;j<n;j++)
{cin>>v[j];
if(v[j]!='0')
{k++;
if(g<(n-j-1))
g=j+1;
else
g=n-j-1;}}
if(k!=0){
if((v[0]=='1')||(v[n-1]=='1'))
cout<<2*n<<"\n";
else
cout<<2*g<<"\n";}
else
cout<<n;}

}


 #include<bits/stdc++.h>
using namespace std;
int main(){
int q,n,r,k;
cin>>q;
vector<char>v;
for(int i=0;i<q;i++){
cin>>n;
v.resize(n);
int k=0,g=0;
for(int j=0;j<n;j++)
{cin>>v[j];
if(v[j]!='0')
{k++;
if(k>1)
{if(g>(n-j-1))
g=j+1;}
else
g=j+1;
}}
if(k!=0){
if((v[0]=='1')||(v[n-1]=='1'))
cout<<2*n<<"\n";
else
cout<<2*(n-g+1)<<"\n";}
else
cout<<n;}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,n,t,p,m,ans,r;
    char a[1001];
    cin >> t;
    while(t--)
    {
        r=0;
        cin >> p;
        for(i=0;i<p;i++)
        {
            cin >> a[i];
        }
        for(i=0;i<p;i++)
        {
            if(a[i]=='1')
            {
                m=i+1;
                r=5;
                break;
            }
        }
        for(i=p-1;i>=1;i--)
        {
            if(a[i]=='1')
            {
                n=i+1;
                break;
            }
        }
        if(p==m) ans=2*p;
        else if(p-m>=p-n)
        {
            ans=2*(p-m+1);
        }
        else
        {
            ans=2*(p-n+1);
        }
        if(r==0) ans=p;
        cout << ans << endl;
    }
    return 0;
}


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

typedef long long int ll;
#define fori(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define foria(i, a, b) for (ll(i) = (a); (i) <= (b); (i)++)
#define foriar(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define ff first
#define ss second
#define pb push_back
#define pob pop_back
#define endl '\n'
const ll mod = 1e9 + 7;

void solve()
{
    string s, a ,b;
    cin >> s >> a >> b;
    ll c = s.find(a);
    bool f = 0, r = 0;
    if(c != -1)
    {
        foria(i, c + a.size(), s.size() - 1)
        {
            if(i + b.size() <= s.size() and s.substr(i, b.size()) == b)
            {
                f=1;
                break;
            }
        }
    }
    reverse(s.begin(), s.end());
    c = 0;
    c = s.find(a);
    if(c != -1)
    {
        foria(i, c + a.size(), s.size() - 1)
        {
            if(i + b.size() <= s.size() and s.substr(i, b.size()) == b)
            {
                r=1;
                break;
            }
        }
    }
    if(f == 1 and r == 1)
    {
        cout << "both" << endl;
    }
    else if(f == 1)
    {
        cout << "forward" << endl;
    }
    else if(r == 1)
    {
        cout << "backward" << endl;
    }
    else
    {
        cout << "fantasy" << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // ifstream cin("input.txt");
    // ofstream cout("output.txt");

    ll t = 1;
    // cin >> t;
    for(ll _ = 0; _ < t; _++)
    {
        // cout << "Case #" << _ + 1 << ": ";
        solve();
    }
    return 0;
}


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

typedef long long int ll;
#define fori(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define foria(i, a, b) for (ll(i) = (a); (i) <= (b); (i)++)
#define foriar(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define ff first
#define ss second
#define pb push_back
#define pob pop_back
#define endl '\n'
const ll mod = 1e9 + 7;

void solve()
{
    string s, a ,b;
    cin >> s >> a >> b;
    ll c = s.find(a);
    bool f = 0, r = 0;
    if(c != -1)
    {
        foria(i, c + 1, s.size())
        {
            if(i + b.size() <= s.size() and s.substr(i, b.size()) == b)
            {
                f=1;
                break;
            }
        }
    }
    reverse(s.begin(), s.end());
    c = 0;
    c = s.find(a);
    if(c != -1)
    {
        foria(i, c + 1, s.size())
        {
            if(i + b.size() <= s.size() and s.substr(i, b.size()) == b)
            {
                r=1;
                break;
            }
        }
    }
    if(f == 1 and r == 1)
    {
        cout << "both" << endl;
    }
    else if(f == 1)
    {
        cout << "forward" << endl;
    }
    else if(r == 1)
    {
        cout << "backward" << endl;
    }
    else
    {
        cout << "fantasy" << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // ifstream cin("input.txt");
    // ofstream cout("output.txt");

    ll t = 1;
    // cin >> t;
    for(ll _ = 0; _ < t; _++)
    {
        // cout << "Case #" << _ + 1 << ": ";
        solve();
    }
    return 0;
}


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

typedef long long int ll;
#define fori(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define foria(i, a, b) for (ll(i) = (a); (i) <= (b); (i)++)
#define foriar(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define ff first
#define ss second
#define pb push_back
#define pob pop_back
#define endl '\n'
const ll mod = 1e9 + 7;

void solve()
{
    string s, a ,b;
    cin >> s >> a >> b;
    ll c = s.find(a);
    bool f = 0, r = 0;
    if(c != -1)
    {
        foria(i, c, s.size())
        {
            if(s.substr(i, b.size()) == b)
            {
                f=1;
                break;
            }
        }
    }
    reverse(s.begin(), s.end());
    c = 0;
    c = s.find(a);
    if(c != -1)
    {
        foria(i, c, s.size())
        {
            if(s.substr(i, b.size()) == b)
            {
                r=1;
                break;
            }
        }
    }
    if(f == 1 and r == 1)
    {
        cout << "both" << endl;
    }
    else if(f == 1)
    {
        cout << "forward" << endl;
    }
    else if(r == 1)
    {
        cout << "backward" << endl;
    }
    else
    {
        cout << "fantasy" << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // ifstream cin("input.txt");
    // ofstream cout("output.txt");

    ll t = 1;
    // cin >> t;
    for(ll _ = 0; _ < t; _++)
    {
        // cout << "Case #" << _ + 1 << ": ";
        solve();
    }
    return 0;
}


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

typedef long long int ll;
#define fori(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define foria(i, a, b) for (ll(i) = (a); (i) <= (b); (i)++)
#define foriar(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define ff first
#define ss second
#define pb push_back
#define pob pop_back
#define endl '\n'
const ll mod = 1e9 + 7;

void solve()
{
    string s, a ,b;
    cin >> s >> a >> b;
    ll c = s.find(a);
    bool f = 0, r = 0;
    if(c != -1)
    {
        foria(i, c + 1, s.size())
        {
            if(i + b.size() < s.size() and s.substr(i, b.size()) == b)
            {
                f=1;
                break;
            }
        }
    }
    reverse(s.begin(), s.end());
    c = 0;
    c = s.find(a);
    if(c != -1)
    {
        foria(i, c + 1, s.size())
        {
            if(i + b.size() < s.size() and s.substr(i, b.size()) == b)
            {
                r=1;
                break;
            }
        }
    }
    if(f == 1 and r == 1)
    {
        cout << "both" << endl;
    }
    else if(f == 1)
    {
        cout << "forward" << endl;
    }
    else if(r == 1)
    {
        cout << "backward" << endl;
    }
    else
    {
        cout << "fantasy" << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // ifstream cin("input.txt");
    // ofstream cout("output.txt");

    ll t = 1;
    // cin >> t;
    for(ll _ = 0; _ < t; _++)
    {
        // cout << "Case #" << _ + 1 << ": ";
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) (x.begin(), x.end())
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    bool fo = false, ba = false;
    ll pos;
    string s4;
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        s4 = s1.substr(pos);
        if(s4.find(s3) != string::npos) {
            fo = true;
        }
    }
    reverse(s1.begin(), s1.end());
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        s4 = s1.substr(pos);
        if(s4.find(s3) != string::npos) {
            ba = true;
        }
    }
    if(fo && !ba) cout << "forward\n";
    else if(!fo && ba) cout << "backward\n";
    if(fo && ba) cout << "both\n";
    if(!fo && !ba) cout << "fantasy\n";

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) (x.begin(), x.end())
#define sz(x) (ll)(x.size())
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    ll l1 = sz(s2);
    ll l2 = sz(s3);
    bool fo = false, ba = false;
    ll pos;
    string s4;
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        pos += (l1);
        s4 = s1.substr(pos);
        if(s4.find(s3) != string::npos) {
            fo = true;
        }
    }
    reverse(s1.begin(), s1.end());
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        pos += (l1);
        s4 = s1.substr(pos);
        if(s4.find(s3) != string::npos) {
            ba = true;
        }
    }
    if(fo && !ba) cout << "forward\n";
    else if(!fo && ba) cout << "backward\n";
    if(fo && ba) cout << "both\n";
    if(!fo && !ba) cout << "fantasy\n";

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) (x.begin(), x.end())
#define sz(x) (ll)(x.size())
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    ll l1 = sz(s2);
    ll l2 = sz(s3);
    bool fo = false, ba = false;
    ll pos;
    string s4;
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        pos += (l1);
        // s4 = s1.substr(pos);
        if(s1.find(s3, pos) != string::npos) {
            fo = true;
        }
    }
    reverse(s1.begin(), s1.end());
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        pos += (l1);
        // s4 = s1.substr(pos);
        if(s1.find(s3, pos) != string::npos) {
            ba = true;
        }
    }
    if(fo && !ba) cout << "forward\n";
    else if(!fo && ba) cout << "backward\n";
    if(fo && ba) cout << "both\n";
    if(!fo && !ba) cout << "fantasy\n";

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) (x.begin(), x.end())
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    bool fo = false, ba = false;
    ll pos;
    string s4;
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        s4 = s1.substr(pos + 1);
        if(s4.find(s3) != string::npos) {
            fo = true;
        }
    }
    reverse(s1.begin(), s1.end());
    if(s1.find(s2) != string::npos) {
        pos = s1.find(s2);
        s4 = s1.substr(pos + 1);
        if(s4.find(s3) != string::npos) {
            ba = true;
        }
    }
    if(fo && !ba) cout << "forward\n";
    else if(!fo && ba) cout << "backward\n";
    if(fo && ba) cout << "both\n";
    if(!fo && !ba) cout << "fantasy\n";

}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1 = -1, pos2 = -1, rpos1 = -1, rpos2 = -1;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    //cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(pos1, seq1.size()); //如果找到了，就从源字符串中清除seq1
        //cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += seq1.size();
        //cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    if(rpos1!=-1)
    {
        reverse = reverse.erase(rpos1, seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += seq1.size();
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1, pos2, rpos1, rpos2;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    //cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(pos1, seq1.size()); //如果找到了，就从源字符串中清除seq1
        pos2 = sequence.rfind(seq2);
        pos2 += seq1.size();
        //cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    if(rpos1!=-1)
    {
        reverse = reverse.erase(rpos1, seq1.size());
        rpos2 = reverse.rfind(seq2);
        rpos2 += seq1.size();
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1, pos2, rpos1, rpos2;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    //cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(pos1, seq1.size()); //如果找到了，就从源字符串中清除seq1
        //cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += seq1.size();
        //cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    if(rpos1!=-1)
    {
        reverse = reverse.erase(rpos1, seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += seq1.size();
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1 = -1, pos2 = -1, rpos1 = -1, rpos2 = -1;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    //cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(0, pos1+seq1.size()); //如果找到了，就从源字符串中清除seq1
        //cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += pos1 + seq1.size();
        //cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    //cout << "rpos1 = " << rpos1 << endl;
    if(rpos1!=-1)
    {
        reverse = reverse.erase(0, rpos1+seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += rpos1 + seq1.size();
        //out << "rpos2 = " << rpos2 << endl;
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;
    int pos1 = sequence.find(seq1);
    int pos2 = sequence.rfind(seq2);
    int rpos1 = reverse.find(seq1);
    int rpos2 = reverse.rfind(seq2);
    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1, pos2, rpos1, rpos2;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(pos1, seq1.size()); //如果找到了，就从源字符串中清除seq1
        cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += seq1.size();
        cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    if(rpos1!=-1)
    {
        reverse = reverse.erase(rpos1, seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += seq1.size();
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1 = -1, pos2 = -1, rpos1 = -1, rpos2 = -1;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(pos1, seq1.size()); //如果找到了，就从源字符串中清除seq1
        cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += seq1.size();
        cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    if(rpos1!=-1)
    {
        reverse = reverse.erase(rpos1, seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += seq1.size();
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;
    int pos1 = sequence.find(seq1);
    int pos2 = sequence.rfind(seq2);
    int rpos1 = reverse.find(seq1);
    int rpos2 = reverse.rfind(seq2);
    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>=rpos2)
        cout << "forward" << endl;
    else if(pos1>=pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    int pos1 = sequence.find(seq1);
    // cout << "pos1 = " << pos1 << endl;
    int pos2 = sequence.find(seq2);
    // cout << "pos2 = " << pos2 << endl;
    int rpos1 = reverse.rfind(seq1);
    int rpos2 = reverse.rfind(seq2);

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>rpos2)
        cout << "forward" << endl;
    else if(pos1>pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string sequence, seq1, seq2, reverse;
    int pos1 = -1, pos2 = -1, rpos1 = -1, rpos2 = -1;
    cin >> sequence >> seq1 >> seq2;
    for(auto it=sequence.cend()-1;it!=sequence.cbegin()-1;--it)
        reverse += *it;

    pos1 = sequence.find(seq1);
    //cout << "pos1 = " << pos1 << endl;
    if(pos1!=-1)
    {
        sequence = sequence.erase(0, pos1+seq1.size()); //如果找到了，就从源字符串中清除seq1
        //cout << "sequence = " << sequence << endl;
        pos2 = sequence.rfind(seq2);
        if(pos2!=-1)
            pos2 += pos1 + seq1.size();
        //cout << "pos2 = " << pos2 << endl;
    }

    rpos1 = reverse.find(seq1);
    //cout << "rpos1 = " << rpos1 << endl;
    if(rpos1!=-1)
    {
        reverse = reverse.erase(0, rpos1+seq1.size());
        rpos2 = reverse.rfind(seq2);
        if(rpos2!=-1)
            rpos2 += rpos1 + seq1.size();
        //out << "rpos2 = " << rpos2 << endl;
    }

    if(pos1<pos2&&rpos1<rpos2)
        cout << "both" << endl;
    else if(pos1<pos2&&rpos1>=rpos2)
        cout << "forward" << endl;
    else if(pos1>=pos2&&rpos1<rpos2)
        cout << "backward" << endl;
    else
        cout << "fantasy" << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());

	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==b[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==b.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==a[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==a.length())
			{
				fd++;
				ctr=0;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;

	bool forward=true;
	bool backward=true;

	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;

			}

		}

	}

	if(fd<2)
		forward=false;
	fd=0;

	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;

	bool forward=true;
	bool backward=true;
	if(a==b)
		forward=false;
	if(a==b)
		backward=false;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;

			}

		}

	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;

			}

		}

	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==a[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==a.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==b[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==b.length())
			{
				fd++;
				ctr=0;
			}

		}
	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(s[i]==b[ctr])
				ctr++;
			else
				ctr=0;
			if(ctr==b.length())
			{	fd++;
				ctr=0;}
		}
		else
		{
			if(s[i]==a[ctr])
				ctr++;
			else
			{
				ctr=0;
			}
			if(ctr==a.length())
			{
				fd++;
				ctr=0;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;

	bool forward=true;
	bool backward=true;

	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=i+a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;

			}

		}

	}

	if(fd<2)
		forward=false;
	fd=0;

	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					{fd++;
						i=i+a.length()-1;}
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	cin>>a;
	string b;
	cin>>b;
	bool forward=true;
	bool backward=true;
	int ctr=0;
	int fd=0;
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					fd++;
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;

			}

		}

	}

	if(fd<2)
		forward=false;
	fd=0;
	ctr=0;
	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		if(fd==0)
		{
			if(i+a.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+a.length();j++)
				{
					if(s[j]==a[j-i])
					{
						ctr++;
					}
				}
				if(ctr==a.length())
					fd++;
			}
		}
		else
		{
			if(i+b.length()>s.length())
				break;
			else
			{
				int ctr=0;
				for(int j=i;j<i+b.length();j++)
				{
					if(s[j]==b[j-i])
					{
						ctr++;
					}
				}
				if(ctr==b.length())
					fd++;
				if(fd>=2)
					break;
			}

		}

	}

	if(fd<2)
		backward=false;
	if(forward==true&&backward==true)
		cout<<"both";
	else if(forward==true)
		cout<<"forward";
	else if(backward==true)
		cout<<"backward";
	else
		cout<<"fantasy";

}


#include <bits/stdc++.h>
using namespace std;
#define optimize() ios::sync_with_stdio(0);cin.tie(0);
typedef long long int ll;
typedef vector<ll>vi;
typedef map<ll,ll>mi;
typedef pair<ll,ll>pll;
typedef vector<pll> vll;
typedef set<ll>st;
typedef set<char>sc;
const int mx =1e3;

int main()
{
    optimize();
    string str,s1,s2;
    cin>>str>>s1>>s2;
    ll l,l1;
    l=str.size();
    l1=s1.size();

    // bool flg=false;
    int k=str.find(s1),t=0;
    if(k<l)
    {
        k=str.find(s2,k+l1);
        if(k<l)
        {
            t=1;
        }
    }
    reverse(str.begin(),str.end());
    k=str.find(s1);
    if(k<l)
    {
        k=str.find(s2,k+l1);
        if(k<l)
        {
            if(t)
            {
                t=3;
            }
            else
                t=2;
        }

    }
    if(!t)
        cout<<"fantasy"<<endl;
    if(t==1)
        cout<<"forward"<<endl;
    if(t==2)
        cout<<"backward"<<endl;
    if(t==3)
        cout<<"both"<<endl;
}


#include<iostream>
#include<cmath>
#include<queue>
#include<string>
#include<algorithm>

using namespace std;
int main(){
    string s, s1, s2;
    cin >> s >> s1 >> s2;
    int k = s.find(s1), t = 0;
    if(k < s.length()){
        k = s.find(s2, k + s1.length());
        if(k < s.length())
            t = 1;
    }
    reverse(s.begin(), s.end());
    k = s.find(s1);
    if(k < s.length()){
        k = s.find(s2, k + s1.length());
        if(k < s.length()){
            if(t)
                t = 3;
            else
                t = 2;
        }
    }
    if(!t)
        cout << "fantasy" << endl;
    if(t == 1)
        cout << "forward" << endl;
    if(t == 2)
        cout << "backward" << endl;
    if(t == 3) cout << "both" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define optimize() ios::sync_with_stdio(0);cin.tie(0);
typedef long long int ll;
typedef vector<ll>vi;
typedef map<ll,ll>mi;
typedef pair<ll,ll>pll;
typedef vector<pll> vll;
typedef set<ll>st;
typedef set<char>sc;
const int mx =1e3;

int main()
{
    optimize();
    string str,s1,s2;
    cin>>str>>s1>>s2;
    // bool flg=false;
    int k=str.find(s1),t=0;
    if(k<str.size())
    {
        k=str.find(s2,k+s1.size());
        if(k<str.size())
        {
            t=1;
        }
    }
    reverse(str.begin(),str.end());
    k=str.find(s1);
    if(k<str.size())
    {
        k=str.find(s2,k+s1.size());
        if(k<str.size())
        {
            if(t)
            {
                t=3;
            }
            else
                t=2;
        }

    }
    if(!t)
        cout<<"fantasy"<<endl;
    if(t==1)
        cout<<"forward"<<endl;
    if(t==2)
        cout<<"backward"<<endl;
    if(t==3)
        cout<<"both"<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,cc=0,ff=0,i,k=0,j;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){f=1;break;}
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){ff=1;break;}
    }
    if(c==1 and f==1)
    {
        if(ff==1 and cc==1)cout<<"both";
        else cout<<"forward";
    }
    else if(c==1 and f==0)
    {
        if(ff==1 and cc==1)cout<<"backward";
        else cout<<"fantasy";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,cc=0,ff=0,i,k=0,j;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){f=1;break;}
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){ff=1;break;}
    }
    if(c==1 and f==1)
    {
        if(ff==1 and cc==1)cout<<"both";
        else cout<<"forward";
    }
    else if(c==1 and f==0)
    {
        if(ff==1 and cc==1)cout<<"backward";
        else cout<<"fantasy";
    }
    else if(c==0)cout<<"fantasy";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,cc=0,ff=0,i,k=0,j;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;k=i;break;}
    }
    for(j=k+1;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){f=1;break;}
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;k=i;break;}
    }
    for(j=k+1;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){ff=1;break;}
    }
    if(c==1 and f==1)
    {
        if(ff==1 and cc==1)cout<<"both";
        else cout<<"forward";
    }
    else if(c==1 and f==0)
    {
        if(ff==1 and cc==1)cout<<"backward";
        else cout<<"fantasy";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,cc=0,ff=0,i,k=0,j;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;k=i;break;}
    }
    for(j=k+1;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){f=1;break;}
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;k=i;break;}
    }
    for(j=k+1;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){ff=1;break;}
    }
    if(c==1 and f==1)
    {
        if(ff==1 and cc==1)cout<<"both";
        else cout<<"forword";
    }
    else if(c==1 and f==0)
    {
        if(ff==1 and cc==1)cout<<"backword";
        else cout<<"fantasy";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int x=0,y=0;
    if(s.find(a)!=-1 and s.find(b,s.find(a)+aa)!=-1)x=1;
    reverse(s.begin(),s.end());
   if(s.find(a)!=-1 and s.find(b,s.find(a)+aa)!=-1)y=1;

    if(x==1 and y==1)cout<<"both";
    else if(x==1)cout<<"forward";
    else if(y==1)cout<<"backward";
    else cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,i,x=0,y=0;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;}
        else if(c!=0 and s.substr(i,bb)==b)x=1;
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;}
        else if(cc!=0 and s.substr(i,bb)==b)y=1;
    }
    if(x==1 and y==1)cout<<"both";
    else if(x==1)cout<<"forward";
    else if(y==1)cout<<"backward";
    else cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,i,cc=0,x=0,y=0;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;}
        else if(c!=0 and s.substr(i,bb)==b)x=1;
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;}
        else if(cc!=0 and s.substr(i,bb)==b)y=1;
    }
    if(x==1 and y==1)cout<<"both";
    else if(x==1)cout<<"forward";
    else if(y==1)cout<<"backward";
    else cout<<"fantasy";

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    int aa=a.size();
    int bb=b.size();
    int c=0,f=0,cc=0,ff=0,i,k=0,j;
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){c=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){f=1;break;}
    }
    reverse(s.begin(),s.end());
    for(i=0;i<s.size();i++)
    {
        if(s.substr(i,aa)==a){cc=1;k=i+aa;break;}
    }
    for(j=k;j<s.size();j++)
    {
        if(s.substr(j,bb)==b){ff=1;break;}
    }
    if(c==1 and f==1)
    {
        if(ff==1 and cc==1)cout<<"both";
        else cout<<"forword";
    }
    else if(c==1 and f==0)
    {
        if(ff==1 and cc==1)cout<<"backword";
        else cout<<"fantasy";

    }
}


#include<bits/stdc++.h>
typedef long long ll;
typedef double ld;
#define vll vector<ll>
#define vvll vector< vll >
#define vld vector< ld >
#define vvld vector< vld >
#define pll pair<ll ,ll >
#define vllp vector< pll >
#define MOD 1000000007
#define endl "\n"
#define MAX 1000000007
#define nl <<endl
#define f(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;

int main()
{
	string s;
	cin>>s;
	string a,b;
	cin>>a>>b;
	ll j=0;
	string p=a+b;
	ll x1=0,x2=0;
	ll i=0;
	for(i = 0; i < s.length(); ++i)
	{
		if(s[i]==a[j]){j++;}
		if(j==a.length()){i++;break;}
	}
	if(j==a.length())
	{
		j=0;
		for (; i < s.length(); ++i)
		{
			if(s[i]==b[j]){j++;}
		}
		if(j==b.length()){x1=1;}
	}
	j=0;
	for (i = s.length()-1; i>=0; --i)
	{
		if(s[i]==a[j]){j++;}
		if(j==a.length()){i--;break;}
	}
	if(j==a.length())
	{
		j=0;
		for (; i>=0; --i)
		{
			if(s[i]==b[j]){j++;}
		}
		if(j==b.length()){x2=1;}
	}

	if(x1+x2==2){cout<<"both";return 0;}
	if(x1+x2==0){cout<<"fantasy";return 0;}
	if(x1){cout<<"forward";return 0;}
	cout<<"backward";
}


#include<bits/stdc++.h>
typedef long long ll;
typedef double ld;
#define vll vector<ll>
#define vvll vector< vll >
#define vld vector< ld >
#define vvld vector< vld >
#define pll pair<ll ,ll >
#define vllp vector< pll >
#define MOD 1000000007
#define endl "\n"
#define MAX 1000000007
#define nl <<endl
#define f(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;

int main()
{
	string s;
	cin>>s;
	string a,b;
	cin>>a>>b;
	ll j=0;
	string p=a+b;
	ll x1=0,x2=0;
	for(ll i = 0; i < s.length(); ++i)
	{
		if(s[i]==p[j]){j++;}
	}
	if(j==p.length()){x1=1;}
	j=0;
	for (ll i = s.length()-1; i>=0; --i)
	{
		if(s[i]==p[j]){j++;}
	}
	if(j==p.length()){x2=1;}
	if(x1+x2==2){cout<<"both";return 0;}
	if(x1+x2==0){cout<<"fantasy";return 0;}
	if(x1){cout<<"forward";return 0;}
	cout<<"backward";
}


#include<bits/stdc++.h>
typedef long long ll;
typedef double ld;
#define vll vector<ll>
#define vvll vector< vll >
#define vld vector< ld >
#define vvld vector< vld >
#define pll pair<ll ,ll >
#define vllp vector< pll >
#define MOD 1000000007
#define endl "\n"
#define MAX 1000000007
#define nl <<endl
#define f(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;

int main()
{
	string s;
	cin>>s;
	string a,b;
	cin>>a>>b;
	ll j=0;
	string p=a+b;
	ll x1=0,x2=0;
	ll i=0;
	for(i = 0; i < s.length(); ++i)
	{
		if(s[i]==a[j]){j++;}
		if(j==a.length()){break;}
	}
	if(j==a.length())
	{
		j=0;
		for (; i < s.length(); ++i)
		{
			if(s[i]==b[j]){j++;}
		}
		if(j==b.length()){x1=1;}
	}
	j=0;
	for (i = s.length()-1; i>=0; --i)
	{
		if(s[i]==a[j]){j++;}
		if(j==a.length()){break;}
	}
	if(j==a.length())
	{
		j=0;
		for (; i>=0; --i)
		{
			if(s[i]==b[j]){j++;}
		}
		if(j==b.length()){x2=1;}
	}

	if(x1+x2==2){cout<<"both";return 0;}
	if(x1+x2==0){cout<<"fantasy";return 0;}
	if(x1){cout<<"forward";return 0;}
	cout<<"backward";
}


#include<bits/stdc++.h>
typedef long long ll;
typedef double ld;
#define vll vector<ll>
#define vvll vector< vll >
#define vld vector< ld >
#define vvld vector< vld >
#define pll pair<ll ,ll >
#define vllp vector< pll >
#define MOD 1000000007
#define endl "\n"
#define MAX 1000000007
#define nl <<endl
#define f(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;

int main()
{
    string s,a,b;
    cin>>s>>a>>b;
    bool f=0,f1=0;
    int x=s.find(a);
    int x1=s.find(b,x+a.size());
    if(x!=-1&&x1!=-1)f=1;
    reverse(s.begin(),s.end());
    x=s.find(a);
    x1=s.find(b,x+a.size());
    if(x!=-1&&x1!=-1)f1=1;
    if(f&&f1){cout<<"both";return 0;}
    if(f){cout<<"forward";return 0;}
    if(f1){cout<<"backward";return 0;}
    cout<<"fantasy";return 0;

}


#include <iostream>
using namespace std;

int main() {
    string flags, a, b;
    cin >> flags >> a >> b;

    bool forward1 = false, forward2 = false, backward1 = false, backward2 = false;
    int i, end;
    for(i = 0, end = flags.length() - a.length(); i <= end; ++i) {
        forward1 = true;
        for(int j = 0, end = a.length(); j < end; ++j) {
            if(a[j] != flags[i + j]) {
                forward1 = false;
                break;
            }
        }

        if(forward1) {
            i = i + a.length();
            break;
        }
    }

    if(forward1) for(end = flags.length() - b.length(); i <= end; ++i) {
        forward2 = true;
        for(int j = 0, end = b.length(); j < end; ++j) {
            if(b[j] != flags[i + j]) {
                forward2 = false;
                break;
            }
        }

        if(forward2) break;
    }

    for(i = flags.length() - 1, end = flags.length() - a.length(); i >= end; --i) {
        backward1 = true;
        for(int j = 0, end = a.length(); j < end; ++j) {
            if(a[j] != flags[i - j]) {
                backward1 = false;
                break;
            }
        }

        if(backward1) {
            i = i - a.length();
            break;
        }
    }

    if(backward1) for(end = flags.length() - b.length(); i >= end; --i) {
        backward2 = false;
        for(int j = 0, end = b.length(); j < end; ++j) {
            if(b[j] != flags[i - j]) {
                backward2 = false;
                break;
            }
        }

        if(backward2) break;
    }

    bool forward = forward1 && forward2;
    bool backward = backward1 && backward2;

    if(forward && backward) cout << "both\n";
    else if(forward) cout << "forward\n";
    else if(backward) cout << "backward\n";
    else cout << "fantasy\n";
}