from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
import re
mod = 1000000007
iinp = lambda: int(sys.stdin.readline())
inp = lambda: sys.stdin.readline().strip()
strl = lambda: list(inp().strip().split(" "))
intl = lambda: list(map(int, inp().split(" ")))
mint = lambda: map(int, inp().split())
flol = lambda: list(map(float, inp().split(" ")))
flush = lambda: sys.stdout.flush()
def myCode():
num = int(input())
savem = input()
array = list(map(int, input().split()))
box, ss = [0, -2147483647]
for i in range(0, num):
if savem[i] == '1':
box += max(array[i], ss)
if savem[i] == '0' or array[i] < ss:
ss = array[i]
print(box)
def main():
for t in range(iinp()):
myCode()
if __name__ == '__main__':
main()
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define max(a,b) (((b)>(a)) ? b : a )
#define min(a,b) (((a)<(b)) ? a : b)
#define endl "\n"
#define space " "
long long binarySearch(int key, int mouse[], int s, int e)
{
while(s<=e)
{
int mid=(s+e)/2;
if (mouse[mid]==key)
return mid;
else if (mouse[mid]>key)
e=mid-1;
else
s=mid+1;
}
return -1;
//O(logn), O(1)
}
long long GCD(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return GCD(a-b, b);
return GCD(a, b-a);
}
long long LCM(int a, int b)
{
return a*b/GCD(a,b);
}
//---------------------------------------------------------------------------------------------------//
void solve()
{
int n;
cin>>n;
string lid;
cin>>lid;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int i=n-1;
while(i>=0)
{
if (lid[i]=='0')
{
i--;
continue;
}
int last=i;
while(i-1>=0 && lid[i-1]=='1' && arr[i-1]>arr[last])
{
i--;
}
if (i-1>=0 && lid[i-1]=='0' && arr[i-1]>arr[last])
{
lid[last]='0';
lid[i-1]='1';
i--;
}
i--;
}
// cout<<lid<<endl;
int answer=0;
for(int i=0;i<n;i++)
{
if (lid[i]=='1')
{
answer+=arr[i];
}
}
cout<<answer<<endl;
}
int main()
{
fastIO;
int t;
cin>>t;
while(t--)
{
solve();
}
}
961A - Tetris | 1635B - Avoid Local Maximums |
20A - BerOS file system | 1637A - Sorting Parts |
509A - Maximum in Table | 1647C - Madoka and Childish Pranks |
689B - Mike and Shortcuts | 379B - New Year Present |
1498A - GCD Sum | 1277C - As Simple as One and Two |
1301A - Three Strings | 460A - Vasya and Socks |
1624C - Division by Two and Permutation | 1288A - Deadline |
1617A - Forbidden Subsequence | 914A - Perfect Squares |
873D - Merge Sort | 1251A - Broken Keyboard |
463B - Caisa and Pylons | 584A - Olesya and Rodion |
799A - Carrot Cakes | 1569B - Chess Tournament |
1047B - Cover Points | 1381B - Unmerge |
1256A - Payment Without Change | 908B - New Year and Buggy Bot |
979A - Pizza Pizza Pizza | 731A - Night at the Museum |
742A - Arpa’s hard exam and Mehrdad’s naive cheat | 1492A - Three swimmers |