//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N], c[N], cnt;
bool cmp (int x, int y)
{
    return abs(x) > abs(y);
}
int main (void)
{
    read (T);
    while (T --)
    {
        cnt = 0;
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++) {
            read(b[i]);
            if (b[i] == 0)
                c[cnt ++] = a[i];
        }
        sort (c , c + cnt, cmp);
        cnt = 0;
        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0)
                a[i] = c[cnt ++];
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N];
vector <int> x, y;
int main (void)
{
    read (T);
    while (T --)
    {
        x.clear() , y.clear();
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++)
            read (b[i]);
        for (int i = 1 ; i <= n ; i ++) {
            if (b[i] == 0) {
                if (a[i] < 0)
                    x.pb(a[i]);
                else
                    y.pb(a[i]);
            }
        }
        sort (y.begin() , y.end());
        int curx = 0 , cury = y.size() - 1;

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && cury >= 0)
                a[i] = y[cury] , cury -- , b[i] = 1;
        }

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && curx < x.size())
                a[i] = x[curx] , curx ++, b[i] = 1;
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N], c[N], cnt;
bool cmp (int x, int y)
{
    return abs(x) < abs(y);
}
int main (void)
{
    read (T);
    while (T --)
    {
        cnt = 0;
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++) {
            read(b[i]);
            if (b[i] == 0)
                c[cnt ++] = a[i];
        }
        sort (c , c + cnt, cmp);
        cnt = 0;
        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0)
                a[i] = c[cnt ++];
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N], c[N], cnt;
bool cmp (int x, int y)
{
    return x > y;
}
int main (void)
{
    read (T);
    while (T --)
    {
        cnt = 0;
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++) {
            read(b[i]);
            if (b[i] == 0)
                c[cnt ++] = a[i];
        }
        sort (c , c + cnt, cmp);
        cnt = 0;
        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0)
                a[i] = c[cnt ++];
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N];
vector <int> x, y;
int main (void)
{
    read (T);
    while (T --)
    {
        x.clear() , y.clear();
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++)
            read (b[i]);
        for (int i = 1 ; i <= n ; i ++) {
            if (b[i] == 0) {
                if (a[i] < 0)
                    x.pb(a[i]);
                else
                    y.pb(a[i]);
            }
        }
        sort (y.begin() , y.end());
        for (int i = n ; i >= 1 ; i --)
        {
            if (b[i] == 0 && x.size())
            {
                a[i] = x[0];
                break;
            }
        }
       // DBG;
        int curx = 1 , cury = y.size() - 1;

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && curx < x.size())
                a[i] = x[curx] , curx ++, b[i] = 1;
        }

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && cury >= 0)
                a[i] = y[cury] , cury -- , b[i] = 1;
        }

        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N];
vector <int> x, y;
int main (void)
{
    read (T);
    while (T --)
    {
        x.clear() , y.clear();
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++)
            read (b[i]);
        for (int i = 1 ; i <= n ; i ++) {
            if (b[i] == 0)
            {
                if (a[i] < 0)
                    x.pb(a[i]);
                else
                    y.pb(a[i]);
            }
        }

        sort (y.begin() , y.end());
        sort (x.begin() , x.end());

        int curx = x.size() - 1 , cury = y.size() - 1;
        int op = 1;
        for (int i = 1 ; i <= n ; i ++)
        {
            if (cury == -1 || curx == -1)
                break;
            if (b[i] == 0)
            {
                if (op)
                    a[i] = y[cury] , cury -- , op = 0;
                else
                    a[i] = x[curx] , curx -- , op = 1;
                b[i] = 1;
            }
        }

        for (int i = 1 ; i <= n ; i ++)
            if (curx >= 0 && b[i] == 0) a[i] = x[curx], curx --;
        for (int i = 1 ; i <= n ; i ++)
            if (cury >= 0 && b[i] == 0) a[i] = y[cury], cury --;

        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N];
vector <int> x, y;
int main (void)
{
    read (T);
    while (T --)
    {
        x.clear() , y.clear();
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++)
            read (b[i]);
        for (int i = 1 ; i <= n ; i ++) {
            if (b[i] == 0) {
                if (a[i] < 0)
                    x.pb(a[i]);
                else
                    y.pb(a[i]);
            }
        }
        sort (y.begin() , y.end());
        int curx = 0 , cury = y.size() - 1;

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && cury >= 0)
                a[i] = y[cury] , cury -- , b[i] = 0;
        }

        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && curx < x.size())
                a[i] = x[curx] , curx ++, b[i] = 1;
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
ll n, a[500005], b[500005];
vector<ll> y;
void solve() {
  y.clear();
  cin >> n;
  forn(n) cin >> a[i];
  forn(n) {
    cin >> b[i];
    b[i] ^= 1;
    if (b[i]) y.pb(a[i]);
  }
  sort(all(y));
  reverse(all(y));
  for (int i = n; i >= 1; i--) {
    if (b[i]) {
      a[i] = y.back();
      y.pop_back();
    }
  }
  forn(n) cout << a[i] << " ";
  cout << "\n";
}
int main() {
  speed;
  oprecision;
  // code
  int T;
  cin >> T;
  while (T--) solve();
    // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
ll n, a[500005], b[500005];
vector<ll> y;
void solve() {
  y.clear();
  cin >> n;
  forn(n) cin >> a[i];
  forn(n) {
    cin >> b[i];
    if (b[i]) y.pb(a[i]);
  }
  sort(all(y));
  forn(n) {
    if (b[i]) {
      cout << y.back() << " ";
      y.pop_back();
    } else {
      cout << a[i] << " ";
    }
  }
  cout << "\n";
}
int main() {
  speed;
  oprecision;
  // code
  int T;
  cin >> T;
  while (T--) solve();
    // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
ll n, a[500005], b[500005];
vector<ll> y;
void solve() {
  y.clear();
  cin >> n;
  forn(n) cin >> a[i];
  forn(n) {
    cin >> b[i];
    if (b[i]) y.pb(a[i]);
  }
  sort(all(y));
  reverse(all(y));
  for (int i = n; i >= 1; i--) {
    if (b[i]) {
      a[i] = y.back();
      y.pop_back();
    }
  }
  forn(n) cout << a[i] << " ";
  cout << "\n";
}
int main() {
  speed;
  oprecision;
  // code
  int T;
  cin >> T;
  while (T--) solve();
    // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
ll n, a[500005], b[500005];
vector<ll> y;
void solve() {
  y.clear();
  cin >> n;
  forn(n) cin >> a[i];
  forn(n) {
    cin >> b[i];
    b[i] ^= 1;
    if (b[i]) y.pb(a[i]);
  }
  sort(all(y));
  reverse(all(y));
  forn(n) {
    if (b[i]) {
      cout << y.back() << " ";
      y.pop_back();
    } else {
      cout << a[i] << " ";
    }
  }
  cout << "\n";
}
int main() {
  speed;
  oprecision;
  // code
  int T;
  cin >> T;
  while (T--) solve();
    // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
ll n, a[500005], b[500005];
vector<ll> y;
void solve() {
  y.clear();
  cin >> n;
  forn(n) cin >> a[i];
  forn(n) {
    cin >> b[i];
    if (b[i]) y.pb(a[i]);
  }
  sort(all(y));
  reverse(all(y));
  forn(n) {
    if (b[i]) {
      cout << y.back() << " ";
      y.pop_back();
    } else {
      cout << a[i] << " ";
    }
  }
  cout << "\n";
}
int main() {
  speed;
  oprecision;
  // code
  int T;
  cin >> T;
  while (T--) solve();
    // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


// In the name of god
#include <bits/stdc++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define use_file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll N=105,inf=1e18+18;
ll t,n,a[N],b[N];
bool bl[N];
int  main(){
	ios::sync_with_stdio(false) , cin.tie(0) ,cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		vector <ll> v;
		v.clear();
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<n;i++){
			cin>>bl[i];
			if(!bl[i])v.pb(a[i]);
		}
		if(v.size()>0)sort(all(v));
		if(v.size()>0)reverse(all(v));
		ll p=0;
		for(int i=0;i<n;i++){
			if(bl[i])cout<<a[i]<<' ';
			else{
				cout<<v[p]<<' ';
				p++;
			}
			bl[i]=0;
			a[i]=0;
		}
		cout<<endl;
	}
}


// In the name of god
#include <bits/stdc++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define use_file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll N=5e5+5,inf=1e18+18;
ll t,n,a[N],b[N];
bool bl[N];
int  main(){
	ios::sync_with_stdio(false) , cin.tie(0) ,cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		vector <ll> v;
		v.clear();
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<n;i++){
			cin>>bl[i];
			if(!bl[i])v.pb(a[i]);
		}
		if(v.size()>0)sort(all(v));
		int p=0;
		for(int i=0;i<n;i++){
			if(bl[i])cout<<a[i]<<' ';
			else{
				cout<<v[p]<<' ';
				p++;
			}
		}
		cout<<endl;
	}
}


// In the name of god
#include <bits/stdc++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define use_file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll N=105,inf=1e18+18;
ll t,n,a[N],b[N];
bool bl[N];
int  main(){
	ios::sync_with_stdio(false) , cin.tie(0) ,cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		vector <ll> v;
		v.clear();
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<n;i++){
			cin>>bl[i];
			if(!bl[i])v.pb(a[i]);
		}
		if(v.size()>0)sort(all(v));
		ll p=0;
		for(int i=0;i<n;i++){
			if(bl[i])cout<<a[i]<<' ';
			else{
				cout<<v[p]<<' ';
				p++;
			}
			bl[i]=0;
			a[i]=0;
		}
		cout<<endl;
	}
}


// In the name of god
#include <bits/stdc++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
const ll N = 105, inf = 1e18 + 18;
ll t, n, a[N], b[N];
bool bl[N];
int  main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin >> t;
	while(t --){
		cin >> n;
		vector <ll> v;
		v.clear();
		for(int i = 0; i < n; i ++)cin >> a[i];
		for(int i = 0; i < n; i ++){
			cin >> bl[i];
			if(!bl[i])v.pb(a[i]);
		}
		if(v.size() > 0)sort(all(v));
		if(v.size() > 0)reverse(all(v));
		ll p = 0;
		for(int i = 0; i < n; i ++){
			if(bl[i])cout << a[i] << ' ';
			else{
				cout << v[p] << ' ';
				p ++;
			}
			bl[i] = 0;
			a[i] = 0;
		}
		cout << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int a[110],b[110],c[110],d[110];
int sum[110],sum2[110];
int main(int argc, char const *argv[])
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i];
			d[i] = a[i];
		}
		int j = 0;
		for (int i = 0; i < n; ++i)
		{
			cin>>b[i];
			if (b[i] == 0) c[j++] = a[i];
		}
		sort(c,c+j);
		int jj = j;
		jj--;
		j = 0;
		for (int i = 0; i < n; ++i)
		{
			if (b[i] == 0)
			{
				a[i] = c[j++];
				d[i] = c[jj--];
			}
		}
		int kk = 0,kk2 = 0;
		sum[0] = a[0];
		sum2[0] = d[0];
		for (int i = 1; i < n; ++i)
		{
			sum[i] += sum[i-1] + a[i];
			if (sum[i] < 0) kk = i;
		}
		//cout<<endl;
		for (int i = 1; i < n; ++i)
		{
			sum2[i] += sum2[i-1] + d[i];
			if (sum2[i] < 0) kk2 = i;
		}
		if (kk <= kk2)
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
		}
		else
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<d[i]<<" ";
			}
			cout<<endl;
		}
	}

}


#include <bits/stdc++.h>
using namespace std;
int a[110],b[110],c[110],d[110];
int sum[110],sum2[110];
int main(int argc, char const *argv[])
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		memset(sum,0,sizeof(sum));
		memset(sum2,0,sizeof(sum2));
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i];
			d[i] = a[i];
		}
		int j = 0;
		for (int i = 0; i < n; ++i)
		{
			cin>>b[i];
			if (b[i] == 0) c[j++] = a[i];
		}
		sort(c,c+j);
		int jj = j;
		jj--;
		j = 0;
		for (int i = 0; i < n; ++i)
		{
			if (b[i] == 0)
			{
				a[i] = c[j++];
				d[i] = c[jj--];
			}
		}
		int kk = 0,kk2 = 0;
		sum[0] = a[0];
		sum2[0] = d[0];
		if (a[0] < 0) kk = 1;
		if (d[0] < 0) kk2 = 1;
		for (int i = 1; i < n; ++i)
		{
			sum[i] += sum[i-1] + a[i];
			if (sum[i] < 0) kk = i+1;
		}
		//cout<<endl;
		for (int i = 1; i < n; ++i)
		{
			sum2[i] += sum2[i-1] + d[i];
			if (sum2[i] < 0) kk2 = i+1;
		}
		//cout<<kk<<" "<<kk2<<endl;
		if (kk <= kk2)
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
		}
		else
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<d[i]<<" ";
			}
			cout<<endl;
		}
	}

}


#include <bits/stdc++.h>
using namespace std;
int a[110],b[110],c[110],d[110];
int sum[110],sum2[110];
int main(int argc, char const *argv[])
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		memset(sum,0,sizeof(sum));
		memset(sum2,0,sizeof(sum2));
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i];
			d[i] = a[i];
		}
		int j = 0;
		for (int i = 0; i < n; ++i)
		{
			cin>>b[i];
			if (b[i] == 0) c[j++] = a[i];
		}
		sort(c,c+j);
		int jj = j;
		jj--;
		j = 0;
		for (int i = 0; i < n; ++i)
		{
			if (b[i] == 0)
			{
				a[i] = c[j++];
				d[i] = c[jj--];
			}
		}
		int kk = 0,kk2 = 0;
		sum[0] = a[0];
		sum2[0] = d[0];
		if (a[0] < 0) kk = 1;
		if (d[0] < 0) kk2 = 1;
		for (int i = 1; i < n; ++i)
		{
			sum[i] += sum[i-1] + a[i];
			if (sum[i] < 0) kk = i+1;
		}
		//cout<<endl;
		for (int i = 1; i < n; ++i)
		{
			sum2[i] += sum2[i-1] + d[i];
			if (sum2[i] < 0) kk2 = i+1;
		}
		cout<<kk<<" "<<kk2<<endl;
		if (kk <= kk2)
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
		}
		else
		{
			for (int i = 0; i < n; ++i)
			{
				cout<<d[i]<<" ";
			}
			cout<<endl;
		}
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define out(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define f(x,n) for(ll x = 0; x < n; ++x)
#define loops(start,x,n) for(ll x = start; x < n; ++x)

int main()
{
   ll t,n,q,x,y,mini=INT_MAX,maxi=INT_MIN,i,sum,ans1,ans2,j,k;
   bool flag=true;
   cin>>t;
   while(t--)
   {
     mini=INT_MAX,maxi=INT_MIN;
    cin>>n;
    ll a[n],lock[n];
    in(a,n);
    in(lock,n);
    vector<ll>v;
    v.clear();
    f(i,n)
    {
        if(lock[i]==0)v.push_back(a[i]);
    }
    j=0;
    sort(v.begin(),v.end(),greater<int>());
    f(i,n)
    {
        if(lock[i]==0)cout<<v[j++]<<" ";
        else cout<<a[i]<<" ";
    }
    cout<<endl;
}
}
//-8 -6 -2 1 4 7 4
//-8 -6 1 4 4 7 -2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define out(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define f(x,n) for(ll x = 0; x < n; ++x)
#define loops(start,x,n) for(ll x = start; x < n; ++x)

int main()
{
   ll t,n,q,k,x,y,mini=INT_MAX,maxi=INT_MIN,i,sum,ans1,ans2,j;
   bool flag=true;
   cin>>t;
   while(t--)
   {
     mini=INT_MAX,maxi=INT_MIN;
    cin>>n;
    ll a[n],lock[n];
    in(a,n);
    in(lock,n);
    vector<int>vect;
    vect.clear();
    f(i,n)
    {
        if(lock[i]==0)vect.push_back(a[i]);
    }
    sort(vect.begin(),vect.end());
    j=0;
    f(i,n)
    {
        if(lock[i]==0)
        {
            a[i]=vect[j];
            j++;
        }
    }
    f(i,n)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define out(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define f(x,n) for(ll x = 0; x < n; ++x)
#define loops(start,x,n) for(ll x = start; x < n; ++x)

int main()
{
   ll t,n,q,x,y,mini=INT_MAX,maxi=INT_MIN,i,sum,ans1,ans2,j,k;
   bool flag=true;
   cin>>t;
   while(t--)
   {
     mini=INT_MAX,maxi=INT_MIN;
    cin>>n;
    ll a[n],lock[n];
    in(a,n);
    in(lock,n);
    vector<int>vect;
    vector<int>vectp;
    vect.clear();
    vectp.clear();
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(a[i]<0)vect.push_back(a[i]);
            else vectp.push_back(a[i]);
        }
    }
    sort(vect.begin(),vect.end());
    sort(vectp.begin(),vectp.end(),greater<int>());

    j=0;//neg
    k=0;//pos
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(j<vect.size())
            {
                a[i]=vect[j];
                j++;
            }
            else
            {
                a[i]=vectp[k];
                k++;
            }

        }
    }
    f(i,n)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
   }
}
//-8 -6 -2 1 4 7 4
//-8 -6 1 4 4 7 -2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define out(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define f(x,n) for(ll x = 0; x < n; ++x)
#define loops(start,x,n) for(ll x = start; x < n; ++x)

int main()
{
   ll t,n,q,x,y,mini=INT_MAX,maxi=INT_MIN,i,sum,ans1,ans2,j,k;
   bool flag=true;
   cin>>t;
   while(t--)
   {
     mini=INT_MAX,maxi=INT_MIN;
    cin>>n;
    ll a[n],lock[n];
    in(a,n);
    in(lock,n);
    vector<int>vect;
    vector<int>vectp;
    vect.clear();
    vectp.clear();
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(a[i]<=0)vect.push_back(a[i]);
            else vectp.push_back(a[i]);
        }
    }
    sort(vect.begin(),vect.end());//neg and 0 sorted
    sort(vectp.begin(),vectp.end(),greater<int>());//pos descending

    j=0;//neg
    k=0;//pos
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(k<vectp.size())
            {
                cout<<vectp[k++]<<" ";
            }
            else
            {
                cout<<vect[j++]<<" ";
            }
        }
        else cout<<a[i]<<" ";
    }
    cout<<endl;
   }
}
//-8 -6 -2 1 4 7 4
//-8 -6 1 4 4 7 -2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define out(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define f(x,n) for(ll x = 0; x < n; ++x)
#define loops(start,x,n) for(ll x = start; x < n; ++x)

int main()
{
   ll t,n,q,x,y,mini=INT_MAX,maxi=INT_MIN,i,sum,ans1,ans2,j,k;
   bool flag=true;
   cin>>t;
   while(t--)
   {
     mini=INT_MAX,maxi=INT_MIN;
    cin>>n;
    ll a[n],lock[n];
    in(a,n);
    in(lock,n);
    vector<int>vect;
    vector<int>vectp;
    vect.clear();
    vectp.clear();
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(a[i]<0)vect.push_back(a[i]);
            else vectp.push_back(a[i]);
        }
    }
    sort(vect.begin(),vect.end());
    sort(vectp.begin(),vectp.end(),greater<int>());

    j=0;//neg
    k=0;//pos
    f(i,n)
    {
        if(lock[i]==0)
        {
            if(k<vectp.size())
            {
                a[i]=vectp[k];
                k++;
            }
            else
            {
                a[i]=vect[j];
                j++;
            }

        }
    }
    f(i,n)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
   }
}
//-8 -6 -2 1 4 7 4
//-8 -6 1 4 4 7 -2


#include <bits/stdc++.h>

using namespace std;
#define nl  "\n"
#define nt " "

typedef long long ll;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0);

vector<int> vec(1005 , 0 ) ;

void ppf(int n)
{
   long long int product = 1;

    for (int i = 2; i <= n; i++) {

        // Checking if 'i' is factor of num
        if (n % i == 0) {

            // Checking if 'i' is a Prime number
            bool isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }

            // condition if 'i' is Prime number
            // as well as factor of num
            if (isPrime) {
                vec[i]++  ;
                product = product * i;
            }
        }
    }

   // return product;
}

void pf(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        vec[2]++;// cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
           vec[i]++; // cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
     vec[n]++; //   cout << n << " ";
}

int main(){
    FIO ;
    int t ; cin>> t ;
    while(t--){
            int n ; cin >> n ;
            vector< int > vec, vecc ;
            int df[n] ; for( auto &i : df ) cin >> i ;
            int li[n] ;  for( auto &i : li ) cin >> i ;
            for( int i  =0 ; i<n ; ++i ){
                    if( li[i] == 0  ){ vec.push_back( df[i] ) ; vecc.push_back( df[i] ); }
            }
            sort( vec.begin() , vec.end() ,  greater< int >()   ) ;
            sort(  vecc.begin() , vecc.end() ) ;
            int s = 0 ;
            int ind = 0 ;
            int f = 1 ;
            for( int i = 0 ; i<n ; ++i ){
                    if( li[i] ) s+= df[i];
                    else{ s+= vec[ind];  ind++ ;  }

                    if( s<0 ) f  = 0  ;
                }
            if( s<0 ){
                  //  cout <<'xd' ;
                    for( auto i : df ) cout << i << nt ;
                }
            else{
                   // if( f ){
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vec[ind];  ind++ ; }
                                cout <<nt ;
                            }
                    //}
                    /*
                    else{
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vecc[ind];  ind++ ; }
                                cout << nt  ;
                            }

                            //if( s<0 ) f  = 0  ;
                }*/
            }

            cout << nl  ;
        }

}


#include <bits/stdc++.h>

using namespace std;
#define nl  "\n"
#define nt " "

typedef long long ll;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0);

vector<int> vec(1005 , 0 ) ;

void ppf(int n)
{
   long long int product = 1;

    for (int i = 2; i <= n; i++) {

        // Checking if 'i' is factor of num
        if (n % i == 0) {

            // Checking if 'i' is a Prime number
            bool isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }

            // condition if 'i' is Prime number
            // as well as factor of num
            if (isPrime) {
                vec[i]++  ;
                product = product * i;
            }
        }
    }

   // return product;
}

void pf(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        vec[2]++;// cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
           vec[i]++; // cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
     vec[n]++; //   cout << n << " ";
}

int main(){
    FIO ;
    int t ; cin>> t ;
    while(t--){
            int n ; cin >> n ;
            vector< int > vec, vecc ;
            int df[n] ; for( auto &i : df ) cin >> i ;
            int li[n] ;  for( auto &i : li ) cin >> i ;
            for( int i  =0 ; i<n ; ++i ){
                    if( li[i] == 0  ){ vec.push_back( df[i] ) ; vecc.push_back( df[i] ); }
            }
            sort( vec.begin() , vec.end() ,  greater< int >()   ) ;
            sort(  vec.begin() , vec.end() ) ;
            int s = 0 ;
            int ind = 0 ;
            int f = 1 ;
            for( int i = 0 ; i<n ; ++i ){
                    if( li[i] ) s+= df[i];
                    else{ s+= vec[ind];  ind++ ;  }

                    if( s<0 ) f  = 0  ;
                }
            if( s<0 ){
                    for( auto i : df ) cout << i << nt ;
                }
            else{
                    if( f ){
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vec[ind];  ind++ ; }
                                cout <<nt ;
                            }
                    }
                    else{
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vecc[ind];  ind++ ; }
                                cout << nt  ;
                            }

                            //if( s<0 ) f  = 0  ;
                }
            }

            cout << nl  ;
        }

}


#include <bits/stdc++.h>

using namespace std;
#define nl  "\n"
#define nt " "

typedef long long ll;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0);

vector<int> vec(1005 , 0 ) ;

void ppf(int n)
{
   long long int product = 1;

    for (int i = 2; i <= n; i++) {

        // Checking if 'i' is factor of num
        if (n % i == 0) {

            // Checking if 'i' is a Prime number
            bool isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }

            // condition if 'i' is Prime number
            // as well as factor of num
            if (isPrime) {
                vec[i]++  ;
                product = product * i;
            }
        }
    }

   // return product;
}

void pf(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        vec[2]++;// cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
           vec[i]++; // cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
     vec[n]++; //   cout << n << " ";
}

int main(){
    FIO ;
    int t ; cin>> t ;
    while(t--){
            int n ; cin >> n ;
            vector< int > vec, vecc ;
            int df[n] ; for( auto &i : df ) cin >> i ;
            int li[n] ;  for( auto &i : li ) cin >> i ;
            for( int i  =0 ; i<n ; ++i ){
                    if( li[i] == 0  ){ vec.push_back( df[i] ) ; vecc.push_back( df[i] ); }
            }
            sort( vec.begin() , vec.end() ,  greater< int >()   ) ;
            sort(  vecc.begin() , vecc.end() ) ;
            int s = 0 ;
            int ind = 0 ;
            int f = 1 ;
            for( int i = 0 ; i<n ; ++i ){
                    if( li[i] ) s+= df[i];
                    else{ s+= vec[ind];  ind++ ;  }

                    if( s<0 ) f  = 0  ;
                }
            if( s<0 ){
                    for( auto i : df ) cout << i << nt ;
                }
            else{
                    if( f ){
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vec[ind];  ind++ ; }
                                cout <<nt ;
                            }
                    }
                    else{
                            int ind  = 0 ;
                            for( int i = 0 ; i<n ; ++i ){
                                if( li[i] ) cout << df[i];
                                else{ cout<<vecc[ind];  ind++ ; }
                                cout << nt  ;
                            }

                            //if( s<0 ) f  = 0  ;
                }
            }

            cout << nl  ;
        }

}


#include <bits/stdc++.h>

using namespace std;
#define nl  "\n"
#define nt " "

typedef long long ll;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0);

vector<int> vec(1005 , 0 ) ;

void ppf(int n)
{
   long long int product = 1;

    for (int i = 2; i <= n; i++) {

        // Checking if 'i' is factor of num
        if (n % i == 0) {

            // Checking if 'i' is a Prime number
            bool isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }

            // condition if 'i' is Prime number
            // as well as factor of num
            if (isPrime) {
                vec[i]++  ;
                product = product * i;
            }
        }
    }

   // return product;
}

void pf(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        vec[2]++;// cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
           vec[i]++; // cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
     vec[n]++; //   cout << n << " ";
}

int main(){
    FIO ;
    int t ; cin>> t ;
    while(t--){
            int n ; cin >> n;
            int df[n] ;  for( auto &i : df ) cin >> i ;
            int lk[n] ;  for( auto &i : lk ) cin >>i ;
            vector<int>vec ,vecc  ;
            for( int i = 0 ; i<n; ++i ){

                    if( lk[i] ==0 ) vec.push_back( df[i] ), vecc.push_back( df[i] );
                }
            sort( vec.begin() , vec.end() )  ;
            sort( vecc.begin() , vecc.end(), greater<int>() ) ;

            int ps = 0  ;
            int f= 1 ;
            int ind = 0 ;
            for( int i  =0 ;i<n; ++i ){
                    if( lk[i] ) ps+=df[i];
                    else ps+=vecc[ind], ind++;

                    if( ps<0 ) f = 0 ;
                }
           // cout << f <<nl ;
            if(f){
                    int ind =0;
                    for( int i  =0 ;i<n; ++i ){
                        if( lk[i] ) cout << df[i] <<nt;
                        else{ cout << vecc[ind]<<nt; ind++;
                        }
                  //  if( ps<0 ) f = 0 ;
                    }
                }
            else{
                    int ps = 0  ;
                    //int f= 1 ;
                    int ind = 0 ;
                    int k ;
                    for( int i  =0 ;i<n ; ++i ){
                        if( lk[i] ) cout << df[i] <<nt;
                        else cout << vec[ind]<<nt, ind++;
                        //if( ps<0 ){ f = 0;  k= i+ind ;  }

                    }

                }
            cout << nl ;
        }

}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>pos; vector<ll>neg;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				if (v[i] >= 0) {
					pos.pb(v[i]);
				}
				else {
					neg.pb(v[i]);
				}
			}
		}
		sort(neg.begin(), neg.end());
		sort(pos.begin(), pos.end());
		reverse(pos.begin(), pos.end());
		int j = 0; int k = 0; int i = 0;
		while ( i < n) {
			if (l[i] == 0) {
				if (j < neg.size() && k < pos.size() &&  pos[k] >= abs(neg[j])) {
					v[i] = pos[k]; k++; i++;
					while (i < n && l[i] == 1) {
						i++;
					}
					if (i < n) {v[i] = neg[j]; j++; i++;}
				}
				else if (j < neg.size() ) {
					v[i] = neg[j]; j++; i++;
				}
				else {
					v[i] = pos[k]; k++; i++;
				}
			}
			i++;
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>pos; vector<ll>neg;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				if (v[i] >= 0) {
					pos.pb(v[i]);
				}
				else {
					neg.pb(v[i]);
				}
			}
		}
		sort(neg.begin(), neg.end());
		sort(pos.begin(), pos.end());
		reverse(pos.begin(), pos.end());
		int j = 0; int k = 0; int i = 0;
		while ( i < n) {
			if (l[i] == 0) {
				if (j < neg.size() && k < pos.size() &&  pos[k] >= abs(neg[j])) {
					v[i] = pos[k]; k++; i++;
					while (i < n && l[i] == 1) {
						i++;
					}
					if (i < n) {v[i] = neg[j]; j++; i++;}
				}
				else if (j < neg.size() ) {
					v[i] = neg[j]; j++; i++;
				}
				else {
					v[i] = pos[k]; k++; i++;
				}
			}
			else {i++;}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>p;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				p.pb(v[i]);
			}
		}
		sort(p.begin(), p.end());
		reverse(p.begin(), p.end()); int j = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {v[i] = p[j]; j++;}
			cout << v[i] << " ";
		} cout << "\n";

	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>pos; vector<ll>neg;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				if (v[i] >= 0) {
					pos.pb(v[i]);
				}
				else {
					neg.pb(v[i]);
				}
			}
		}
		sort(neg.begin(), neg.end());
		sort(pos.begin(), pos.end());
		reverse(pos.begin(), pos.end());
		int j = 0; int k = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {
				if (j < neg.size() && k < pos.size() &&  pos[k] >= abs(neg[j])) {
					v[i] = pos[k]; k++;
				}
				else if (j < neg.size() ) {
					v[i] = neg[j]; j++;
				}
				else {
					v[i] = pos[k]; k++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>pos; vector<ll>neg;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				if (v[i] >= 0) {
					pos.pb(v[i]);
				}
				else {
					neg.pb(v[i]);
				}
			}
		}
		sort(neg.begin(), neg.end());
		sort(pos.begin(), pos.end());
		reverse(pos.begin(), pos.end());
		int j = 0; int k = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {
				if (j < neg.size() && k < pos.size() &&  pos[k] >= neg[j]) {
					v[i] = pos[k]; k++;
				}
				else if (j < neg.size() ) {
					v[i] = neg[j]; j++;
				}
				else {
					v[i] = pos[k]; k++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>s;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {s.pb(v[i]);}
		}
		sort(s.begin(), s.end()); int j = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {v[i] = s[j]; j++;}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
		ll sum = 0;
		for (int i = 0; i < n; i++) {
			sum += v[i];
			if (sum < 0) {cout << i + 1 << "\n"; break;}
		}
		if (sum >= 0) {cout << 0 << "\n";}
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>s;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {s.pb(v[i]);}
		}
		sort(s.begin(), s.end()); int j = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {v[i] = s[j]; j++;}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

void s_y() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
int main() {
	s_y();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<ll>l(n);
		vector<ll>pos; vector<ll>neg;
		for (int i = 0; i < n; i++) {
			cin >> l[i];
			if (l[i] == 0) {
				if (v[i] >= 0) {
					pos.pb(v[i]);
				}
				else {
					neg.pb(v[i]);
				}
			}
		}
		sort(neg.begin(), neg.end());
		sort(pos.begin(), pos.end());
		reverse(pos.begin(), pos.end());
		int j = 0; int k = 0;
		for (int i = 0; i < n; i++) {
			if (l[i] == 0) {
				if (j < neg.size()) {
					v[i] = neg[j]; j++;
				}
				else {
					v[i] = pos[k]; k++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << v[i] << " ";
		} cout << "\n";
	}
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> pos(n);
		for(auto &it : v)
			cin >> it;

		for(auto &it : pos)
			cin >> it;

		vector<ll> arr;
		set<ll> locked;
		for(int i = 0; i < n; i++){
			if(pos[i] == 1)
				locked.insert(i);
			else{
				arr.push_back(v[i]);
			}
		}

		sort(arr.begin(), arr.end());

		vector<ll> sol(n);
		set<ll> dirty;

		set<ll> :: iterator it = locked.begin();
		ll pref = 0;

		ll ai = 0;
		for(int i = 0; i < n; i++){
			if(locked.find(i) != locked.end()){
				sol[i] = v[*it];
				it++;
			}
			else{
				sol[i] = arr[ai];
				ai++;
			}

		}

		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> pos(n);
		for(auto &it : v)
			cin >> it;

		for(auto &it : pos)
			cin >> it;

		vector<ll> arr;
		set<ll> locked;
		for(int i = 0; i < n; i++){
			if(pos[i] == 1)
				locked.insert(i);
			else
				arr.push_back(v[i]);
		}

		sort(arr.begin(), arr.end());

		vector<ll> sol(n);

		ll ap = 0;
		set<ll> :: iterator it = locked.begin();
		for(int i = 0; i < n; i++){
			if(locked.find(i) != locked.end()){
				sol[i] = v[*it];
				it++;
			}
			else{
				sol[i] = arr[ap];
				ap++;
			}
		}

		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> pos(n);
		for(auto &it : v)
			cin >> it;

		for(auto &it : pos)
			cin >> it;

		vector<ll> arr;
		set<ll> locked;
		for(int i = 0; i < n; i++){
			if(pos[i] == 1)
				locked.insert(i);
			else
				arr.push_back(v[i]);
		}

		sort(arr.begin(), arr.end());

		vector<ll> sol(n);
		set<ll> dirty;
		ll apneg = 0, appos= arr.size() - 1;

		ll arn = arr.size();
		while(appos >= 0 && arr[appos] > 0)
			appos--;
		if(appos < 0 || arr[appos] < 0)
			appos++;
		set<ll> :: iterator it = locked.begin();
		ll pref = 0;
		for(int i = 0; i < n; i++){
			if(locked.find(i) != locked.end()){
				sol[i] = v[*it];
				pref += v[*it];
				it++;
			}
			else{
				if(pref > 0){
					if(apneg == arn)
						apneg = 0;
					while(dirty.find(apneg) != dirty.end()){
						if(apneg == arn)
							apneg = 0;
						else
							apneg++;
					}

					/*if(apneg <0 || apneg >= arn)
						cout << "[" << t <<"] apneg out of bounds " << apneg << "\n";*/
					sol[i] = arr[apneg];
					pref += arr[apneg];
					dirty.insert(apneg);
					apneg++;
				}
				else{
					if(appos == arn)
						appos = 0;
					while(dirty.find(appos) != dirty.end()){
						if(appos == arn)
							appos = 0;
						else
							appos++;
					}
					/*if(appos <0 || appos >= arn)
						cout << "[" << t <<"] appos out of bounds " << appos << "\n";*/
					sol[i] = arr[appos];
					pref += arr[appos];
					dirty.insert(appos);
					appos++;
				}
			}

		}

		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> visited(n);
		vector<ll> sol(n);
		vector<ll> a;
		for(auto &it : v)
			cin >> it;
		for(auto &it : visited)
			cin >> it;

		for(int i = 0; i < n; i++){
			if(!visited[i])
				a.push_back(v[i]);
		}
		sort(a.begin(), a.end());
		reverse(a.begin(), a.end());
		ll ap = 0;
		for(int i = 0; i < n; i++){
			if(visited[i])
				sol[i] = v[i];
			else
				sol[i] = a[ap++];
		}
		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> pos(n);
		for(auto &it : v)
			cin >> it;

		for(auto &it : pos)
			cin >> it;

		vector<ll> arr;
		set<ll> locked;
		for(int i = 0; i < n; i++){
			if(pos[i])
				locked.insert(i);
			else
				arr.push_back(v[i]);
		}

		sort(arr.begin(), arr.end());

		vector<ll> sol(n);

		ll vp = 0, ap = 0;
		for(int i = 0; i < n; i++){
			if(locked.find(i) != locked.end()){
				sol[i] = v[vp];
				vp++;
			}
			else{
				sol[i] = arr[ap];
				ap++;
			}
		}

		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> visited(n);
		vector<ll> sol(n);
		vector<ll> a;
		for(auto &it : v)
			cin >> it;
		for(auto &it : visited)
			cin >> it;

		for(int i = 0; i < n; i++){
			if(!visited[i])
				a.push_back(v[i]);
		}
		sort(a.begin(), a.end());
		reverse(a.begin(), a.end());
		ll ap = 0;
		for(int i = 0; i < n; i++){
			if(visited[i])
				sol[i] = v[i];
			else
				sol[i] = a[ap++];
		}
		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> visited(n);
		vector<ll> sol(n);
		vector<ll> a;
		for(auto &it : v)
			cin >> it;
		for(auto &it : visited)
			cin >> it;

		for(int i = 0; i < n; i++){
			if(!visited[i])
				a.push_back(v[i]);
		}
		sort(a.begin(), a.end(), greater<ll>());
		ll ap = 0;
		for(int i = 0; i < n; i++){
			if(visited[i])
				sol[i] = v[i];
			else
				sol[i] = a[ap++];
		}
		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){

	ll t,n;
	cin >> t;
	while(t--){
		cin >> n;
		vector<ll> v(n);
		vector<ll> pos(n);
		for(auto &it : v)
			cin >> it;

		for(auto &it : pos)
			cin >> it;

		vector<ll> arr;
		set<ll> locked;
		for(int i = 0; i < n; i++){
			if(pos[i] == 1)
				locked.insert(i);
			else
				arr.push_back(v[i]);
		}

		sort(arr.begin(), arr.end());

		vector<ll> sol(n);
		set<ll> dirty;
		ll apneg = 0, appos= arr.size() - 1;
		while(appos >= 0 && arr[appos] > 0)
			appos--;
		if(appos < 0 || arr[appos] < 0)
			appos++;
		set<ll> :: iterator it = locked.begin();
		ll pref = 0;
		for(int i = 0; i < n; i++){
			if(locked.find(i) != locked.end()){
				sol[i] = v[*it];
				pref += v[*it];
				it++;
			}
			else{
				if(pref > 0){
					while(dirty.find(apneg) != dirty.end())
						apneg++;
					sol[i] = arr[apneg];
					pref += arr[apneg];
					dirty.insert(apneg);
					apneg++;
				}
				else{
					while(dirty.find(appos) != dirty.end())
						appos++;
					sol[i] = arr[appos];
					pref += arr[appos];
					dirty.insert(appos);
					appos++;
				}
			}

		}

		for(auto it : sol)
			cout << it << " ";
		cout << "\n";
	}

}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class DetermineLine {
	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		String s = sc.nextLine();
		ArrayList<Integer> list = new ArrayList<>();
		TreeSet<Integer> set = new TreeSet<>();
		int max = 0;
		int index = 0;
		for (int i = 0; i < n; i++) {
			if (s.charAt(i) == 'S') {
				list.add(i);
				index = i + 1;
			} else {
				set.add(i);
				if (index != 0)
					max = Math.max(max, i - index + 1);
			}
		}
		if (set.size() == 0) {
			pw.println(0);
		} else {
			index = set.first();
			if (list.size() == 0) {
				pw.println(n);
				pw.flush();
				return;
			}
			if (list.size() == 1) {
				pw.println(n - 1);
				pw.flush();
				return;
			}
			for (int i = 1; i < list.size(); i++) {
				if (set.higher(list.get(i)) != null)
					max = Math.max(max, list.get(i) - index);
				else
					max = Math.max(max, list.get(i) - index - 1);
				index = list.get(i - 1) + 1;
			}
			pw.println(max);
		}
		pw.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class DetermineLine {
	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		String s = sc.nextLine();
		ArrayList<Integer> list = new ArrayList<>();
		TreeSet<Integer> set = new TreeSet<>();
		int max = 0;
		int index = -1;
		for (int i = 0; i < n; i++) {
			if (s.charAt(i) == 'S') {
				list.add(i);
				index = i + 1;
			} else {
				set.add(i);
				if (index != -1)
					max = Math.max(max, i - index + 1);
			}
		}
		if (set.size() == 0) {
			pw.println(0);
		} else {
			index = set.first();
			if (list.size() == 0) {
				pw.println(n);
				pw.flush();
				return;
			}
			if (list.size() == 1) {
				pw.println(n - 1);
				pw.flush();
				return;
			}
			for (int i = 1; i < list.size(); i++) {
				if (set.higher(list.get(i)) != null)
					max = Math.max(max, list.get(i) - index);
				else
					max = Math.max(max, list.get(i) - index - 1);
			}
			pw.println(max);
		}
		pw.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class DetermineLine {
	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		String s = sc.nextLine();
		ArrayList<Integer> list = new ArrayList<>();
		TreeSet<Integer> set = new TreeSet<>();
		int max = 0;
		int index = 0;
		for (int i = 0; i < n; i++) {
			if (s.charAt(i) == 'S') {
				list.add(i);
				index = i + 1;
			} else {
				set.add(i);
				if (index != 0)
					max = Math.max(max, i - index + 1);
			}
		}
		if (set.size() == 0) {
			pw.println(0);
		} else {
			index = set.first();
			if (list.size() == 0) {
				pw.println(n);
				pw.flush();
				return;
			}
			if (list.size() == 1) {
				pw.println(n - 1);
				pw.flush();
				return;
			}
			for (int i = 1; i < list.size(); i++) {
				if (set.higher(list.get(i)) != null)
					max = Math.max(max, list.get(i) - index);
				else
					max = Math.max(max, list.get(i) - index - 1);
			}
			pw.println(max);
		}
		pw.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class DetermineLine {
	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		String s = sc.nextLine();
		ArrayList<Integer> list = new ArrayList<>();
		TreeSet<Integer> set = new TreeSet<>();
		int max = 0;
		int index = 0;
		for (int i = 0; i < n; i++) {
			if (s.charAt(i) == 'S') {
				list.add(i);
				index = i + 1;
			} else {
				set.add(i);
				if (index != 0)
					max = Math.max(max, i - index + 1);
			}
		}
		if (set.size() == 0) {
			pw.println(0);
		} else {
			index = set.first();
			if (list.size() == 0) {
				pw.println(n);
				pw.flush();
				return;
			}
			if (list.size() == 1) {
				pw.println(n - 1);
				pw.flush();
				return;
			}
			for (int i = 1; i < list.size(); i++) {
				if (set.higher(list.get(i)) != null)
					max = Math.max(max, list.get(i) - index);
				else
					max = Math.max(max, list.get(i) - index - 1);
				index = list.get(i - 1) + 1;
			}
			index = set.last();
			for (int i = list.size() - 2; i >= 0; i--) {
				if (set.lower(list.get(i)) != null)
					max = Math.max(max, index - list.get(i));
				else
					max = Math.max(max, index - list.get(i) - 1);
				index = list.get(i + 1) + 1;
			}
			pw.println(max);
		}
		pw.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class DetermineLine {
	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		String s = sc.nextLine();
		ArrayList<Integer> list = new ArrayList<>();
		TreeSet<Integer> set = new TreeSet<>();
		int max = 0;
		int index = 0;
		for (int i = 0; i < n; i++) {
			if (s.charAt(i) == 'S') {
				list.add(i);
				index = i + 1;
			} else {
				set.add(i);
				if (index != 0)
					max = Math.max(max, i - index + 1);
			}
		}
		if (set.size() == 0) {
			pw.println(0);
		} else {
			index = set.first();
			if (list.size() == 0) {
				pw.println(n);
				pw.flush();
				return;
			}
			if (list.size() == 1) {
				pw.println(n - 1);
				pw.flush();
				return;
			}
			for (int i = 1; i < list.size(); i++) {
				if (set.higher(list.get(i)) != null)
					max = Math.max(max, list.get(i) - index);
				else
					max = Math.max(max, list.get(i) - index - 1);
				index = list.get(i - 1) + 1;
			}
			index = set.last();
			for (int i = list.size() - 2; i >= 0; i--) {
				if (set.lower(list.get(i)) != null)
					max = Math.max(max, index - list.get(i));
				else
					max = Math.max(max, index - list.get(i) - 1);
				index = list.get(i + 1) - 1;
			}
			pw.println(max);
		}
		pw.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}


import java.util.*;
import java.util.Map.Entry;

import java.io.*;
/*
-> Written by <-
  -----------
 |J_O_B_E_E_L|
 |___________|
 |    ___    |
 |   (^_^)   |
 |  /( | )\  |
 |____|_|____|
*/

public  class Test {

	public static void main(String[] args) throws Exception {
		int x=sc.nextInt();
		char [] ch=sc.next().toCharArray();
		int max=0;
		int c=0;
		int m=x-1;
		while(m>=0 && ch[m]=='S') m--;
		for(int i=0;i<x;i++) {
			if(ch[i]=='G') c++;
			else {
				int j=i+1;
				while(j<x&&ch[j]=='G') {c++; j++;}
				if(j<=m) c++;
				max=Math.max(max, c);
				c=0;
				i=j;
			}
		}
		max=Math.max(c, max);
		pw.println(max);
		pw.flush();
		pw.close();
	}

    static class Scanner {

        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int curbuf;
        private int lenbuf;

        public Scanner(InputStream in) {
            this.in = in;
            this.curbuf = this.lenbuf = 0;
        }

        public boolean hasNextByte() {
            if (curbuf >= lenbuf) {
                curbuf = 0;
                try {
                    lenbuf = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return false;
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[curbuf++];
            else
                return -1;
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private void skip() {
            while (hasNextByte() && isSpaceChar(buffer[curbuf]))
                curbuf++;
        }

        public boolean hasNext() {
            skip();
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (!isSpaceChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }
	static Scanner sc = new Scanner(System.in);
	static PrintWriter pw = new PrintWriter(System.out);
}


import java.util.*;
import java.util.Map.Entry;

import java.io.*;
/*
-> Written by <-
  -----------
 |J_O_B_E_E_L|
 |___________|
 |    ___    |
 |   (^_^)   |
 |  /( | )\  |
 |____|_|____|
*/

public  class Test {

	public static void main(String[] args) throws Exception {
		int x=sc.nextInt();
		char [] ch=sc.next().toCharArray();
		int [] prefix=new int [x];
		int [] suffix=new int [x];
		int max=0,c=0,silver=0;
		for(int i=0;i<x;i++) {
			if(ch[i]=='G') {
				prefix[i]=1;
				if(i>0) prefix[i]+=prefix[i-1];
			}else {
				silver++;
			}
		}
		for(int i=x-1;i>=0;i--) {
			if(ch[i]=='G') {
				suffix[i]=1;
				if(i<x-1) suffix[i]+=suffix[i+1];
			}
		}
		int ans=0;
		for(int i=0;i<x;i++) {
			if(ch[i]=='S') {
				int l=0,r=0;
				if(i>0) l=prefix[i-1];
				if(i<x-1) r=suffix[i+1];
				if(r+l+(silver)<x) l++;
				ans=Math.max(ans, l);
				ans=Math.max(ans, r);
				ans=Math.max(ans, l+r);
			}
		}
		if(silver==0) ans=x;
		pw.print(ans);
		pw.flush();
		pw.close();
	}

    static class Scanner {

        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int curbuf;
        private int lenbuf;

        public Scanner(InputStream in) {
            this.in = in;
            this.curbuf = this.lenbuf = 0;
        }

        public boolean hasNextByte() {
            if (curbuf >= lenbuf) {
                curbuf = 0;
                try {
                    lenbuf = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return false;
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[curbuf++];
            else
                return -1;
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private void skip() {
            while (hasNextByte() && isSpaceChar(buffer[curbuf]))
                curbuf++;
        }

        public boolean hasNext() {
            skip();
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (!isSpaceChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }
	static Scanner sc = new Scanner(System.in);
	static PrintWriter pw = new PrintWriter(System.out);
}


import java.util.ArrayList;
import java.util.Scanner;

public class VovaTrophies {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> al = new ArrayList<Integer>();
    int n = sc.nextInt();
    String str = sc.next();
    sc.close();
    int cnt = 0;
    int cnts = 0;
    for(int i = 0; i < n; i++){
      if(cnt == 0){
        if(str.charAt(i)=='G'){
          cnt = 1;
          if(cnts>=2){
            al.add(0);
          }
          cnts=0;
          al.add(1);
        } else {
          cnts++;
          continue;
        }
      } else {
        if(str.charAt(i)=='G'){
          al.set(al.size()-1,al.get(al.size()-1)+1);
          cnts=0;
        }
        else {
          cnt = 0;
          cnts++;
        }
      }
    }
    /*for(Integer i : al){
      System.out.println(i);
    }*/
    if(al.size()==0){
      System.out.println(0);
      return;
    }
    if(al.size()==1){
      System.out.println(al.get(0));
      return;
    }
    if(al.size()==2){
      System.out.println(al.get(0)+al.get(1));
      return;
    }
    int ans = 0;
    for(int i = 0; i < al.size()-1;i++){
      ans = Math.max(ans, al.get(i)+al.get(i+1));
      //if((i==0 && al.get(i)>0) || (i==al.size()-2 && al.get(i+1)>0))
        //ans--;
    }
    if(al.size()==3){
      if(al.get(1)!=0) ans--;
    }
    System.out.println(ans+1);
  }

}


import java.util.ArrayList;
import java.util.Scanner;

public class VovaTrophies {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> al = new ArrayList<Integer>();
    int n = sc.nextInt();
    String str = sc.next();
    sc.close();
    int cnt = 0;
    int cnts = 0;
    for(int i = 0; i < n; i++){
      if(cnt == 0){
        if(str.charAt(i)=='G'){
          cnt = 1;
          if(cnts>=2){
            al.add(0);
          }
          cnts=0;
          al.add(1);
        } else {
          cnts++;
          continue;
        }
      } else {
        if(str.charAt(i)=='G'){
          al.set(al.size()-1,al.get(al.size()-1)+1);
          cnts=0;
        }
        else {
          cnt = 0;
          cnts++;
        }
      }
    }
    /*for(Integer i : al){
      System.out.println(i);
    }*/
    if(al.size()==0){
      System.out.println(0);
      return;
    }
    if(al.size()==1){
      System.out.println(al.get(0));
      return;
    }
    if(al.size()==2){
      System.out.println(al.get(0)+al.get(1));
      return;
    }
    int ans = 0;
    for(int i = 0; i < al.size()-1;i++){
      ans = Math.max(ans, al.get(i)+al.get(i+1));
      //if((i==0 && al.get(i)>0) || (i==al.size()-2 && al.get(i+1)>0))
        //ans--;
    }
    if(al.size()==3){
      if(al.get(1)!=0 && (al.get(0)==0 || al.get(2)==0)) ans--;
    }
    System.out.println(ans+1);
  }

}


import java.util.ArrayList;
import java.util.Scanner;

public class VovaTrophies {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> al = new ArrayList<Integer>();
    int n = sc.nextInt();
    String str = sc.next();
    sc.close();
    int cnt = 0;
    int cnts = 0;
    for(int i = 0; i < n; i++){
      if(cnt == 0){
        if(str.charAt(i)=='G'){
          cnt = 1;
          if(cnts>=2){
            al.add(0);
          }
          cnts=0;
          al.add(1);
        } else {
          cnts++;
          continue;
        }
      } else {
        if(str.charAt(i)=='G'){
          al.set(al.size()-1,al.get(al.size()-1)+1);
          cnts=0;
        }
        else {
          cnt = 0;
          cnts++;
        }
      }
    }
    /*for(Integer i : al){
      System.out.println(i);
    }*/
    if(al.size()==0){
      System.out.println(0);
      return;
    }
    if(al.size()==1){
      System.out.println(al.get(0));
      return;
    }
    if(al.size()==2){
      System.out.println(al.get(0)+al.get(1));
      return;
    }
    int ans = 0;
    for(int i = 0; i < al.size()-1;i++){
      ans = Math.max(ans, al.get(i)+al.get(i+1));
      //if((i==0 && al.get(i)>0) || (i==al.size()-2 && al.get(i+1)>0))
        //ans--;
    }
    if(al.size()==3 && al.contains(0)) ans--;
    System.out.println(ans+1);
  }

}


import java.util.ArrayList;
import java.util.Scanner;

public class VovaTrophies {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> al = new ArrayList<Integer>();
    int n = sc.nextInt();
    String str = sc.next();
    sc.close();
    int cnt = 0;
    int cnts = 0;
    for(int i = 0; i < n; i++){
      if(cnt == 0){
        if(str.charAt(i)=='G'){
          cnt = 1;
          if(cnts>=2){
            al.add(0);
          }
          cnts=0;
          al.add(1);
        } else {
          cnts++;
          continue;
        }
      } else {
        if(str.charAt(i)=='G'){
          al.set(al.size()-1,al.get(al.size()-1)+1);
          cnts=0;
        }
        else {
          cnt = 0;
          cnts++;
        }
      }
    }
    /*for(Integer i : al){
      System.out.println(i);
    }*/
    if(al.size()==0){
      System.out.println(0);
      return;
    }
    if(al.size()==1){
      System.out.println(al.get(0));
      return;
    }
    if(al.size()==2){
      System.out.println(al.get(0)+al.get(1));
      return;
    }
    int ans = 0;
    for(int i = 0; i < al.size()-1;i++){
      ans = Math.max(ans, al.get(i)+al.get(i+1));
    }
    System.out.println(ans+1);
  }

}


import java.util.ArrayList;
import java.util.Scanner;

public class VovaTrophies {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> al = new ArrayList<Integer>();
    int n = sc.nextInt();
    String str = sc.next();
    sc.close();
    int cnt = 0;
    for(int i = 0; i < n; i++){
      if(cnt == 0){
        if(str.charAt(i)=='G'){
          cnt = 1;
          al.add(1);
        } else continue;
      } else {
        if(str.charAt(i)=='G')
          al.set(al.size()-1,al.get(al.size()-1)+1);
        else cnt = 0;
      }
    }
    /*for(Integer i : al){
      System.out.println(i);
    }*/
    if(al.size()==0){
      System.out.println(0);
      return;
    }
    if(al.size()==1){
      System.out.println(al.get(0));
      return;
    }
    if(al.size()==2){
      System.out.println(al.get(0)+al.get(1));
      return;
    }
    int ans = 0;
    for(int i = 0; i < al.size()-1;i++){
      ans = Math.max(ans,(al.get(i)+al.get(i+1)));
    }
    System.out.println(ans+1);
  }

}


//No sorceries shall prevail.
import java.util.*;
import java.io.*;
public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	//Main
	void main() {

		int n=inp.nextInt();
		String s=inp.next();
		int dp[]=new int[n];
		dp[0]=s.charAt(0)=='G'?1:0;
		long gg=dp[0];
		long total=gg;
		for(int i=1;i<n;i++) {
			if(s.charAt(i)=='G') {
				dp[i]=dp[i-1]+1;
				total++;
			}
			gg=Math.max(gg, dp[i]);
		}
		//for(int i=0;i<n;i++) System.out.print(dp[i]+" ");
		//System.out.println();
		for(int i=n-2;i>=0;i--) {
			if(dp[i]>0 && dp[i+1]>0) dp[i]=dp[i+1];
		}
		//for(int i=0;i<n;i++) System.out.print(dp[i]+" ");
		if(dp[0]==0 && n>1 && total>dp[1]) {
			gg=Math.max(gg, 1+dp[1]);
		}
		if(dp[n-1]==0 && n>1 && total>dp[n-2]) {
			gg=Math.max(gg, 1+dp[n-2]);
		}
		for(int i=1;i<n-1;i++) {
			if(dp[i]==0 && total>dp[i-1]+dp[i+1]) gg=Math.max(gg, dp[i-1]+1+dp[i+1]);
		}
		System.out.println(gg);
	}

}


//No sorceries shall prevail.
import java.util.*;
import java.io.*;
public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	//Main
	void main() {

		int n=inp.nextInt();
		String s=inp.next();
		int dp[]=new int[n];
		dp[0]=s.charAt(0)=='G'?1:0;
		long gg=dp[0];
		long total=gg;
		for(int i=1;i<n;i++) {
			if(s.charAt(i)=='G') {
				dp[i]=dp[i-1]+1;
				total++;
			}
			gg=Math.max(gg, dp[i]);
		}
		//for(int i=0;i<n;i++) System.out.print(dp[i]+" ");
		//System.out.println();
		for(int i=n-2;i>=0;i--) {
			if(dp[i]>0 && dp[i+1]>0) dp[i]=dp[i+1];
		}
		//for(int i=0;i<n;i++) System.out.print(dp[i]+" ");
		if(dp[0]==0 && n>1) {
			gg=Math.max(gg, 1+dp[1]);
		}
		if(dp[n-1]==0 && n>1) {
			gg=Math.max(gg, 1+dp[n-2]);
		}
		for(int i=1;i<n-1;i++) {
			if(dp[i]==0) gg=Math.max(gg, dp[i-1]+1+dp[i+1]);
		}
		System.out.println(Math.min(total,gg));
	}

}


	/*input

	*/
	import java.io.ByteArrayInputStream;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.PrintWriter;
	import java.util.*;
	public class Main {
	InputStream is;
		PrintWriter out;
		String INPUT = "";
	//class Declaration
	static class pair implements Comparable<pair>{
		int x,y;

		pair (int i,int j)
		{ x=i; y=j;

		}
		public int compareTo(pair p){
			if(this.x!=p.x) { return this.x-p.x;}
			else { return this.y-p.y;}
		}
		public int hashCode() { return (x+" "+y).hashCode();}
		public String toString(){ return x+" "+y;}
		public boolean equals(Object o){
			pair x = (pair) o ;
			return (x.x==this.x&&x.y==this.y);}
	}

void solve(){
int n=ni();
char str[] =ns().toCharArray();
ArrayList<Character> modified= new ArrayList<>();
ArrayList<Integer> count = new ArrayList<>();
char prev='@';
int cnt=1;
int sz=0;
for(int i=0;i<n;++i){
	if(str[i]==prev){
		cnt++;
		count.set(sz-1,cnt);
	}
	else{
		cnt=1;
		modified.add(str[i]);
		count.add(cnt);
		sz++;
		prev=str[i];

	}
}
//pn(modified);
//pn(count);
int ans=0;
//pn("sze "+sz);
for(int i=0;i<sz;++i){
	if(modified.get(i)=='S' && count.get(i)==1){
		if(i-1>=0 && i+1<sz){
			//pn("i "+i);
			ans = Math.max(ans,count.get(i-1)+count.get(i+1));
			//pn(count.get(i-1)+" "+count.get(i+1));
			if(i+3<sz || i-3>=0) ans=Math.max(ans,count.get(i-1)+count.get(i+1)+1);

		}
	}
	else {
		if(modified.get(i)=='G'){
			ans= Math.max(ans,count.get(i));
			if(i+2<sz){
				ans = Math.max(ans,1+count.get(i));
			}

		}
	}
}

pn(ans);

}

int gcd(int a, int b)
{
  if (b == 0)
	return a;
  return gcd(b, a % b);
}

	void run() throws Exception
	{
			is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
			out = new PrintWriter(System.out);

			long s = System.currentTimeMillis();
			solve();
			out.flush();
			if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}

		public static void main(String[] args) throws Exception { new Main().run(); }

	//output methods
	private void pn(Object o)
	{
		out.println(o);
	}
	private void p(Object o)
	{
		out.print(o);
	}

	//input methods
			private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte()
		{
			if(lenbuf == -1)throw new InputMismatchException();
			if(ptrbuf >= lenbuf){
				ptrbuf = 0;
				try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
				if(lenbuf <= 0)return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
		private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

		private double nd() { return Double.parseDouble(ns()); }
		private char nc() { return (char)skip(); }

		private String ns()
		{
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n)
		{
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while(p < n && !(isSpaceChar(b))){
				buf[p++] = (char)b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private char[][] nm(int n, int m)
		{
			char[][] map = new char[n][];
			for(int i = 0;i < n;i++)map[i] = ns(m);
			return map;
		}

		private int[] na(int n)
		{
			int[] a = new int[n];
			for(int i = 0;i < n;i++)a[i] = ni();
			return a;
		}

		private int ni()
		{
			int num = 0, b;
			boolean minus = false;
			while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
			if(b == '-'){
				minus = true;
				b = readByte();
			}

			while(true){
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl()
		{
			long num = 0;
			int b;
			boolean minus = false;
			while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
			if(b == '-'){
				minus = true;
				b = readByte();
			}

			while(true){
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
	}


	/*input

	*/
	import java.io.ByteArrayInputStream;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.PrintWriter;
	import java.util.*;
	public class Main {
	InputStream is;
		PrintWriter out;
		String INPUT = "";
	//class Declaration
	static class pair implements Comparable<pair>{
		int x,y;

		pair (int i,int j)
		{ x=i; y=j;

		}
		public int compareTo(pair p){
			if(this.x!=p.x) { return this.x-p.x;}
			else { return this.y-p.y;}
		}
		public int hashCode() { return (x+" "+y).hashCode();}
		public String toString(){ return x+" "+y;}
		public boolean equals(Object o){
			pair x = (pair) o ;
			return (x.x==this.x&&x.y==this.y);}
	}

void solve(){
int n=ni();
char str[] =ns().toCharArray();
ArrayList<Character> modified= new ArrayList<>();
ArrayList<Integer> count = new ArrayList<>();
char prev='@';
int cnt=1;
int sz=0;
for(int i=0;i<n;++i){
	if(str[i]==prev){
		cnt++;
		count.set(sz-1,cnt);
	}
	else{
		cnt=1;
		modified.add(str[i]);
		count.add(cnt);
		sz++;
		prev=str[i];

	}
}
//pn(modified);
//pn(count);
int ans=0;
//pn("sze "+sz);
for(int i=0;i<sz;++i){
	if(modified.get(i)=='S' && count.get(i)==1){
		if(i-1>=0 && i+1<sz){
			//pn("i "+i);
			ans = Math.max(ans,count.get(i-1)+count.get(i+1));
			//pn(count.get(i-1)+" "+count.get(i+1));
			if(i+3<sz || i-3>=0) ans=Math.max(ans,count.get(i-1)+count.get(i+1)+1);

		}
	}
	else {
		if(modified.get(i)=='G'){
			if(i+2<sz){
				ans = Math.max(ans,count.get(i+2)+count.get(i));
			}

		}
	}
}

pn(ans);

}

int gcd(int a, int b)
{
  if (b == 0)
	return a;
  return gcd(b, a % b);
}

	void run() throws Exception
	{
			is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
			out = new PrintWriter(System.out);

			long s = System.currentTimeMillis();
			solve();
			out.flush();
			if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}

		public static void main(String[] args) throws Exception { new Main().run(); }

	//output methods
	private void pn(Object o)
	{
		out.println(o);
	}
	private void p(Object o)
	{
		out.print(o);
	}

	//input methods
			private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte()
		{
			if(lenbuf == -1)throw new InputMismatchException();
			if(ptrbuf >= lenbuf){
				ptrbuf = 0;
				try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
				if(lenbuf <= 0)return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
		private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

		private double nd() { return Double.parseDouble(ns()); }
		private char nc() { return (char)skip(); }

		private String ns()
		{
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n)
		{
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while(p < n && !(isSpaceChar(b))){
				buf[p++] = (char)b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private char[][] nm(int n, int m)
		{
			char[][] map = new char[n][];
			for(int i = 0;i < n;i++)map[i] = ns(m);
			return map;
		}

		private int[] na(int n)
		{
			int[] a = new int[n];
			for(int i = 0;i < n;i++)a[i] = ni();
			return a;
		}

		private int ni()
		{
			int num = 0, b;
			boolean minus = false;
			while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
			if(b == '-'){
				minus = true;
				b = readByte();
			}

			while(true){
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl()
		{
			long num = 0;
			int b;
			boolean minus = false;
			while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
			if(b == '-'){
				minus = true;
				b = readByte();
			}

			while(true){
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
	}