Bertown is a city with $n$ buildings in a straight line.

The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length $n$, where the $i$-th character is "1" if there is a mine under the building number $i$ and "0" otherwise.

Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered $x$ is activated, it explodes and activates two adjacent mines under the buildings numbered $x-1$ and $x+1$ (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes $a$ coins. He can repeat this operation as many times as you want.

Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes $b$ coins. He can also repeat this operation as many times as you want.

The sapper can carry out operations in any order.

You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.


-----Input-----

The first line contains one positive integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Then $t$ test cases follow.

Each test case begins with a line containing two integers $a$ and $b$ ($1 \le a, b \le 1000$) — the cost of activating and placing one mine, respectively.

The next line contains a map of mines in the city — a string consisting of zeros and ones.

The sum of the string lengths for all test cases does not exceed $10^5$.


-----Output-----

For each test case, output one integer — the minimum number of coins that the sapper will have to pay.


-----Example-----
Input
2
1 1
01000010
5 1
01101110

Output
2
6



-----Note-----

In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, $b=1$ coin for placing a mine and $a=5$ coins for activating.

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Sorting

Mathematics

Greedy algorithms


Solutions:
for z in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	st = s.strip('0')
	if len(st) != 0:
		count = 0
		total = a
		for i in st:
			if i == '0':
				count += 1
			elif count != 0:
				if count * b <= a:
					total += count * b
				else:
					total += a
				count = 0
		print(total)
	else:
		print(0)


import sys
for _ in range(int(sys.stdin.readline())):
	a = list(map(int, sys.stdin.readline().strip().split(' ')))
	b = sys.stdin.readline().strip()
	n = 0
	inq = False
	s = 0
	if b.count('1') != 0:
		for i in b[b.index('1'):]:
			if i == '1':
				if not inq:
					inq = True
					if n != 0:
						s += min(a[0], n * a[1])
					else:
						s += a[0]
					n = 0
			else:
				inq = False
				n += 1
	print(s)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	cost = 0
	rowcost = a
	amchain = False
	for c in s:
		if c == '1':
			if not amchain and rowcost:
				amchain = True
				cost += min(rowcost, a)
		elif amchain:
			amchain = False
			rowcost = b
		else:
			rowcost += b
	print(cost)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip()
	s = s.strip('0')
	s = s.rstrip('0')
	x = a // b
	(ans, f) = (0, 0)
	l = s.split('1')
	for i in range(len(l)):
		t = len(l[i])
		if t > 0:
			if t * b < a:
				ans += t * b
			else:
				ans += a
	if '1' in s:
		ans += a
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	m = input()
	flag = False
	l = []
	prev = 0
	flag = False
	for i in range(len(m)):
		if flag:
			if m[i] == '0':
				l.append((prev, i - 1))
				flag = False
			else:
				continue
		elif m[i] == '0':
			continue
		else:
			flag = True
			prev = i
	if flag:
		l.append((prev, len(m) - 1))
	if len(l) == 1:
		print(a)
	elif len(l) == 0:
		print(0)
	else:
		ans = a
		for i in range(1, len(l)):
			if (l[i][0] - l[i - 1][1] - 1) * b <= a:
				ans += (l[i][0] - l[i - 1][1] - 1) * b
			else:
				ans += a
		print(ans)


import sys, time
from os import path
from collections import *
from heapq import *
from math import *

def solve(kkkk):
	(a, b) = inm()
	s = ins()
	vect = []
	i = 0
	while i < len(s):
		if s[i] == '0':
			i += 1
		else:
			j = i
			while j < len(s) and s[j] == '1':
				j += 1
			vect.append((i, j - 1))
			i = j
	ans = 0
	if len(vect):
		ans = a
		prev = vect[0][1]
		del vect[0:1]
		for x in vect:
			bb = b * (x[0] - prev - 1)
			ans += min(bb, a)
			prev = x[1]
	out(ans)

def main():
	t = 1
	t = inp()
	for i in range(t):
		solve(i + 1)
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	localsys = 1
cin = sys.stdin.readline
cout = sys.stdout.write

def inp():
	return int(cin().strip())

def ins():
	return list(cin().strip())

def inm():
	return map(int, cin().strip().split())

def ina():
	return list(map(int, cin().strip().split()))

def out(a):
	cout(str(a) + '\n')

def outa(a):
	cout(' '.join(map(str, a)) + '\n')

def outm(a, b, c=None, d=None):
	cout(str(a) + ' ' + str(b))
	if c != None:
		cout(' ' + str(c))
	if d != None:
		cout(' ' + str(d))
	cout('\n')
main()
if localsys:
	print('\n\nTime Elapsed :', time.time() - start_time, 'seconds')


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	check = False
	cnt = 0
	sm = 0
	i = 0
	while i < n:
		sm += b
		if s[i] == '1':
			if not check:
				check = True
				cnt += a
			else:
				cnt += min(sm, a)
			sm = 0
			while i < n and s[i] == '1':
				i += 1
		i += 1
	print(cnt)


t = int(input())
for i in range(t):
	from sys import stdin
	(a, b) = [int(i) for i in stdin.readline().strip().split()]
	s = input()
	zeroSeen = 1000000
	cost = 0
	for j in s:
		if j == '0':
			zeroSeen += 1
		if j == '1':
			cost += min(a, b * zeroSeen)
			zeroSeen = 0
	print(cost)


try:
	t = int(input())
	for i in range(t):
		(a, b) = map(int, input().split())
		s = input()
		if '1' not in s:
			print(0)
		else:
			short_s = []
			short_s.append(s[0])
			for i in range(1, len(s)):
				if short_s[-1] == '1' and s[i] == '1':
					pass
				else:
					short_s.append(s[i])
			new_s = ''.join(short_s)
			pos = -1
			for i in range(len(new_s)):
				if new_s[i] == '1':
					pos = i
					break
			posj = -1
			for j in range(len(new_s) - 1, -1, -1):
				if new_s[j] == '1':
					posj = j
					break
			s = new_s[pos:posj + 1]
			p = [0] * len(s)
			p[0] = a
			res = 0
			zero = 0
			for i in range(1, len(s)):
				if s[i] == '0':
					zero += 1
				else:
					p[i] = min(zero * b, a)
					zero = 0
			res = sum(p)
			print(res)
except:
	pass


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	c = 0
	t = 0
	if '1' in s:
		ans = a
	else:
		print(0)
		continue
	for i in s:
		if i == '0':
			if t:
				c += 1
		else:
			t = 1
			if c:
				if c * b <= a:
					ans += c * b
				else:
					ans += a
				c = 0
	print(ans)


def SavingTheCity4():
	t = int(input())
	for i in range(t):
		(a, b) = input().split()
		(a, b) = (int(a), int(b))
		m = input().strip('0').rstrip('0')
		if len(m) == 0:
			print(0)
		else:
			ngb = 1
			nbeg = 0
			V = 0
			T = True
			for x in m:
				if x == '0':
					nbeg += 1
					T = False
				elif x == '1' and T == False:
					ngb += 1
					T = True
					if nbeg * b < a:
						V += nbeg * b - a
					nbeg = 0
			V += ngb * a
			print(V)
SavingTheCity4()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = [int(a) for a in input()]
	ls1 = []
	if 1 in s:
		start = s.index(1)
		i = start
		while i < len(s):
			fn = 1
			if s[i] == 0:
				for j in range(i, len(s)):
					if s[j] == 1:
						fn = 0
						ls1.append(j - i)
						i = j
						break
			if fn:
				i += 1
		sm = 0
		for i in ls1:
			sm += min(i * b, a)
		sm += a
	else:
		sm = 0
	print(sm)


val = int(input())
for i in range(val):
	a1 = input().split()
	a = int(a1[0])
	b = int(a1[1])
	a2 = input()
	mines = list()
	for j in range(len(a2)):
		if a2[j] == '1':
			mines.append(j)
	total = a
	for j in range(len(mines) - 1):
		total += a
		if (mines[j + 1] - mines[j] - 1) * b < a:
			total += (mines[j + 1] - mines[j] - 1) * b - a
	if len(mines) == 0:
		print(0)
	else:
		print(total)


T = int(input())
for t in range(T):
	(a, b) = [int(x) for x in input().split()]
	mines = input()
	price = 0
	last = ''
	not_mines = []
	there_was_mines = False
	not_mine = 0
	for c in mines:
		if c == '1':
			if last != c:
				price += a
				if not_mine > 0:
					if there_was_mines:
						not_mines.append(not_mine)
					not_mine = 0
			there_was_mines = True
		else:
			not_mine += 1
		last = c
	for m in not_mines:
		if m * b < a:
			price = price - a + m * b
	print(price)


def main():
	for _ in range(int(input())):
		(cost_to_activate, cost_to_place) = [int(x) for x in input().split()]
		mines = input()
		gap_allowed = cost_to_activate // cost_to_place
		ans = 0
		for i in range(len(mines)):
			if mines[i] == '1':
				prev_was_mine = True
				break
		else:
			prev_was_mine = False
		gap = 0
		while i < len(mines) - 1:
			i += 1
			if mines[i] == '1':
				if prev_was_mine:
					continue
				prev_was_mine = True
				if gap <= gap_allowed:
					ans += gap * cost_to_place
				else:
					ans += cost_to_activate
			elif prev_was_mine:
				gap = 1
				prev_was_mine = False
			else:
				gap += 1
		if prev_was_mine or gap > 0:
			ans += cost_to_activate
		print(ans)
main()


for i in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	(cnt, ans) = (1000000, 0)
	for j in range(len(s)):
		if s[j] == '0':
			cnt += 1
		else:
			ans += min(a, b * cnt)
			cnt = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().strip(' ').split(' '))
	s = input().strip(' ').strip('0')
	n = len(s)
	i = 0
	ones = 0
	zeros = []
	c = 0
	while i < n:
		if s[i] == '1':
			k = 0
			while i + k < n and s[i + k] == '1':
				k += 1
			ones += 1
			if c != 0:
				zeros.append(c)
				c = 0
			i += k
		else:
			c += 1
			i += 1
	zeros.sort()
	n = len(zeros)
	ans = float('inf')
	c = 0
	for i in range(n):
		ans = min(ones * a + c, ans)
		c += zeros[i] * b
		ones -= 1
	ans = min(ones * a + c, ans)
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7
dp = [[-1, -1] for i in range(100010)]

@iterative
def rec(pos, a, v1, v2, type):
	if pos >= len(a):
		if type == 0:
			yield v1
		else:
			yield 0
	if dp[pos][type] != -1:
		yield dp[pos][type]
	temp = inf
	if pos == 0:
		temp = min(temp, v1 + (yield rec(pos + 2, a, v1, v2, 1)))
		temp = min(temp, a[pos + 1] * v2 + (yield rec(pos + 2, a, v1, v2, 0)))
	else:
		temp = min(temp, v1 + (yield rec(pos + 2, a, v1, v2, 1)))
		if pos != len(a) - 1:
			temp = min(temp, a[pos + 1] * v2 + (yield rec(pos + 2, a, v1, v2, 0)))
	dp[pos][type] = temp
	yield temp

def solve(case):
	(a, b) = sep()
	s = list(inp())
	n = len(s)
	for i in range(n):
		s[i] = int(s[i])
	l = -1
	for i in range(n):
		if s[i] == 1:
			l = i
			break
	r = n
	for i in range(n - 1, -1, -1):
		if s[i] == 1:
			r = i
			break
	if l == -1:
		print(0)
		return
	seg = []
	chck = 1
	prev = l
	for i in range(l, r + 1):
		if s[i] == chck:
			continue
		else:
			seg.append(i - prev)
			prev = i
			chck = 1 - chck
	seg.append(r - prev + 1)
	if len(seg) == 1:
		print(a)
		return
	for i in range(len(seg)):
		dp[i][0] = -1
		dp[i][1] = -1
	print(rec(0, seg, a, b, 1))
testcase(int(inp()))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = list(input())
	cnt = 0
	ans = a
	if '1' in s:
		ind = s.index('1')
	else:
		print('0')
		continue
	for i in range(ind + 1, len(s)):
		if s[i] == '0':
			cnt += 1
		else:
			ans += min(a, cnt * b)
			cnt = 0
	print(ans)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	(a, b) = IP()
	s = input()
	n = len(s)
	pos = -1
	cost = 0
	for i in range(n):
		if s[i] == '1' and pos == -1:
			cost += a
			pos = i
		elif s[i] == '1':
			cost += min(b * (i - pos - 1), a)
			pos = i
	print(cost)
	return
t = II()
for i in range(t):
	solve()


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	(count, ans) = (10000000000000.0, 0)
	for i in s:
		if i == '0':
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


def solve(a, b, s):
	n = len(s)
	dp = [float('inf')] * n
	last = float('-inf')
	if s[0] == '1':
		dp[0] = a
		last = 0
	else:
		dp[0] = 0
	for i in range(1, n):
		if s[i] == '1':
			if s[i - 1] == '1':
				dp[i] = dp[i - 1]
			else:
				dp[i] = min(dp[i - 1] + a, (i - last - 1) * b + dp[max(0, last)])
			last = i
		else:
			dp[i] = dp[i - 1]
	return dp[n - 1]
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	print(solve(a, b, s))


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		s = input().strip('0')
		if not len(s):
			print(0)
			continue
		(one, ze) = ([], [])
		c = 0
		c1 = 0
		for i in s:
			if i == '1':
				c += 1
				if c1:
					ze.append(c1)
					c1 = 0
			else:
				c1 += 1
				if c:
					one.append(c)
					c = 0
		if c:
			one.append(c)
		if c1:
			ze.append(c1)
		ans = a
		for i in range(1, len(one)):
			ans += min(a, ze[i - 1] * b)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input()) if True else 1):
	(a, b) = map(int, input().split())
	s = input()
	l = list(s)
	n = len(l)
	ss = list()
	nu = list()
	ptn = 0
	while ptn < n and l[ptn] == '0':
		ptn += 1
	if ptn == n:
		print('0')
		continue
	else:
		aruyu = []
		tt = list()
		add = 0
		sb = list()
		for i in range(ptn, n):
			if l[i] == '1':
				if add:
					aruyu.append(add)
				add = 0
				tt.append(2)
			else:
				add += 1
				sb.append(1)
		res = 0
		for i in aruyu:
			if i * b > a:
				res += a
				sb.append(2)
			else:
				res += i * b
				sb.append(3)
		ans = res + a
		r = 9
		print(ans - 9 + r)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def valid(row, col, rows, cols, rcross, lcross):
	return rows[row] == 0 and cols[col] == 0 and (rcross[col + row] == 0) and (lcross[col - row] == 0)

def main():
	ans = ''
	for _ in range(ii()):
		(a, b) = mi()
		s = I().strip()
		start = 0
		end = len(s) - 1
		while start <= end:
			if s[start] == '0':
				start += 1
			elif s[end] == '0':
				end -= 1
			else:
				break
		if start > end:
			ans += '0\n'
		else:
			cnt = a
			i = start
			while i <= end:
				if s[i] == '0':
					c = 0
					while s[i] == '0':
						c += 1
						i += 1
					cnt += min(a, b * c)
				else:
					i += 1
			ans += str(cnt) + '\n'
	print(ans)
main()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	s1 = s.split('1')
	count = 0
	if s[0] != '0' and s[len(s) - 1] != '0':
		for i in range(len(s1)):
			if b * len(s1[i]) > a:
				count += a
				if i == len(s1) - 1:
					count += a
			elif b * len(s1[i]) <= a:
				count += b * len(s1[i])
				if i == len(s1) - 1:
					count += a
	elif s[0] != '0' and s[len(s) - 1] == '0':
		for i in range(len(s1) - 1):
			if b * len(s1[i]) > a:
				count += a
				if i == len(s1) - 2:
					count += a
			elif b * len(s1[i]) <= a:
				count += b * len(s1[i])
				if i == len(s1) - 2:
					count += a
	elif s[0] == '0' and s[len(s) - 1] != '0':
		for i in range(1, len(s1)):
			if b * len(s1[i]) > a:
				count += a
				if i == len(s1) - 1:
					count += a
			elif b * len(s1[i]) <= a:
				count += b * len(s1[i])
				if i == len(s1) - 1:
					count += a
	elif s[0] == '0' and s[len(s) - 1] == '0':
		if len(s1) == 2:
			count = count + a
		for i in range(1, len(s1) - 1):
			if b * len(s1[i]) > a:
				count += a
				if i == len(s1) - 2:
					count += a
			elif b * len(s1[i]) <= a:
				count += b * len(s1[i])
				if i == len(s1) - 2:
					count += a
	if len(s1) == 0:
		count = 0
	print(count)


t = int(input())
for x in range(t):
	s = input()
	s1 = s.split()
	a = int(s1[0])
	b = int(s1[1])
	l = input()
	temp = []
	temp.append(l[0])
	for i in range(1, len(l)):
		if l[i] == '1' and l[i - 1] != '1':
			temp.append(l[i])
		elif l[i] == '0':
			temp.append(l[i])
	cnt = 0
	tot = 0
	for i in range(len(temp)):
		if temp[i] == '0':
			cnt = cnt + 1
		else:
			if tot == 0:
				tot = a
			else:
				tot = min(tot + a, tot + b * cnt)
			cnt = 0
	print(tot)


def solve():
	(a, b) = map(int, input().split())
	s = str(input())
	n = len(s)
	comp = []
	score = 0
	if '1' not in s:
		print(0)
		return
	j = 0
	while s[j] == '0':
		j += 1
	while j < n and s[j] == '1':
		j += 1
	score += a
	while j < n:
		i = j
		while j < n and s[j] == '0':
			j += 1
		comp.append(j - i)
		i = j
		while j < n and s[j] == '1':
			j += 1
		if j != i:
			score += a
	if s[-1] == '0':
		comp.pop()
	comp.sort()
	for i in comp:
		newscore = score - a + i * b
		score = min(score, newscore)
	print(score)

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


def get_min_cost(mines, a, b):
	mines = mines.strip('0')
	if len(mines) == 0:
		return 0
	max_zeros_len = a // b
	ones_run_count = 0
	short_zeros_count = 0
	short_zeros_sum_length = 0
	for (i, ch) in enumerate(mines):
		if ch == '1':
			if i == 0 or mines[i - 1] == '0':
				ones_run_count += 1
			if i > 0 and mines[i - 1] == '0':
				l = i - start_index
				if l <= max_zeros_len:
					short_zeros_count += 1
					short_zeros_sum_length += l
		if ch == '0':
			if mines[i - 1] == '1':
				start_index = i
	return a * (ones_run_count - short_zeros_count) + b * short_zeros_sum_length
n_samples = int(input())
for t in range(n_samples):
	(a, b) = map(int, input().split(' '))
	mines = input()
	print(get_min_cost(mines, a, b))


import sys

def solve():
	(a, b) = map(int, input().split())
	s = input()
	(x, y) = (sys.maxsize, 0)
	for i in s:
		if i == '0':
			x += 1
		else:
			y += min(a, b * x)
			x = 0
	print(y)
for _ in range(int(input())):
	solve()


import sys
for l in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	(x, y) = (sys.maxsize, 0)
	for i in s:
		if i == '0':
			x = x + 1
		else:
			y = y + min(a, b * x)
			x = 0
	print(y)


t_n = int(input())
for i_t in range(t_n):
	(a, b) = map(int, input().split())
	s = input()
	s = s.strip('0')
	result = 0
	i = 0
	while i < len(s):
		ones_n = 0
		while i < len(s) and s[i] == '1':
			ones_n += 1
			i += 1
		zeros_n = 0
		while i < len(s) and s[i] == '0':
			zeros_n += 1
			i += 1
		if zeros_n != 0:
			result += min(a, zeros_n * b)
		else:
			result += a
	print(result)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def solve(a, b, lst):
	if lst == '0' * len(lst):
		return 0
	lst = lst.strip('0')
	lst1 = []
	cnt = 0
	zero = []
	ind = [[float('inf'), 0]]
	prev = -1
	for i in range(len(lst)):
		if lst[i] == '0':
			cnt += 1
		else:
			if cnt != 0:
				zero.append(cnt)
				if b * cnt + a > 2 * a:
					ind.append([prev, i])
				cnt = 0
			prev = i + 1
	ind.append([len(lst), float('inf')])
	ans = 0
	for i in range(len(ind) - 1):
		l1 = lst[ind[i][1]:ind[i + 1][0]]
		ans += (len(l1) - l1.count('1')) * b + a
	return ans
for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	lst = input()
	print(solve(a, b, lst))


tc = int(input())
for _ in range(tc):
	(a, b) = map(int, input().split())
	s = input()
	cursor = s[0]
	cnt = 0
	l = []
	for i in s:
		if cursor == i:
			cnt += 1
		else:
			l.append([cursor, cnt])
			if cursor == '1':
				cursor = '0'
			else:
				cursor = '1'
			cnt = 1
	l.append([cursor, cnt])
	if l[0][0] == '0':
		l.pop(0)
	if len(l) == 0:
		print(0)
		continue
	if len(l) == 1:
		print(a)
		continue
	if l[-1][0] == '0':
		l.pop(-1)
	if len(l) == 1:
		print(a)
		continue
	answer = 0
	for i in range(1, len(l), 2):
		if i == 1:
			answer += min(a * 2, l[i][1] * b + a)
		else:
			answer += min(a, l[i][1] * b)
	print(answer)


a = int(input())
for i in range(a):
	(a, b) = map(int, input().split())
	s = input()
	arr = [int(i) for i in s]
	brr = []
	for i in arr:
		if i == 0:
			if len(brr) > 0:
				brr.append(0)
		elif len(brr) == 0 or brr[len(brr) - 1] == 0:
			brr.append(1)
		else:
			brr[len(brr) - 1] = brr[len(brr) - 1] + 1
	while len(brr) > 0 and brr[len(brr) - 1] == 0:
		brr.pop()
	arr = []
	k = 0
	for i in brr:
		if i == 0:
			if k == 0:
				arr.append(1)
				k = 1
			else:
				arr[len(arr) - 1] = arr[len(arr) - 1] + 1
		else:
			arr.append(i)
			k = 0
	ans = 0
	for i in range(1, len(arr) - 1, 2):
		if arr[i] * b + a > 2 * a:
			ans = ans + a
		else:
			ans = ans + arr[i] * b
	if len(arr) == 0:
		print(0)
	else:
		print(ans + a)


t = int(input())
while t:
	t -= 1
	(a, b) = map(int, input().split())
	l = input()
	l = l.strip('0')
	if '1' in l:
		ans = a
		i = 0
		while i < len(l):
			if l[i] == '0':
				c = 0
				for j in range(i, len(l)):
					if l[j] == '0':
						c += 1
					else:
						break
				if c * b < a:
					ans += c * b
				else:
					ans += a
				i += j - i
			else:
				i += 1
		print(ans)
	else:
		print(0)


T = int(input())
while T > 0:
	T -= 1
	(a, b) = map(int, input().split())
	S = input()
	N = len(S)
	i = 0
	j = N - 1
	while i < N and S[i] == '0':
		i += 1
	while j >= 0 and S[j] == '0':
		j -= 1
	if i >= N or j < 0:
		print(0)
	else:
		c = 0
		ans = a
		for k in range(i, j + 1):
			if S[k] == '0':
				c += 1
			else:
				if a < c * b:
					ans += a
				else:
					ans += c * b
				c = 0
		print(ans)


t = int(input())
for j in range(t):
	(a, b) = map(int, input().split())
	(c, d) = (-100000, a)
	s = input()
	for i in s:
		if i == '1':
			if c > 0:
				d += min(a, c * b)
			c = 0
		else:
			c += 1
	if c < 0:
		print(0)
	else:
		print(d)


t = int(input())
for ti in range(t):
	(a, b) = map(int, input().split())
	s = input()
	q = 0
	A = []
	f = False
	for i in s:
		if i == '0' and f:
			q += 1
		elif i == '1':
			A.append(min(q * b, a))
			q = 0
			f = True
	print(sum(A) + a * ('1' in s))


from sys import stdin, stdout
(readline, writeline) = (stdin.readline, stdout.write)
for _ in range(int(readline())):
	(a, b) = map(int, readline().split())
	s = list(map(int, readline().rstrip()))
	(n, zero) = (len(s), 0)
	dp = [0] * n
	dp[0] = a if s[0] else 0
	for i in range(1, n):
		dp[i] = dp[i - 1]
		if s[i]:
			if dp[i]:
				dp[i] += min(zero * b, a)
			else:
				dp[i] += a
			zero = 0
		else:
			zero += 1
	writeline('{}\n'.format(dp[-1]))


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	x = 0
	y = 0
	l = 0
	flag = 0
	f = a / b
	for i in range(n):
		if i == 0 and s[i] == '1' or (i > 0 and s[i] == '1' and (s[i - 1] == '0')):
			x += 1
			if l < f and flag == 1:
				y += l
				x -= 1
			l = 0
			flag = 1
		elif s[i] == '0' and flag == 1:
			l += 1
	print(a * x + y * b)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(A, B) = getInts()
	arr = [int(s) for s in input()]
	while arr and arr[-1] == 0:
		arr.pop()
	arr.reverse()
	while arr and arr[-1] == 0:
		arr.pop()
	if not arr:
		return 0
	N = len(arr)
	ans = 0
	segs = []
	curr = 0
	gap = 0
	gaps = []
	for i in range(N):
		if arr[i] == 1:
			curr += 1
			if gap:
				gaps.append(gap)
				gap = 0
		else:
			gap += 1
			if curr:
				segs.append(curr)
				curr = 0
	segs.append(curr)
	ans = len(segs)
	cost = 0
	for g in gaps:
		if B * g <= A:
			ans -= 1
			cost += B * g
	cost += ans * A
	return cost
for _ in range(getInt()):
	print(solve())


n = int(input())
for i in range(n):
	cost = 0
	(a, b) = map(int, input().split())
	k = list(map(int, list(input())))
	x = []
	previos = 0
	for j in range(len(k)):
		if k[j] == 1 and previos == 0:
			x.append([j, 0])
		elif k[j] == 0 and previos == 1:
			x[-1] = [x[-1][0], j - 1]
		previos = k[j]
	if k[-1] == 1:
		x[-1] = [x[-1][0], len(k) - 1]
	v = len(x)
	for p in range(len(x) - 1):
		if (x[p + 1][0] - x[p][1] - 1) * b <= a:
			v -= 1
			cost += (x[p + 1][0] - x[p][1] - 1) * b
	print(cost + v * a)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	y = input()
	z = [int(i) for i in y]
	c = -1
	m = 0
	for i in range(len(z)):
		if z[i] == 1 and c == -1:
			c = i
		elif z[i] == 1 and c > -1:
			s = i - c - 1
			c = i
			m += min(s * b, a)
	if sum(z) != 0:
		m += a
	print(m)


t = int(input())
for _ in range(t):
	(a, b) = [int(x) for x in input().split()]
	m = input().strip('0')
	c = a // b
	activate = 0
	place = 0
	zeros_sequence = 0
	ones_sequence = 0
	for i in m:
		if i == '1' and 0 < zeros_sequence <= c:
			place += zeros_sequence
			zeros_sequence = 0
			ones_sequence += 1
		elif i == '1' and ones_sequence == 0:
			ones_sequence += 1
			zeros_sequence = 0
			activate += 1
		elif i == '0':
			zeros_sequence += 1
			ones_sequence = 0
	print(activate * a + place * b)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	(count, ans) = (10 ** 6, 0)
	for i in s:
		if i == '0':
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = input().split()
	(a, b) = (int(a), int(b))
	st = input()
	s = 0
	bs = 0
	for i in range(len(st)):
		if st[i] == '1':
			break
	if st[i] == '1':
		for c in st[i:]:
			if c == '0':
				bs += b
			else:
				s += min(bs, a)
				bs = 0
		print(s + a)
	else:
		print(0)


t = int(input())
for i in range(t):
	(a, b) = [int(k) for k in input().split()]
	w = input()
	if '1' not in w:
		print(0)
		continue
	z = w.split('1')[1:-1]
	q = []
	res = a
	y = a // b
	for j in z:
		if j:
			qwe = len(j)
			q.append(qwe)
			if qwe <= y:
				res += qwe * b
			else:
				res += a
	print(res)


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P, permutations as PERMUT
from bisect import bisect_left as BL, bisect_right as BR, insort as INSORT
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY, log as LOG
from sys import stdin, stdout
INPUT = stdin.readline
PRINT = stdout.write
L = list
M = map

def player1():
	print('')

def player2():
	print('')

def yes():
	PRINT('Yes\n')

def no():
	PRINT('No\n')

def isPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def factors(x):
	ans = []
	for i in range(1, MC(SQRT(x)) + 1):
		if x % i == 0:
			ans.append(i)
			if x % (x // i) == 0:
				ans.append(x // i)
	return ans

def checkPath(source, destination, g):
	visited = [0] * 101
	q = Q()
	q.append(source)
	visited[source] = 1
	while q:
		node = q.popleft()
		if node == destination:
			return 1
		for v in g[node]:
			if not visited[v]:
				q.append(v)
				visited[v] = 1
	return 0

def Sieve(n):
	prime = [1] * (n + 1)
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = 0
		p += 1
	primes = []
	for p in range(2, n + 1):
		if prime[p]:
			primes.append(p)
	return primes

def main():
	for _ in range(int(INPUT())):
		(a, b) = M(int, INPUT().split())
		s = INPUT()
		n = len(s)
		(start, end) = (-1, -1)
		for i in range(n):
			if s[i] == '1':
				start = i
				break
		else:
			PRINT('%d\n' % 0)
			continue
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				end = i
				break
		else:
			PRINT('%d\n' % 0)
			continue
		if start == end:
			PRINT('%d\n' % a)
			continue
		ans = a
		count = 0
		for i in range(start, end + 1):
			if s[i] == '0':
				count += 1
				continue
			if count == 0:
				continue
			ans = min(ans + b * count, ans + a)
			count = 0
		PRINT('%d\n' % ans)
main()


gans = []
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	w = list(map(int, list(input())))
	u = []
	k = 1
	for i in range(1, len(w)):
		if w[i] == w[i - 1]:
			k += 1
		else:
			u.append([w[i - 1], k])
			k = 1
	u.append([w[-1], k])
	dp = [0] * len(u)
	if u[0][0] == 1:
		dp[0] = a
	for i in range(1, len(u)):
		if u[i][0] == 0:
			dp[i] = dp[i - 1]
		elif i == 1:
			dp[i] = dp[i - 1] + a
		else:
			dp[i] = min(dp[i - 1] + a, dp[i - 1] + b * u[i - 1][1])
	gans.append(dp[-1])
print(*gans, sep='\n')


n = int(input())
for _ in range(n):
	(a, b) = map(int, input().split(' '))
	l = input()
	count = 1
	j = 0
	q = 0
	for i in range(len(l)):
		if l[i] == '1':
			if count == 1:
				count = 0
				j = 0
				q = q + a
			else:
				q = q + min(j * b, a)
				j = 0
		else:
			j = j + 1
	print(q)


tests = int(input())
for _ in range(tests):
	(t, b) = map(int, input().split())
	mines = input().strip('0')
	zeros = []
	onesChecker = False
	counter = 0
	for mine in mines:
		if mine == '1' and counter > 0:
			zeros.append(counter)
			counter = 0
			onesChecker = True
		elif mine == '1':
			onesChecker = True
		else:
			counter += 1
	zeros = sorted(zeros)
	if not onesChecker:
		ones = 0
	else:
		ones = len(zeros) + 1
	currBest = ones * t
	for zero in zeros:
		newBest = currBest - t + zero * b
		if newBest < currBest:
			currBest = newBest
		else:
			break
	print(currBest)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = list(input())
	n = len(l)
	t = ''
	c = []
	z = 0
	f = 0
	for i in range(n):
		if l[i] == '1':
			t += '1'
			if z != 0 and f != 1:
				c.append('0' * z)
			z = 0
			f = 0
		else:
			if i == 0:
				f = 1
				z = 0
			else:
				z += 1
			if len(t) != 0:
				c.append(t)
				t = ''
	if len(t) != 0:
		c.append(t)
	s = a
	curr = 0
	z = 0
	f = 0
	for i in c:
		if i[0] == '0':
			if a < len(i) * b:
				s += a
			else:
				s += len(i) * b
				curr += 1
			z += 1
		if i[0] == '1':
			f = 1
	if f == 1:
		print(s)
	else:
		print(0)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	c = 0
	f = 0
	t = 0
	for i in s:
		if i == '1' and f == 0:
			c = a
			f = 1
		if i == '1' and f == 1:
			c += min(a, t * b)
			t = 0
		if i == '0' and f == 1:
			t += 1
	print(c)


import itertools

def countstrs(s):
	return [(k, len(list(g))) for (k, g) in itertools.groupby(s)]

def do():
	(a, b) = map(int, input().split())
	s = input()
	if s.count('1') == 0:
		print(0)
		return
	s = s.strip('0')
	c = countstrs(s)
	c = filter(lambda x: x[0] == '0', c)
	res = a
	for (char, cnt) in c:
		if cnt * b < a:
			res += cnt * b
		else:
			res += a
	print(res)
q = int(input())
for _ in range(q):
	do()


import math
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if s.count('0') == len(s):
		print(0)
		continue
	if s.count('1') == len(s):
		print(a)
		continue
	i = 0
	while i < len(s):
		if s[i] == '0':
			i += 1
			continue
		break
	s = s[i:]
	i = len(s) - 1
	while i > -1:
		if s[i] == '0':
			i -= 1
			continue
		break
	s = s[0:i + 1]
	l = []
	cnt = 0
	f = 1
	for i in s:
		if f == 1:
			if i == '1':
				cnt += 1
			else:
				f = 0
				l.append(cnt)
				cnt = 1
			continue
		if f == 0:
			if i == '0':
				cnt += 1
			else:
				f = 1
				l.append(cnt)
				cnt = 1
	l.append(cnt)
	i = 1
	ans = 0
	while i < len(l):
		if l[i] * b + a < a + a:
			ans += l[i] * b
			i += 2
			continue
		else:
			ans += a
		i += 2
	print(ans + a)


import typing
import sys
import math
import collections
import bisect
import itertools
import heapq
import decimal
import copy
INF = 10 ** 20
MOD = 10 ** 9 + 7

def ni():
	return int(sys.stdin.buffer.readline())

def ns():
	return map(int, sys.stdin.buffer.readline().split())

def na():
	return list(map(int, sys.stdin.buffer.readline().split()))

def na1():
	return list(map(lambda x: int(x) - 1, sys.stdin.buffer.readline().split()))

def main():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		s = input()
		cnt1 = 0
		cnt0 = 0
		res1 = []
		res0 = []
		flg1 = True
		for i in range(len(s)):
			if s[i] == '0':
				if flg1:
					continue
				cnt0 += 1
				if i != 0:
					if s[i - 1] != s[i]:
						res1.append(cnt1)
						cnt1 = 0
			else:
				flg1 = False
				cnt1 += 1
				if len(res1) != 0:
					if i != 0:
						if s[i - 1] != s[i]:
							res0.append(cnt0)
							cnt0 = 0
		if cnt1 != 0:
			res1.append(cnt1)
		ans = 0
		acnt = 0
		for ri in res0:
			if b * ri <= a:
				ans += b * ri
				acnt += 1
		ans += a * (len(res1) - acnt)
		print(ans)
main()


from itertools import *
for _ in range(int(input())):
	(a, b) = tuple(map(int, input().split()))
	minemap = [(k, len(list(g))) for (k, g) in groupby(input().strip('0'))]
	price_a = a * int(len(minemap[::2]))
	minprice = price_a
	for (_, zeros) in minemap[1::2]:
		if zeros * b < a:
			minprice -= a
			minprice += zeros * b
	print(minprice)


def solve(s, a, b, ans):
	found = False
	last_one = -1
	cost = 0
	for i in range(len(s)):
		if s[i] == '1':
			if last_one == -1:
				cost += a
			else:
				cost = min((i - last_one - 1) * b + cost, cost + a)
			last_one = i
	ans.append(str(cost))

def main():
	t = int(input())
	ans = []
	for i in range(t):
		(a, b) = map(int, input().split())
		s = input()
		solve(s, a, b, ans)
	print('\n'.join(ans))
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import deque
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if s == '0' * len(s):
		print(0)
	else:
		cost = 0
		n = len(s)
		pre = -1
		for i in range(n):
			if s[i] == '1':
				if i == 0 or s[i - 1] != s[i]:
					if pre == -1:
						cost += a
					else:
						var = (i - pre - 1) * b
						if var <= a:
							cost += var
						else:
							cost += a
				pre = i
		print(cost)


from sys import stdin, stdout
from math import factorial as f
from random import randint, shuffle
input = stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()[:-1]
	s = s.lstrip('0')
	if not s:
		print(0)
		continue
	ans = a
	cnt = 0
	for i in s:
		if i == '0':
			cnt += 1
		elif cnt:
			ans += min(a, cnt * b)
			cnt = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	mines = [bool(int(v)) for v in input() + '0']
	n = len(mines)
	mine_found = False
	bridge_length = a
	cost = 0
	for i in range(n):
		if mine_found:
			if not mines[i]:
				cost += a
				bridge_length = 1
				mine_found = False
		elif mines[i]:
			if bridge_length * b < a:
				cost = cost - a + bridge_length * b
			mine_found = True
		else:
			bridge_length += 1
	print(cost)


t = int(input())
for _ in range(t):
	(a, b) = [int(x) for x in input().split()]
	s = list(input())
	cnt = 0
	ans = a
	if '1' in s:
		v = s.index('1')
	else:
		print('0')
		continue
	for i in range(v + 1, len(s)):
		if s[i] == '0':
			cnt += 1
		else:
			ans += min(a, cnt * b)
			cnt = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	last = -10 ** 9
	n = len(s)
	ans = 0
	for i in range(n):
		if s[i] == '1':
			ans += min(a, b * (i - last - 1))
			last = i
	print(ans)


for _ in range(0, int(input())):
	(blast, plant) = map(int, input().split())
	s = input()
	count = float('inf')
	ans = 0
	for i in range(0, len(s)):
		if s[i] == '0':
			count += 1
		else:
			ans += min(blast, count * plant)
			count = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = list(map(int, input().split()))
	s = input()
	count = float('inf')
	res = 0
	for i in range(len(s)):
		if s[i] == '0':
			count += 1
		else:
			res += min(a, b * count)
			count = 0
	print(res)


import math
t = int(input())
while t > 0:
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	s = input()
	unmined = 10 ** 6
	ans = 0
	for i in range(0, len(s)):
		if s[i] == '0':
			unmined += 1
		elif unmined > 10 ** 6:
			ans = ans + a
			unmined = 0
		else:
			ans = ans + min(a, unmined * b)
			unmined = 0
	t = t - 1
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	(a, b) = Ri()
	s = ri()
	flag = False
	cnt = 0
	i = 0
	while i < len(s) and s[i] != '1':
		i += 1
	if i == len(s):
		print(0)
		continue
	while i < len(s) and s[i] == '1':
		i += 1
	if i == len(s):
		print(a)
		continue
	lis = []
	for i in range(i, len(s)):
		if s[i] == '0':
			cnt += 1
		elif cnt != 0:
			lis.append(cnt)
			cnt = 0
	ans = a
	for i in range(len(lis)):
		if b * lis[i] + a < 2 * a:
			flag = False
			ans += b * lis[i]
		else:
			flag = True
			ans += a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	if not len(s):
		print(0)
		continue
	n = len(s)
	ans = 0
	res = []
	prev = s[0]
	count = 1
	for i in range(1, n):
		if s[i] == prev:
			count += 1
		else:
			res.append(count)
			count = 1
		prev = s[i]
	res.append(count)
	for i in res[1::2]:
		ans += min(a, i * b)
	ans += a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	x = 0
	y = 10 ** 10
	for i in input():
		if i == '0':
			(x, y) = (min(x, y), min(y + b, x + b + a))
		else:
			(x, y) = (10 ** 10, min(y, x + a))
	print(min(x, y))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	st = list(map(int, list(input())))
	n = len(st)
	count = 0
	for i in st:
		if i == 1:
			count += 1
	if count == 0:
		print(0)
	elif count == 1:
		print(a)
	else:
		for i in range(n):
			if st[i] == 1:
				break
		zer = 0
		ans = a
		for j in range(i + 1, n):
			if st[j] == 0:
				zer += 1
			elif zer != 0:
				ans += min(zer * b, a)
				zer = 0
		print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip().strip('0')
	n = len(s)
	if n == 0:
		print(0)
		continue
	ans = a
	now = 0
	for i in s:
		if i == '0':
			now += 1
		if i == '1' and now:
			ans += min(a, b * now)
			now = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	res = []
	i = 0
	n = len(s)
	ans = 0
	while i < n:
		if s[i] == '1':
			temp = [i]
			while i < n and s[i] == '1':
				i += 1
			temp.append(i - 1)
			if len(res):
				if b * (temp[0] - res[-1][1] - 1) + a <= 2 * a:
					ans += b * (temp[0] - res[-1][-1] - 1)
					res[-1][-1] = temp[1]
				else:
					res.append(temp)
			else:
				res.append(temp)
		else:
			i += 1
	print(len(res) * a + ans)


def solution():
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	i = 0
	j = n - 1
	while i < n and s[i] == '0':
		i += 1
	while j >= 0 and s[j] == '0':
		j -= 1
	l = []
	cnt = 0
	while i <= j:
		if s[i] == '0':
			cnt += 1
		else:
			if cnt > 0:
				l.append(cnt)
			cnt = 0
		i += 1
	l.sort()
	if len(l) == 0:
		if '1' in s:
			print(a)
		else:
			print(0)
		return
	ans = a * (len(l) + 1)
	cnt = 0
	for i in range(len(l)):
		if 2 * a > l[i] * b + a:
			x = ans - a + l[i] * b
			ans = min(ans, x)
	print(ans)
t = int(input())
for _ in range(t):
	solution()


def miner(a, b, mines):
	s = 0
	j = 0
	while j < len(mines) and mines[j] != '1':
		j += 1
	if j == len(mines):
		return 0
	for i in range(j + 1, len(mines)):
		if mines[i] == '0' and mines[i - 1] == '1':
			r1 = i - 1
		elif mines[i] == '1' and mines[i - 1] == '0':
			if b * (i - r1 - 1) <= a:
				s += b * (i - r1 - 1)
			else:
				s += a
	s += a
	return s
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	mines = input()
	print(miner(a, b, mines))


import itertools

def solve():
	[a, b] = [int(x) for x in input().strip().split()]
	mines = list(input().strip())
	(cost, prev) = (0, 0)
	for (index, (value, count)) in enumerate(itertools.groupby(mines)):
		count = len(list(count))
		if index == 0:
			if value == '1':
				cost += a
			else:
				prev = count
			continue
		if value == '0':
			prev = count
			continue
		if index > 1:
			cost += min(a, prev * b)
		else:
			cost += a
	return cost
for _ in range(int(input().strip())):
	print(solve())


from sys import stdin

def si():
	return stdin.readline().strip()

def ii():
	return int(si())

def mi():
	return map(int, si().split())

def li():
	return list(mi())
t = ii()
for _ in range(t):
	(a, b) = mi()
	s = si()
	count = float('inf')
	res = 0
	for i in range(len(s)):
		if s[i] == '0':
			count += 1
		else:
			res += min(a, b * count)
			count = 0
	print(res)


T = int(input())
for _ in range(T):
	(A, B) = map(int, input().split())
	s = input()
	N = len(s)
	c = [1]
	M = int('1' in s)
	for i in range(1, N):
		if s[i - 1] != s[i]:
			c.append(1)
		else:
			c[-1] += 1
	if s[0] == '0' and len(c):
		del c[0]
	if s[-1] == '0' and len(c):
		del c[-1]
	c = c[1::2]
	S = [min(c[i] * B, A) for i in range(len(c))]
	print(M * A + sum(S))


t = int(input())
for j in range(0, t):
	l1 = [int(x) for x in input().split()]
	a = l1[0]
	b = l1[1]
	bomb = []
	city = input()
	for i in range(0, len(city)):
		if city[i] == '1':
			bomb.append(i)
	out = 0
	for i in range(0, len(bomb) - 1):
		out += min(a, (bomb[i + 1] - bomb[i] - 1) * b)
	if len(bomb) > 0:
		out += a
	print(out)


from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s) - 1
	i = 0
	arr = []
	while i < n:
		if s[i] == '0':
			i = i + 1
			continue
		j = i
		while j < n:
			if s[j] == '0':
				break
			j = j + 1
		arr.append((i, j - 1))
		i = j
	l = len(arr)
	res = 0
	i = 0
	while i < l:
		ans = a
		j = i + 1
		while j < l:
			if b * (arr[j][0] - arr[j - 1][1] - 1) > a:
				break
			ans = ans + b * (arr[j][0] - arr[j - 1][1] - 1)
			j = j + 1
		i = j
		res = res + ans
	print(res)


t = int(input())
for z in range(t):
	(a, b) = map(int, input().split())
	s = input()
	c0 = 0
	c1 = 0
	for i in range(len(s)):
		if s[i] == '1':
			c1 += 1
		else:
			c0 += 1
	if c0 == 0:
		print(a)
	elif c0 == len(s):
		print(0)
	else:
		i = 0
		while i < len(s):
			if s[i] == '1':
				break
			i += 1
		j = len(s) - 1
		l = []
		while j >= 0:
			if s[j] == '1':
				break
			j -= 1
		f = 0
		for k in range(i, j + 1):
			if s[k] == '0':
				f = 0
				l[len(l) - 1] += 1
			else:
				if f == 0:
					l.append(0)
				f = 1
		ans = 0
		c = 0
		for j in range(len(l) - 1):
			u = min(l[j] * b + a, 2 * a)
			ans += min(l[j] * b, a)
		print(ans + a)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	c = 0
	if s.count('1') == 0:
		print(0)
		continue
	x = s.index('1')
	ans = a
	for i in s[x + 1:]:
		if i == '1':
			ans += min(c * b, a)
			c = 0
		else:
			c += 1
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	x = input()
	if x.count('1') == 0:
		print(0)
	else:
		l = x.split('1')
		l = l[1:-1]
		y = [len(i) for i in l if len(i) > 0]
		cost = []
		for j in y:
			if b * j < a:
				cost.append(b * j)
			else:
				cost.append(a)
		print(a + sum(cost))


import sys, threading, os.path
import string
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('in.rtf'):
		input = open('in.rtf', 'r')
	else:
		input = sys.stdin
	t = int(input.readline())
	results = []
	for i in range(t):
		(a, b) = list(map(int, input.readline().split()))
		builds = str(input.readline().rstrip('\n'))
		(sum1, last1) = (0, -1)
		for j in range(len(builds)):
			if builds[j] == '1':
				if last1 == -1:
					last1 = j
					sum1 += a
				elif j - last1 == 1:
					last1 = j
				else:
					sum1 += min(a, (j - last1 - 1) * b)
					last1 = j
			else:
				continue
		results.append(sum1)
	output = '\n'.join(map(str, results))
	if os.path.exists('out.rtf'):
		open('out.rtf', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		(a, b) = In()
		s = input()
		noz = 0
		last = -1
		ans = 0
		for x in range(len(s)):
			if last == -1:
				if s[x] == '1':
					last = x
					noz = 0
			elif s[x] == '0':
				noz += 1
			else:
				ans += min(a, noz * b)
				noz = 0
		if last != -1:
			ans += a
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(a, b) = list(map(int, input().split()))
		mines = input()
		currCost = 0
		currZero = 0
		for mine in mines:
			if mine == '1':
				if currZero == 0:
					if currCost == 0:
						currCost += a
				elif a >= b * currZero:
					currCost += b * currZero
				else:
					currCost += a
				currZero = 0
			elif currCost != 0:
				currZero += 1
		yield currCost
t = int(input())
ans = gift()
print(*ans, sep='\n')


import sys
num_tests = int(sys.stdin.readline())
for _ in range(num_tests):
	(trigger_cost, mine_cost) = map(int, sys.stdin.readline().strip().split())
	mines = sys.stdin.readline().strip()
	mines = mines.strip('0')
	gaps = []
	l = 0
	while l < len(mines):
		if mines[l] == '1':
			l += 1
			continue
		r = l + 1
		while r < len(mines) and mines[r] == '0':
			r += 1
		gaps.append(r - l)
		l = r
	total_cost = 0
	if not gaps:
		if mines:
			total_cost = trigger_cost
	else:
		total_cost = trigger_cost
		for g in gaps:
			total_cost += min(g * mine_cost, trigger_cost)
	print(total_cost)


def solve(a: int, b: int, mines: str):
	mines += '0'
	mine_fields = []
	begin_mine_segment = -1
	pos = 0
	while pos < len(mines):
		if mines[pos] == '1':
			if begin_mine_segment == -1:
				begin_mine_segment = pos
		elif begin_mine_segment != -1:
			mine_fields.append((begin_mine_segment, pos - 1))
			begin_mine_segment = -1
		pos += 1
	if not mine_fields:
		return 0
	last_mine_field = len(mine_fields) - 1
	costs = a
	while last_mine_field > 0:
		dist_to_last_mf = mine_fields[last_mine_field][0] - mine_fields[last_mine_field - 1][1] - 1
		costs += min(dist_to_last_mf * b, a)
		last_mine_field -= 1
	return costs

def main():
	ts = int(input())
	for t in range(ts):
		(a, b) = input().split(' ')
		mines = input()
		print(solve(int(a), int(b), mines))
main()


import sys
t = int(sys.stdin.readline().strip())
for j in range(t):
	(a, b) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline().strip('0')
	dp = []
	last = 0
	zC = 0
	if '1' not in s:
		print(0)
		continue
	for i in range(len(s)):
		if s[i] == '1' and last == 0:
			if i != 0:
				dp.append(zC)
			zC = 0
			last = 1
		if s[i] == '0':
			last = 0
			zC += 1
	total = a
	for i in range(len(dp)):
		total += min(a, dp[i] * b)
	print(total)


T = int(input())
for t in range(T):
	(a, b) = map(int, input().split())
	s = input()
	i = 0
	tot = a
	n = len(s)
	while i < n and s[i] == '0':
		i += 1
	if i == n:
		tot = 0
	while i < n:
		k = 0
		while i < n and s[i] == '0':
			k += 1
			i += 1
		if i != n:
			tot += min(k * b, a)
		i += 1
	print(tot)


for _ in range(int(input())):
	(blow, place) = map(int, input().split())
	c = input()
	enable = False
	op = 0
	for i in c:
		if i == '1':
			if not enable:
				enable = True
				op += 1
		if i == '0':
			enable = False
	base_cost = blow * op
	start = False
	zero_arr = []
	count = None
	for i in c:
		if i == '1':
			if start:
				if count != 0:
					zero_arr.append(count)
					count = 0
			else:
				start = True
				count = 0
		elif start:
			count += 1
	print(base_cost - sum((blow - i * place for i in zero_arr if i <= blow // place)))


def main():
	(a, b) = map(int, input().split())
	s = input()
	l = []
	n = len(s)
	prev = -1
	for i in range(n):
		if s[i] == '1':
			if prev != -1 and prev != i - 1:
				l.append(i - prev - 1)
			prev = i
	l.sort()
	if prev == -1:
		print(0)
		return
	ans = (len(l) + 1) * a
	q = 0
	for i in range(len(l)):
		q += l[i]
		ans = min(ans, (len(l) - i) * a + b * q)
	print(ans)
for _ in range(int(input())):
	main()


import math
import sys, bisect
from heapq import *
from itertools import *
from collections import *
sys.setrecursionlimit(10 ** 6)
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	(a, b) = neo()
	s = input()
	s = s.lstrip('0')
	s = s.rstrip('0')
	A = [len(list(j)) for (_, j) in groupby(list(s))]
	if len(s) == 0:
		print(0)
		continue
	B = [s[0]]
	for i in s[1:]:
		if B[-1] != i:
			B += [i]
	Ans = 0
	(n, i) = (len(A), 0)
	f = 0
	while i < n:
		if B[i] == '0':
			if A[i] * b < a:
				Ans += A[i] * b
				Ans -= 2 * a
				f += 1
		else:
			Ans += a
		i += 1
	Ans += f * a
	print(Ans)


import sys, os
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 10 ** 9 + 7

def main():
	for _ in range(int(input())):
		(a, b) = read()
		s = input().strip().strip('0')
		t1 = 0
		for i in s.split('0'):
			if i:
				t1 += 1
		t2 = [len(i) for i in s.split('1') if i]
		ans = 0
		i = 0
		for i in range(len(t2)):
			if t2[i] * b + a < 2 * a:
				ans += t2[i] * b
				t1 -= 1
		print(ans + t1 * a)
main()


for _ in range(int(input())):
	(act, pl) = map(int, input().split())
	m = input().strip('0')
	le = len(m)
	if not le:
		print(0)
		continue
	act_cost = 0
	pl_cost = 0
	new = True
	i = 0
	while i < le:
		if new:
			act_cost += act
			new = False
		while i < le and m[i] == '1':
			i += 1
		while i < le and m[i] == '0':
			pl_cost += pl
			i += 1
		if pl_cost and pl_cost < act:
			act_cost += pl_cost
		else:
			new = True
		pl_cost = 0
	print(act_cost)


def solve():
	(a, b) = map(int, input().split())
	s = list(input())
	n = len(s)
	c = 0
	i = 0
	while i < n:
		if s[i] == '1':
			j = i - 1
			while j >= 0 and s[j] != '1':
				j -= 1
			if j >= 0:
				k = i - j - 1
				c += min(a, b * k)
			else:
				c += a
			while i < n and s[i] == '1':
				i += 1
			continue
		i += 1
	return c
t = int(input())
i = 0
while i < t:
	print(solve())
	i += 1


for t in range(int(input())):
	(a, b) = map(int, input().split())
	arr = [int(x) for x in input()]
	mines = []
	flag = False
	count = 0
	start = 0
	for i in range(len(arr)):
		if flag == False:
			if arr[i] == 1:
				start = i
				flag = True
		elif arr[i] == 1:
			count += 1
		else:
			mines.append([start, start + count])
			count = 0
			flag = False
	if flag == True:
		mines.append([start, start + count])
	count = 0
	new = 0
	i = 0
	n = len(mines)
	while i < n - 1:
		new_mines = mines[i + 1][0] - mines[i][1] - 1
		if new_mines * b < a:
			count += 1
			new += new_mines
		i += 1
	cost = (n - count) * a + new * b
	print(cost)


import math
import collections
t = int(input())
for w in range(t):
	(a, b) = (int(i) for i in input().split())
	l = list(input())
	if '1' not in l:
		print(0)
	else:
		k = l.index('1')
		l = l[k:]
		c1 = 0
		c = 1
		s = 0
		l1 = [0]
		for i in range(1, len(l)):
			if l[i] != l[i - 1]:
				c1 += 1
				if c1 % 2 == 0:
					c += 1
			if l[i] == '1':
				l1.append(i)
		if len(l1) == 1:
			print(a)
		else:
			for i in range(1, len(l1)):
				if l1[i] - l1[i - 1] > 1:
					if (l1[i] - l1[i - 1] - 1) * b + a < 2 * a:
						c -= 1
						s += (l1[i] - l1[i - 1] - 1) * b
			print(s + c * a)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	q = 0
	gaps = []
	k = 0
	while k < n and s[k] == '0':
		k += 1
	while k < n:
		q += 1
		while k < n and s[k] == '1':
			k += 1
		g = 0
		while k < n and s[k] == '0':
			g += 1
			k += 1
		if k < n:
			gaps.append(g)
	gaps.sort()
	res = 0
	l = a // b
	for x in gaps:
		if x > l:
			break
		res += x * b
		q -= 1
	res += q * a
	print(res)


import math
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	ar = list(map(int, input()))
	n = len(ar)
	(f, l) = (-1, -1)
	for i in range(n):
		if ar[i] == 1:
			f = i
			break
	for i in range(n):
		if ar[n - 1 - i] == 1:
			l = n - 1 - i
			break
	if f == l and l == -1:
		print(0)
	else:
		(ans, i) = (a, 0)
		ar = ar[f:l + 1]
		n = len(ar)
		while i < n:
			z = 0
			while ar[i] == 0:
				z += 1
				i += 1
			ans += min(b * z, a)
			if z == 0:
				i += 1
		print(ans)


for t in range(int(input())):
	(a, b) = list(map(int, input().split()))
	m = input()
	x = []
	i = 0
	while i < len(m) and m[i] == '0':
		i += 1
	cs = 0
	while i < len(m):
		if m[i] == '0':
			cs += 1
		if m[i] == '1' and cs != 0:
			x += [cs]
			cs = 0
		i += 1
	cp = (len(x) + 1) * a
	for i in x:
		if i * b < a:
			cp -= a
			cp += i * b
	if m == '0' * len(m):
		print(0)
	else:
		print(cp)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	c = float('inf')
	ans = 0
	for i in range(len(s)):
		if s[i] == '0':
			c += 1
		else:
			ans += min(a, b * c)
			c = 0
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil, floor, sqrt
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def solve():
	(a, b) = map1()
	l1 = list(input().strip('0'))
	n = len(l1)
	l2 = []
	(c, flag, sum1) = (0, 0, 0)
	for i in range(n):
		if l1[i] == '0':
			c = c + 1
		else:
			if flag == 0:
				v = c * b + a
				v1 = 2 * a
				flag = 1
			else:
				v = c * b
				v1 = a
			sum1 += min(v, v1)
			c = 0
	print(sum1)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip(' 0').rstrip(' 0')
	ans = 0
	l = s.split('1')
	for i in range(len(l)):
		t = len(l[i])
		if t > 0:
			ans += min(a, t * b)
	if '1' in s:
		ans += a
	print(ans)


import sys
input = sys.stdin.readline
tests = int(input())
for test in range(tests):
	(activatingCost, placingCost) = map(int, input().split())
	cityMap = input()
	cityMap = cityMap.strip('\n0')
	if cityMap == '':
		print(0)
	else:
		activations = 1
		placings = 0
		lastLot = '1'
		emptylots = 0
		for lot in cityMap:
			if lastLot == '1':
				if lot == '0':
					emptylots = 1
					lastLot = '0'
			elif lot == '0':
				emptylots += 1
			else:
				if placingCost * emptylots < activatingCost:
					placings += emptylots
				else:
					activations += 1
				lastLot = '1'
				emptylots = 0
		print(activations * activatingCost + placings * placingCost)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	j = 0
	n = len(s)
	l = []
	while j < n:
		if s[j] == '1':
			x = j
			while s[j] == '1':
				j += 1
				if j == n:
					break
			y = j - 1
			l.append([x, y])
		else:
			j += 1
	ans = 0
	j = 0
	while j < len(l):
		if j == 0:
			ans += a
		else:
			ans += min(a, b * (l[j][0] - l[j - 1][1] - 1))
		j += 1
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if not '1' in s:
		print(0)
		continue
	(res, ind, z, c) = (a, s.index('1'), False, 0)
	for i in s[ind:]:
		if i == '1' and z:
			res += min(c * b, a)
			z = False
			c = 0
		if i == '0':
			z = True
			c += 1
	print(res)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	cnt = 1000000000.0
	ans = 0
	for i in s:
		if i == '0':
			cnt += 1
		else:
			ans += min(a, cnt * b)
			cnt = 0
	print(ans)


mod = 10 ** 9 + 7

def solve():
	(a, b) = map(int, input().split())
	s = input()
	v = []
	tmp = 0
	ok = False
	for i in range(len(s)):
		if s[i] == '1':
			if tmp > 0:
				v.append(tmp)
			tmp = 0
			ok = True
		elif ok:
			tmp += 1
	v.sort()
	ans = a * (len(v) + 1)
	if not ok:
		ans = 0
	for i in range(len(v)):
		if ans >= ans - a + b * v[i]:
			ans = ans - a + b * v[i]
		else:
			break
	print(ans)
t = 1
t = int(input())
while t > 0:
	solve()
	t -= 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = input()
	c = 2 ** 32
	ans = 0
	for i in l:
		if i == '1':
			ma = min(n, c * m)
			ans += ma
			c = 0
		else:
			c += 1
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	ans = len(s) and a + sum((min(a, b * len(x)) for x in s.split('1')))
	print(ans)


for _ in range(int(input())):
	(c1, c2) = [int(i) for i in input().split()]
	a = [int(i) for i in input()]
	n = len(a)
	have = []
	i = 0
	last = -1
	while i < n:
		if a[i] == 0:
			i += 1
		else:
			st = i
			i += 1
			while i < n and a[i]:
				i += 1
			if last == -1:
				have.append(0)
			else:
				have.append(st - last)
			last = i
	ans = c1 * (len(have) > 0)
	for i in range(1, len(have)):
		ans += min(have[i] * c2, c1)
	print(ans)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def solve():
	t = get_int_input()
	for _ in range(t):
		(a, b) = get_int_input_arr()
		st = get_input()
		curr_len = float('inf')
		res = 0
		for i in range(len(st)):
			if st[i] == '1':
				res += min(a, b * curr_len)
				curr_len = 0
			else:
				curr_len += 1
		cout << res << endl

def main():
	solve()
main()


for i in range(int(input())):
	(a, b) = map(int, input().split())
	n = list(input())
	(s, cnt) = (0, 10 ** 6)
	for j in n:
		if j == '0':
			cnt += 1
		else:
			s += min(cnt * b, a)
			cnt = 0
	print(s)


import math
t = int(input())
for _ in range(t):
	(a, b) = [int(i) for i in input().split()]
	s = [int(i) for i in input()]
	temp = -1
	lis = []
	flag = 0
	count = -2
	for i in range(len(s)):
		if s[i] == 0:
			count += 1
			if temp != -1:
				lis.append((temp, i - 1))
			temp = -1
			flag = 0
		elif flag == 0:
			temp = i
			flag = 1
	if temp != -1:
		lis.append((temp, len(s) - 1))
	ans = 0
	temp = a
	if len(lis) > 0:
		for i in range(len(lis) - 1):
			if (lis[i + 1][0] - lis[i][1] - 1) * b <= a:
				temp += (lis[i + 1][0] - lis[i][1] - 1) * b
			else:
				ans += temp
				temp = a
		ans += temp
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = str(input())
	try:
		fir = s.index('1')
	except:
		fir = 0
	n = len(s)
	s += '0'
	d = [0 for i in range(n)]
	(j, k) = (0, 0)
	f = s[0]
	if f == '1':
		d[0] = a
	else:
		k = 1
	for i in range(1, n):
		if i <= fir:
			k = 0
		if s[i] == '1':
			if s[i - 1] == '1':
				(d[i], k) = (d[i - 1], 0)
			elif k > 0 and d[i - 1] > 0:
				d[i] = min(d[i - 1] + a, k * b + d[i - 1])
				k = 0
			else:
				d[i] = d[i - 1] + a
		else:
			d[i] = d[i - 1]
			k += 1
		if k > 0 and d[i - 1] > 0 and (s[i + 1] == '1'):
			d[i] = min(d[i], k * b + d[i - 1])
	print(d[-1])


T = int(input())
for t in range(T):
	(a, b) = map(int, input().split())
	s = str(input())
	mn = 100001
	r = 0
	for i in s:
		if i == '1':
			r += min(a, b * mn)
			mn = 0
		else:
			mn += 1
	print(r)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	arr = list(input().strip())
	n = len(arr)
	s = a
	c = 0
	pos = n
	for i in range(n):
		if arr[i] == '1':
			pos = i
			break
	if pos == n:
		print(0)
	else:
		while pos < n:
			if arr[pos] == '0':
				c += 1
				pos += 1
				continue
			else:
				pos += 1
			s = min(s + c * b, s + a)
			c = 0
		print(s)


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def OPS(ans):
	sys.stdout.write(str(ans) + '\n')

def OPL(ans):
	[sys.stdout.write(str(_) + ' ') for _ in ans]
	sys.stdout.write('\n')

def main():
	try:
		for _ in range(inp()):
			(a, b) = invr()
			s = list(input())
			n = len(s)
			c = 0
			ans = 0
			f = -1
			for i in range(n):
				if s[i] == '1':
					if f == -1:
						f = 0
						c = 0
						ans += a
					else:
						ans += min(c * b, a)
						c = 0
				else:
					c += 1
			print(ans)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def read_generator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = read_generator()

def readword():
	return next(reader)

def readint():
	return int(next(reader))

def readfloat():
	return float(next(reader))

def readline():
	return input()

def solve(a, b, m):
	o = []
	new = True
	i = 0
	for c in m:
		if c == '1':
			if new:
				o.append([i, i])
				new = False
			else:
				o[len(o) - 1][1] = i
		else:
			new = True
		i += 1
	res = len(o) * a
	for i in range(1, len(o)):
		cur = o[i]
		prev = o[i - 1]
		if (cur[0] - prev[1] - 1) * b < a:
			res -= a
			res += (cur[0] - prev[1] - 1) * b
	return res
tests = readint()
for t in range(tests):
	(a, b) = (readint(), readint())
	m = readline()
	print(solve(a, b, m))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	m = input()
	flag = -1
	count = 0
	z = []
	cost = 0
	for x in range(len(m)):
		if m[x] == '1':
			if count > 0 and flag == 1:
				z.append(count)
				count = 0
			if flag == -1:
				cost += a
				flag = 1
		elif flag == 1:
			count += 1
	for x in z:
		if x * b < a:
			cost += x * b
		else:
			cost += a
	print(cost)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	cnt = 2 ** 32
	ans = 0
	for i in s:
		if i == '0':
			cnt += 1
		else:
			ans += min(a, b * cnt)
			cnt = 0
	print(ans)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	s = input()
	s = s + '0000'
	if '1' not in s:
		print(0)
	elif a < b:
		s = s.split('0')
		c = 0
		for i in s:
			if i != '':
				c = c + a
		print(c)
	else:
		f = 0
		c = 0
		for i in range(len(s)):
			if s[i] == '1':
				break
		s = s[i:]
		s = s.split('0')
		l = []
		f = 0
		g = 1
		for i in s:
			if i != '':
				if g == 1:
					c = c + a
					f = 0
					g = 0
				else:
					f = f + 1
					c = c + min(f * b, a)
					f = 0
			else:
				f = f + 1
		print(c)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	(x, y) = (-100000, a)
	for j in input():
		if j == '1':
			if x > 0:
				y += min(a, x * b)
			x = 0
		else:
			x += 1
	print(0 if x < 0 else y)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	s = s.strip('0')
	n = len(s)
	cost2 = 0
	prevMineIdx = -1
	addInBetween = float('inf')
	for i in range(n):
		if s[i] == '1':
			if i > 0:
				addInBetween = cost2 + b * (i - prevMineIdx - 1)
			if i == 0 or (i > 0 and s[i - 1] == '0'):
				dontAddInBetween = cost2 + a
			else:
				dontAddInBetween = cost2
			cost2 = min(addInBetween, dontAddInBetween)
			prevMineIdx = i
	print(cost2)


import math
t = int(input())
for tt in range(t):
	(a, b) = map(int, input().split())
	s = input().strip()
	n = len(s)
	j = n - 1
	i = 0
	while i < len(s) and s[i] == '0':
		i += 1
	while j >= i and s[j] == '0':
		j -= 1
		n -= 1
	ii = i
	ans = 0
	while ii < n:
		jj = ii
		while jj < n and s[ii] == s[jj]:
			jj += 1
		if s[ii] == '1':
			ans += a
		elif b * (jj - ii) <= a:
			ans += b * (jj - ii) - a
		ii = jj
	print(ans)


z = int(input())
W = []
for p in range(z):
	(a, b) = map(int, input().split())
	A = input()
	B = []
	i = 0
	while i < len(A):
		k1 = 0
		k2 = 0
		if A[i] == '0':
			while i < len(A) and A[i] == '0':
				k1 += 1
				i += 1
			B.append(str(k1))
		else:
			while i < len(A) and A[i] == '1':
				k2 += 1
				i += 1
			B.append(k2 * a)
	t = 0
	if len(B) == 1 and type(B[0]) == str:
		V = 0
	elif len(B) == 1 and type(B[0]) == int:
		V = a
	elif type(B[0]) == str:
		t += 1
		V = a
	else:
		V = a
	for j in range(t + 1, len(B) - 1):
		if type(B[j]) == str:
			n = int(B[j])
			if n < a / b:
				V += b * n
			else:
				V += a
	W.append(V)
for l in range(len(W)):
	print(W[l])


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	i = 0
	ans2 = 0
	while i < n and s[i] != '1':
		i += 1
	zeros = 0
	ans = 0
	flag = 0
	while i < n:
		if s[i] == '0':
			zeros += 1
			i += 1
		else:
			ones = 0
			while i < n and s[i] == '1':
				flag = 1
				i += 1
				ones += 1
			t1 = a
			t2 = 100000000
			if zeros != 0:
				t2 = b * zeros
			ans = ans + min(t1, t2)
			zeros = 0
	if flag == 0:
		print(ans2)
	else:
		print(ans)


def func(s, a, b):
	count = check = cost = 0
	s = s.strip('0')
	l = len(s)
	for i in range(l):
		if s[i] == '0':
			count += 1
			check = 1
		elif check == 1:
			if count * b < a:
				cost += count * b
			else:
				cost += a
			count = check = 0
	return cost + a
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	if int(s) == 0:
		print(0)
	else:
		print(func(s, a, b))


T = int(input())
for _ in range(T):
	(a, b) = map(int, input().split())
	s = input()
	c = 0
	if s.count('1') == 0:
		print(0)
		continue
	ans = 0
	f = 0
	x = 0
	for i in s:
		if i == '0' and f:
			c += 1
		elif i == '1':
			f = 1
			if x == 0:
				ans += min(c * b + a, 2 * a)
				x = 1
			else:
				ans += min(c * b, a)
			c = 0
	print(ans)


from collections import deque
t = int(input())
for case in range(t):
	(a, b) = (int(x) for x in input().split())
	s = input()
	(n, ln, runs) = (len(s), 1, deque())
	for j in range(1, len(s)):
		if s[j] != s[j - 1]:
			runs.append((s[j - 1], ln))
			ln = 1
		else:
			ln += 1
	runs.append((s[-1], ln))
	if runs and runs[0][0] == '0':
		runs.popleft()
	if runs and runs[-1][0] == '0':
		runs.pop()
	(co, vals) = (0, [])
	while runs:
		x = runs.popleft()
		if x[0] == '1':
			co += 1
		elif x[0] == '0' and b * x[1] < a:
			vals.append(b * x[1])
	print(a * (co - len(vals)) + sum(vals))


t = int(input())
for z in range(t):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	i = 0
	j = len(s) - 1
	ans = 0
	if len(set(s)) == 1:
		if '0' in set(s):
			ans = 0
		else:
			ans = a
	else:
		while s[i] == '0':
			i += 1
		while s[j] == '0':
			j -= 1
		ans = a
		while i < j:
			if s[i] == '1':
				while i < j and s[i] == '1':
					i += 1
			if s[i] == '0':
				c = 0
				while i < j and s[i] == '0':
					i += 1
					c += 1
				ans = min(ans + c * b, ans + a)
				c += 1
				i += 1
	print(ans)


q = int(input())
for i in range(q):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	l = 0
	r = n - 1
	for i in range(n):
		if s[i] == '0':
			l += 1
		else:
			break
	for i in range(n):
		if s[n - 1 - i] == '0':
			r -= 1
		else:
			break
	s = s[l:r + 1]
	block = []
	bl = 0
	fl = True
	c = 0
	s_b = 0
	for i in range(r - l + 1):
		if s[i] == '1' and fl:
			fl = False
			c += 1
			if bl != 0 and bl * b < a:
				c -= 1
				s_b += bl * b
			bl = 0
		if s[i] == '0':
			bl += 1
			fl = True
	print(c * a + s_b)


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		M = list(input().rstrip())
		while M and M[0] == '0':
			M.pop(0)
		while M and M[-1] == '0':
			M.pop()
		n = len(M)
		cnt_a = 0
		B = []
		i = 0
		while i < n:
			if i < n and M[i] == '1':
				cnt_a += 1
				while i < n and M[i] == '1':
					i += 1
			if i < n and M[i] == '0':
				cnt = 0
				while i < n and M[i] == '0':
					cnt += 1
					i += 1
				i -= 1
				B.append(b * cnt)
			i += 1
		res = 0
		for cost_b in B:
			if a * 2 >= a + cost_b:
				cnt_a -= 1
				res += cost_b
		print(res + cnt_a * a)
resolve()


for i in range(int(input())):
	(a, b) = map(int, input().split())
	l = input()
	(c, ans) = (10 ** 9, 0)
	for _ in l:
		if _ == '0':
			c += 1
		else:
			ans += min(a, b * c)
			c = 0
	print(ans)


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	c = 0
	s = input()
	l = list(s.split('0'))
	w = list(s.split('1'))
	for j in l:
		if len(j) > 0:
			c += 1
	ans = c * a
	for j in w[1:len(w) - 1]:
		if len(j) > 0 and b * len(j) < a:
			ans += b * len(j)
			ans -= a
	print(ans)


T = int(input())
for _ in range(T):
	(a, b) = map(int, input().split())
	data = list(map(int, input()))
	minelen = 0
	vaccumlen = 0
	prev = 0
	cost = 0
	for i in range(len(data)):
		if data[i] == 0:
			if prev == 1:
				vaccumlen = 1
				prev = 0
			else:
				vaccumlen += 1
		elif prev == 0:
			if minelen > 0:
				cost += min(vaccumlen * b, a)
			minelen = 1
			prev = 1
		else:
			minelen += 1
	if minelen > 0:
		cost += a
	print(cost)


import sys
from copy import *
from bisect import *
from math import gcd, ceil, sqrt
from itertools import permutations as prm, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def string(s):
	return ''.join(s)

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	(a, b) = mi()
	s = si()
	n = len(s)
	dp = [[0] * 2 for i in range(n + 1)]
	dp[-1][1] = a
	for i in range(n):
		if s[i] == '0':
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1][1] + b, dp[i - 1][0] + a + b)
		else:
			dp[i][0] = 10 ** 18
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][0] + a)
	print(min(dp[n - 1]))


for test in range(int(input())):
	(a, b) = list(map(int, input().split()))
	houses = input()
	nulls = 0
	one = 0 if houses[0] == '0' else 1
	result = 0 if one == 0 else a
	for i in range(1, len(houses)):
		if houses[i] == '1' and one == 0:
			one = 1
			result += a
		elif houses[i] == '0' and houses[i - 1] == '1':
			nulls += 1
		elif houses[i] == '0' and nulls > 0:
			nulls += 1
		elif houses[i] == '1' and houses[i - 1] == '0':
			result += min(a, b * nulls)
			nulls = 0
	print(result)


from collections import defaultdict as dd
t = int(input())
for i in range(t):
	(a, b) = list(map(int, input().split()))
	mines = input()
	pointer = 0
	while pointer < len(mines):
		if mines[pointer] != '0':
			break
		pointer += 1
	land = False
	unify = 0
	if pointer < len(mines):
		cost = a
	else:
		cost = 0
	while pointer < len(mines):
		if mines[pointer] == '1':
			if land == False:
				land = True
				if unify < a:
					cost += unify
				else:
					cost += a
				unify = 0
		else:
			land = False
			unify += b
		pointer += 1
	print(cost)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	if '1' not in s:
		print('0')
	else:
		start = s.index('1')
		end = s.rindex('1')
		j = start
		l = []
		count = 0
		zero = 0
		while j <= end:
			if j == 0:
				count += 1
			elif s[j - 1] == '0' and s[j] == '1':
				count += 1
				l.append(zero)
				zero = 0
			elif s[j] == '0':
				zero += 1
			j += 1
		if b >= a:
			print(a * count)
		else:
			ans = a
			for j in range(len(l)):
				if b * l[j] < a:
					ans += b * l[j]
				else:
					ans += a
			print(ans)


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	s = list(input())
	while s and s[-1] == '0':
		s.pop()
	if '1' in s:
		s = s[s.index('1'):]
	if len(s) == 0:
		print(0)
	else:
		ans = a
		n0 = 0
		for i in s:
			if i == '0':
				n0 += 1
			if i == '1' and n0 > 0:
				ans += min(a, b * n0)
				n0 = 0
		print(ans)


test_cases = int(input())
for i in range(test_cases):
	costs = input().split()
	cost_a = int(costs[0])
	cost_b = int(costs[1])
	string = input()
	first_1 = False
	cost = 0
	count_0 = 0
	for j in string:
		if j == '1' and first_1 == False:
			cost += cost_a
			first_1 = True
		if j == '0' and first_1 == True:
			count_0 += 1
		if j == '1' and first_1 == True:
			if count_0 * cost_b > cost_a:
				cost += cost_a
			else:
				cost += count_0 * cost_b
			count_0 = 0
	print(cost)


import math
from collections import *
from functools import lru_cache
import bisect
from itertools import *
from heapq import *
inf = float('inf')
ninf = -float('inf')
ip = input
alpha = 'abcdefghijklmnopqrstuvwxyz'

def ipl():
	return list(map(int, input().split()))

def ipn():
	return int(input())

def ipf():
	return float(input())

def solve():
	(a, b) = ipl()
	s = ip()
	n = len(s)
	c = i = 0
	pj = -1
	while i < n:
		if s[i] == '1':
			j = i
			while j < n and s[j] == '1':
				j += 1
			if pj == -1:
				c += a
			elif b * (i - pj) <= a:
				c += b * (i - pj)
			else:
				c += a
			pj = j
			i = j
		else:
			i += 1
	print(c)
for _ in range(int(input())):
	solve()


test_case = int(input())
for i in range(test_case):
	cost = 0
	(a, b) = map(int, input().split(' '))
	m = input()
	previous_index = -1
	for i in range(len(m)):
		if m[i] == '1':
			if previous_index == -1:
				previous_index = i
			elif i - previous_index == 1:
				previous_index = i
			elif (i - previous_index - 1) * b > a:
				cost += a
				previous_index = i
			else:
				cost += (i - previous_index - 1) * b
				previous_index = i
	if previous_index != -1:
		cost += a
	print(cost)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def endline():
	return input()

def isPrime(num):
	if num > 1:
		for i in range(2, num):
			if num % i == 0:
				return False
		return True
	else:
		return False

def main():
	t = inp()
	while t > 0:
		t -= 1
		(a, b) = invr()
		s = insr()
		l = [[0 for _i in range(2)] for _j in range(len(s))]
		if len(s) == 1:
			answer = int(s[0]) * a
			print(answer)
		else:
			l[0][1] = int(s[0]) * a
			l[0][0] = 10000
			for i in range(1, len(s)):
				if s[i] == '1':
					if s[i - 1] == '1':
						l[i][1] = l[i - 1][1]
					else:
						l[i][1] = min(l[i - 1][1] + a, l[i - 1][0])
				elif s[i - 1] == '1':
					l[i][0] = l[i - 1][1] + b
					l[i][1] = l[i - 1][1]
				else:
					l[i][0] = l[i - 1][0] + b
					l[i][1] = l[i - 1][1]
			answer = l[len(s) - 1][1]
			print(answer)
input = sys.stdin.readline
main()


import math
test = int(input())

def testCase():
	(a, b) = map(int, input().split())
	city = input()
	n = len(city)
	count = int(100000.0)
	ans = 0
	for i in range(n):
		if city[i] == '0':
			count += 1
		else:
			ans += min(count * b, a)
			count = 0
	print(ans)
while test:
	test -= 1
	testCase()


for _ in range(int(input())):
	(activate, place) = map(int, input().split())
	S = input()
	n = len(S)
	if S.count('0') == n:
		print(0)
	else:
		front = 0
		while S[front] != '1':
			front += 1
		back = n - 1
		while S[back] != '1':
			back -= 1
		S = S[front:back + 1]
		Bins = []
		flag = True
		for i in range(len(S)):
			if S[i] == '0' and flag:
				flag = False
				O = i
			elif S[i] == '1' and (not flag):
				flag = True
				Bins.append(i - O)
		if Bins:
			cost = (len(Bins) + 1) * activate
			for i in range(len(Bins)):
				if place * Bins[i] < activate:
					cost -= activate - place * Bins[i]
			print(cost)
		else:
			print(activate)


t = int(input())
for t1 in range(0, t):
	(a, b) = map(int, input().split())
	l = input()
	i = 0
	space = []
	n = len(l)
	while i < n:
		if l[i] == '1':
			j = i + 1
			while j < n and l[j] == '1':
				j += 1
			space.append((i, j - 1))
			i = j
		else:
			i += 1
	diff = []
	for i in range(1, len(space)):
		diff.append(space[i][0] - space[i - 1][1] - 1)
	ans = 0
	if '1' in l:
		ans = a
	for i in diff:
		if b * i <= a:
			ans += b * i
		else:
			ans += a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	cnt = 10 ** 12
	c = 0
	for i in s:
		if i == '0':
			cnt += 1
		else:
			c += min(a, b * cnt)
			cnt = 0
	print(c)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def YES():
	print('YES')

def NO():
	print('NO')

def Yes():
	print('Yes')

def No():
	print('No')
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		self.num_sets -= 1
		self.parent[a] = b
		self.size[b] += self.size[a]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def primesbelow(N):
	correction = N % 6 > 1
	N = {0: N, 1: N - 1, 2: N + 4, 3: N + 3, 4: N + 2, 5: N + 1}[N % 6]
	sieve = [True] * (N // 3)
	sieve[0] = False
	for i in range(int(N ** 0.5) // 3 + 1):
		if sieve[i]:
			k = 3 * i + 1 | 1
			sieve[k * k // 3::2 * k] = [False] * ((N // 6 - k * k // 6 - 1) // k + 1)
			sieve[(k * k + 4 * k - 2 * k * (i % 2)) // 3::2 * k] = [False] * ((N // 6 - (k * k + 4 * k - 2 * k * (i % 2)) // 6 - 1) // k + 1)
	return [2, 3] + [3 * i + 1 | 1 for i in range(1, N // 3 - correction) if sieve[i]]
p = primesbelow(5000)

def solve():
	(a, b) = sep()
	s = inp()
	if s == '0' * len(s):
		print(0)
		return
	n = len(s)
	for i in range(n):
		if s[i] == '1':
			f = i
			break
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			l = i
			break
	c = '1'
	co = 1
	zeroes = []
	ones = []
	for i in range(f + 1, l + 1):
		if s[i] == c:
			co += 1
			continue
		else:
			if c == '1':
				ones.append(co)
			else:
				zeroes.append(co)
			co = 1
			c = s[i]
	if c == '1':
		ones.append(co)
	else:
		zeroes.append(co)
	req = a // b
	cost = 0
	rem = 0
	for i in zeroes:
		if i <= req:
			cost += i * b
			rem += 1
	t = len(ones) - rem
	cost += t * a
	print(cost)
testcase(int(inp()))


from math import gcd
from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	(a, b) = map(int, input().split())
	s = input().split()[0]
	n = len(s)
	ind = -1
	for i in range(n):
		if s[i] == '1':
			ind = i
			break
	if ind == -1:
		ans = 0
	else:
		arr = []
		cou = 1
		for j in range(ind + 1, n):
			if s[j] == s[j - 1]:
				cou += 1
			else:
				arr.append(cou)
				cou = 1
		if s[-1] == '1':
			arr.append(cou)
		ans = 0
		arr.reverse()
		curr = a
		while len(arr) >= 3:
			a1 = arr.pop()
			b1 = arr.pop()
			a2 = arr.pop()
			if b1 * b <= a:
				ans += b1 * b + curr
				curr = 0
			else:
				ans += curr
				curr = a
			arr.append(1)
		ans += curr
	print(ans)
	t -= 1


n = int(input())
for j in range(n):
	(e, p) = map(int, input().split())
	m = input()
	if m.count('1') > 0:
		mine = []
		sec = 1
		pre = '1'
		cnt = 0
		for i in range(m.index('1'), len(m)):
			if m[i] == '0' and pre != m[i]:
				cnt = 1
				pre = m[i]
			elif m[i] == '1' and pre != m[i]:
				mine.append(cnt)
				cnt = 1
				pre = m[i]
				sec += 1
			elif m[i] == pre:
				cnt += 1
		blow = 1
		buy = 0
		for i in range(len(mine)):
			if mine[i] * p < e:
				buy += mine[i]
			else:
				blow += 1
		print(blow * e + buy * p)
	else:
		print(0)


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd, inf

def coun(a, p, n):
	c = 0
	for i in range(n):
		if a[i] & 1 << p:
			c += 1
	return c

def main():
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		s = input().strip()
		f = 0
		x = []
		an = 0
		(on, ze, on2) = (0, 0, 0)
		n = len(s)
		p = False
		if len(set(s)) == 1:
			if s[0] == '1':
				an = a
			else:
				an = 0
			p = True
			print(an)
		if not p:
			for i in range(s.index('1'), n):
				if s[i] == '0':
					f = 1
					ze += 1
				elif f == 1:
					if ze * b <= a:
						an += ze * b
					else:
						an += a
					ze = 0
					f = 0
			an += a
			print(an)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from collections import deque, defaultdict, namedtuple
import heapq
from math import sqrt, factorial, gcd, ceil, atan, pi
from itertools import permutations

def input():
	return sys.stdin.buffer.readline().decode('utf-8').strip()
import string
import operator
import random
from bisect import bisect_left, bisect_right
from functools import lru_cache, reduce
MOD = int(1000000000.0) + 7
INF = float('inf')

def solve():
	(a, b) = [int(x) for x in input().split()]
	s = [int(x) for x in input()]
	d = deque(s)
	while d and d[0] == 0:
		d.popleft()
	while d and d[-1] == 0:
		d.pop()
	if not d:
		print(0)
		return
	s = ''.join([str(x) for x in d])
	seg = [len(x) for x in s.split('1') if x]
	ans = a * (len(seg) + 1)
	for s in seg:
		if s * b < a:
			ans -= a
			ans += s * b
	print(ans)
T = 1
T = int(input())
for case in range(1, T + 1):
	ans = solve()


from math import *
from operator import *
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	l = list()
	(p, q) = (0, 0)
	s = '0' + s + '0'
	for i in range(len(s)):
		if s[i] == '1' and s[i - 1] == '0':
			p = i
		elif s[i] == '0' and s[i - 1] == '1':
			q = i - 1
			l.append([p, q])
	answer = 0
	f = False
	if len(l) == 0:
		print(0)
	else:
		for i in range(0, len(l) - 1):
			if (l[i + 1][0] - l[i][1] - 1) * b <= a:
				answer += (l[i + 1][0] - l[i][1] - 1) * b
				f = True
			elif f:
				f = False
				answer += a
			else:
				answer += a
		print(answer + a)


import sys
input = sys.stdin.readline

def so():
	(a, b) = map(int, input().split())
	s = input()
	if not '1' in s:
		return '0'
	dif = a // b
	ind = s.index('1')
	(c, z, res) = (0, False, a)
	for i in s[ind:]:
		if i == '1' and z:
			if c <= dif:
				res += c * b
			else:
				res += a
			c = 0
			z = False
		elif i == '0':
			z = True
			c += 1
	return res
t = int(input())
for _ in range(t):
	print(so())


def main():
	n = int(input())
	for _ in range(n):
		(cost_active, add_cost) = map(int, input().split())
		line = input().strip('0')
		charge_delta = list()
		charges = False
		delta = 0
		for i in range(1, len(line)):
			part = line[i - 1:i + 1]
			if not charges and (part[0] == '1' or part[1] == '1'):
				charges = True
			if part == '10':
				delta = 1
			elif part == '01' and delta:
				charge_delta.append(delta)
				delta = 0
			elif part == '00' and delta:
				delta += 1
		if len(line) == 1:
			charges = True if line[0] == '1' else False
		total = cost_active if charges else 0
		for item in charge_delta:
			if item * add_cost > cost_active:
				total += cost_active
			else:
				total += item * add_cost
		print(total)
main()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	pre = ''
	x = ''
	for i in s:
		if i == '1' and pre == i:
			continue
		x += i
		pre = i
	ans = x.count('1') * a
	zeros = x.strip('1').split('1')
	for i in zeros:
		if i:
			ans = min(ans, ans - a + len(i) * b)
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = list(map(int, input().split()))
	mapping = list(map(int, input()))
	(bl, flag, count, counts) = (mapping[0], mapping[0], 0, [])
	for num in mapping:
		if not flag:
			flag = bl = num
			continue
		if not num:
			count += 1
		elif not bl:
			counts.append(count)
			count = 0
		bl = num
	counts.sort()
	(cum_sum, cum_sums) = (0, [0])
	for count in counts:
		cum_sum += count
		cum_sums.append(cum_sum)
	amounts = list(map(lambda x: (len(cum_sums) - x[0]) * a + x[1] * b, enumerate(cum_sums)))
	print(min(amounts) if counts or flag else 0)


def solve(a, b):
	main = input()
	co = 0
	las = '1'
	s = ''
	flag = 0
	for i in main:
		if flag == 1:
			s += i
		if i == '1':
			flag = 1
			s += i
	if '1' in s:
		ans = a
	else:
		ans = 0
	for i in range(1, len(s)):
		if s[i] == '0':
			co += 1
			las = '0'
		else:
			if las == '0':
				if co * b < a:
					ans += co * b
				else:
					ans += a
			co = 0
			las = '1'
	print(ans)
for _ in range(int(input())):
	(u, v) = map(int, input().split())
	solve(u, v)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = str(input())
	count = []
	no = []
	o = 0
	z = 0
	(oo, zz) = (0, 0)
	for j in range(len(s)):
		if s[j] == '1':
			break
	for i in range(j, len(s)):
		if s[i] == '1':
			if zz == 1:
				no.append(0)
				count.append(z)
				zz = 0
			o += 1
			z = 0
			oo = 1
		else:
			if oo == 1:
				no.append(1)
				count.append(o)
				oo = 0
			z += 1
			o = 0
			zz = 1
	if o > 0:
		count.append(o)
		no.append(1)
	i = 1
	ans = 0
	d = -1
	while i < len(count) - 1:
		if count[i] * b <= a:
			d = 0
			ans += count[i] * b
			i += 2
		else:
			ans += a
			d = 1
			i += 2
	if d == -1:
		if len(count) == 0:
			print(0)
		else:
			print(a)
	elif i - 2 == d:
		print(ans + a)
	elif i == len(count):
		print(a + ans)
	else:
		print(a + ans)


import sys
import itertools as it
import math as mt
import bisect as bi
import collections as cc
import itertools as it
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(a, b) = I()
	s = input().strip()
	if '1' not in s:
		print(0)
		continue
	x = s.find('1')
	y = s.rfind('1')
	s = s[x:y + 1]
	cnt = 0
	tot = s.count('1')
	if tot == 0:
		print(0)
		continue
	else:
		n = len(s)
		cnt = []
		c = 1
		st = s[0]
		for i in range(1, n):
			if s[i] == st:
				c += 1
			else:
				cnt.append(c)
				c = 1
				st = s[i]
		if c:
			cnt.append(c)
		nn = len(cnt)
		if nn == 1:
			ans = a
		else:
			ans = min(2 * a, a + cnt[1] * b)
			for i in range(3, nn, 2):
				ans += min(cnt[i] * b, a)
		print(ans)


def solve():
	t = inp()
	for j in range(t):
		(a, b) = mul()
		l = strl()
		countzero = 0
		countone = 0
		batchone = 0
		batchzero = []
		if '1' in l:
			for j in range(l.index('1'), len(l)):
				if l[j] == '0':
					if countzero == 0:
						batchone += 1
					countzero += 1
				else:
					if countzero != 0:
						batchzero.append(countzero)
					countzero = 0
			total = a
			for j in batchzero:
				if j * b < a:
					total += j * b
				else:
					total += a
			print(total)
		else:
			print(0)
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
solve()


for _ in range(int(input())):
	(m, k) = map(int, input().split())
	s = input()
	st = [int(c) for c in s]
	l = 0
	while l < len(st) and st[l] == 0:
		l += 1
	safe = 0
	ans = m if l < len(st) else 0
	while l < len(st):
		safe += st[l] == 0
		if st[l] == 1 and safe != 0:
			ans += min(m, k * safe)
			safe = 0
		l += 1
	print(ans)


for test in range(int(input())):
	cost = list(map(int, input().split()))
	blast = cost[0]
	place = cost[1]
	seq = input()
	i = 0
	seq_t = ''
	while i < len(seq):
		if seq[i] == '0':
			seq_t += '0'
		elif i == len(seq) - 1:
			seq_t += '1'
		elif seq[i + 1] != '1':
			seq_t += '1'
		i += 1
	ind1 = seq.find('1')
	if ind1 == -1:
		print(0)
	else:
		seq_t = seq_t[ind1:]
		cnt_zero = 0
		ans = blast
		for i in range(1, len(seq_t)):
			if seq_t[i] == '0':
				cnt_zero += 1
			else:
				ans = min(ans + blast, ans + cnt_zero * place)
				cnt_zero = 0
		print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	arr = []
	city = input()
	for char in city:
		arr.append(int(char))
	dp = [0 for i in range(len(arr))]
	between_zero = 0
	one_shown = 0
	for i in range(len(arr)):
		if arr[i] == 1:
			dp[i] += a
			if i >= 1 and arr[i - 1] == 1:
				dp[i] = dp[i - 1]
			elif one_shown == 0:
				dp[i] = a
			else:
				dp[i] = min(dp[i - 1] + between_zero * b, dp[i - 1] + a)
			between_zero = 0
			one_shown += 1
		else:
			if i >= 1:
				dp[i] = dp[i - 1]
			between_zero += 1
	print(dp[len(arr) - 1])


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	str = input()
	count = 0
	res = a
	oke = 1
	for i in range(len(str)):
		if str[i] == '0':
			count += 1
		else:
			if oke == 0:
				res += min(a, b * count)
			oke = 0
			count = 0
	if oke == 1:
		print(0)
	else:
		print(res)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().rstrip().strip('0') + '0'
	n = len(s)
	cnt = []
	now = 1
	res = 0
	for (i, x) in enumerate(s[:-1]):
		if s[i] == s[i + 1]:
			now += 1
		else:
			cnt.append(now)
			now = 1
	m = len(cnt)
	if m == 0:
		print(0)
		continue
	for x in range(1, m, 2):
		if cnt[x] * b <= a:
			res += cnt[x] * b
			res -= a
	print(res + a * (m + 1) // 2)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	str1 = input()
	if str1.find('1') > -1:
		ans = x
		cnt = 0
		p = str1.index('1')
	else:
		print(0)
		continue
	for i in range(p + 1, len(str1)):
		if str1[i] == '0':
			cnt += 1
		elif str1[i] == '1' and cnt == 0:
			pass
		elif str1[i] == '1':
			if cnt * y < x:
				ans += cnt * y
				cnt = 0
			else:
				ans += x
				cnt = 0
	print(ans)


from collections import deque
import sys

def input():
	return sys.stdin.readline().rstrip()

def solve():
	(a, b) = map(int, input().split())
	s = list(map(int, list(input())))
	s = deque(s)
	while s and s[0] == 0:
		s.popleft()
	while s and s[-1] == 0:
		s.pop()
	ans = 0
	s = list(s)
	if len(s) == 0:
		print(0)
		return
	cnt = 0
	minus = []
	command = -1
	for i in range(len(s)):
		cnt += 1
		command = s[i]
		if i == len(s) - 1:
			if cnt >= 1:
				if command == 1:
					ans += a
				else:
					minus.append(a - b * cnt)
			break
		if s[i] != s[i + 1]:
			if command == 1:
				ans += a
			elif command == 0:
				minus.append(a - b * cnt)
			cnt = 0
	for v in minus:
		if v > 0:
			ans -= v
	print(ans)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


def solve(costA, costB, city):
	gap = []
	totalGroups = 0
	findOne = False
	tempGap = 0
	for i in city:
		if i == '1':
			if not findOne:
				findOne = True
				if tempGap > 0:
					gap.append(tempGap)
					tempGap = 0
		else:
			if findOne:
				totalGroups += 1
				findOne = False
			if totalGroups > 0:
				tempGap += 1
	if findOne:
		totalGroups += 1
	totalGap = sum(gap)
	gap.sort(reverse=True)
	maximum = totalGap * costB + costA * min(1, totalGroups)
	activateIncrement = 1
	temp = 0
	for x in gap:
		totalGap -= x
		temp = totalGap * costB + costA * (1 + activateIncrement)
		if temp < maximum:
			maximum = temp
		activateIncrement += 1
	return maximum
for j in range(int(input())):
	(a, b) = list(map(int, input().split()))
	city = list(input())
	print(solve(a, b, city))


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	s = input()
	start = 0
	end = 0
	total = 0
	st = 0
	for i in range(len(s)):
		if st == 0 and s[i] == '1':
			start = i
			end = i
			total += a
			st = 1
		elif s[i] == '1':
			start = end
			end = i
			total += min((end - start - 1) * b, a)
	print(total)


t = int(input())
for tt in range(t):
	(a, b) = map(int, input().split())
	n = str(input())
	(ka, kb) = (1, 0)
	(g, s) = (0, 0)
	ss = 0
	for i in range(len(n)):
		if n[i] == '1':
			s = i + 1
			if g == 0:
				ss = i
				g = 1
	if g == 0:
		print(0)
		continue
	(g, f) = (0, 0)
	for i in range(ss, s):
		if n[i] == '0':
			f += 1
		elif f != 0 and n[i] == '1':
			if f * b < a:
				kb += f
			else:
				ka += 1
			f = 0
	print(ka * a + kb * b)


t = int(input())
while t:
	inp = input().strip().split()
	a = int(inp[0])
	b = int(inp[1])
	s = input().strip('0').split('0')
	s2 = []
	s3 = []
	for i in s:
		if i != '':
			s2.append(i)
	c = 1
	for i in s:
		if i != '':
			s3.append(c)
			c = 1
		else:
			c += 1
	s3.sort()
	s3 = s3[1:]
	ans0 = len(s2) * a
	ans = len(s2) * a
	for i in s3:
		if ans - a + i * b <= ans:
			ans = ans - a + i * b
		else:
			break
	print(min([ans, ans0]))
	t -= 1


I = input
for _ in [0] * int(I()):
	(a, b) = map(int, I().split())
	s = I().strip('0')
	print(len(s) and a + sum((min(a, b * len(x)) for x in s.split('1'))))


import sys
input = sys.stdin.buffer.readline
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().decode().rstrip('\n\r')
wrt = sys.stdout.write
pr = lambda *args, end='\n': wrt(' '.join([str(x) for x in args]) + end)
xrange = lambda args: reversed(range(args))
cdiv = lambda x, y: -(-x // y)
enum = enumerate
inf = float('inf')
mod = 10 ** 9 + 7
Cases = ri()
for Case in range(Cases):
	(a, b) = rl()
	s = rs()
	cc = 0
	o = 0
	for i in s:
		if i == '1':
			o = 1
		if i == '0':
			cc += o
			o = 0
	if o:
		cc += 1
	holes = []
	last = -1
	for (i, j) in enum(s):
		if j == '1':
			if last != -1:
				holes.append(i - last - 1)
				last = i
			else:
				last = i
	holes = [i for i in holes if i != 0]
	cost = cc * a
	for i in holes:
		if cost - a + i * b <= cost:
			cost = cost - a + i * b
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	if s == '':
		print(0)
	else:
		n = []
		c = 0
		for x in list(s):
			if x == '1':
				n.append(c)
				c = 0
			else:
				c += 1
		n.append(c)
		blow = 1
		m = 0
		for x in n:
			if x > a // b:
				blow += 1
			else:
				m += x
		print(blow * a + m * b)


for case in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	s = s.strip('0')
	k = a // b
	ct = 0
	i = 0
	n = len(s)
	while i < n:
		if i < n and s[i] == '1':
			ct += a
			while i < n and s[i] == '1':
				i += 1
		elif i < n and s[i] == '0':
			t = 0
			while i < n and s[i] == '0':
				i += 1
				t += 1
			if t <= k:
				ct -= a
				ct += b * t
	print(ct)


t = int(input())
for you in range(t):
	l = input().split()
	a = int(l[0])
	b = int(l[1])
	s = input()
	n = len(s)
	l = []
	start = 0
	end = 0
	done = 0
	for i in range(n):
		if done:
			if s[i] == '1':
				end += 1
			else:
				l.append((start, end))
				done = 0
		elif s[i] == '1':
			done = 1
			start = i
			end = i
	if done:
		l.append((start, end))
	z = a * len(l)
	lo = []
	for i in range(len(l) - 1):
		lo.append(l[i + 1][0] - l[i][1] - 1)
	for i in lo:
		if i * b < a:
			z -= a
			z += i * b
	print(z)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	for i in range(n):
		if s[i] == '1':
			break
	for j in range(n - 1, -1, -1):
		if s[j] == '1':
			break
	if i >= j:
		if s[i] == '0':
			print('0')
		else:
			print(a)
		continue
	cou = s[i:j + 1].count('10')
	cou += 1
	total_cost = cou * a
	reducing_cost = 0
	while i != j + 1:
		if s[i] == '1':
			if reducing_cost != 0:
				if reducing_cost * b < a:
					total_cost += reducing_cost * b
					total_cost -= a
				reducing_cost = 0
		else:
			reducing_cost += 1
		i += 1
	print(total_cost)


t = int(input().strip())
for case in range(t):
	(a, b) = map(int, input().split(' '))
	m_line = input().strip()
	if len(m_line) == 0:
		print(0)
		continue
	onepart_cnt = 0
	pre_m = ''
	for m in m_line:
		if pre_m == '1' and m == '0':
			onepart_cnt = onepart_cnt + 1
		pre_m = m
	if pre_m == '1':
		onepart_cnt = onepart_cnt + 1
	min_c = onepart_cnt * a
	pre_zero_cnt = 1
	flag = False
	pre_m = ''
	for m in m_line:
		if pre_m == '1' and m == '0':
			pre_zero_cnt = 0
			flag = True
		if flag is True and m == '0':
			pre_zero_cnt = pre_zero_cnt + 1
		if flag is True and m == '1':
			flag = False
			tmp_c = pre_zero_cnt * b - a
			if tmp_c < 0:
				min_c = min_c + tmp_c
		pre_m = m
	print(min_c)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	result = 0
	count = float('inf')
	for i in range(n):
		if s[i] == '0':
			count += 1
		else:
			result += min(a, b * count)
			count = 0
	print(result)


t = int(input())
for q in range(t):
	(a, b) = map(int, input().split())
	z = input()
	last = 0
	count = 0
	cost = 0
	flag = 0
	for i in z:
		if i == '1' and last == '1':
			continue
		if i == '1' and last == '0':
			last = '1'
			cost += min(a, b * count)
			count = 0
		if flag == 0:
			if i == '1':
				flag = 1
				cost += a
				last = '1'
				continue
			if i == '0':
				continue
		if flag == 1:
			if i == '0':
				count += 1
				last = '0'
				continue
	print(cost)


t = int(input())
while t > 0:
	dp = []
	prev_i = -1
	flag = 0
	ans = 0
	(a, b) = map(int, input().split())
	arr = input()
	for (idx, s) in enumerate(arr):
		if s == '1':
			if prev_i == -1:
				prev_i = idx
			else:
				dp.append(idx - prev_i - 1)
				prev_i = idx
			flag = 1
	if flag:
		ans = a
	for num in dp:
		ans += min(num * b, a)
	print(ans)
	t -= 1


for y in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	ans = 0
	l = len(s)
	cnt = float('inf')
	for i in range(l):
		if s[i] == '0':
			cnt += 1
		else:
			ans += min(a, cnt * b)
			cnt = 0
	print(ans)


test = int(input())
while test:
	inp = input().strip().split()
	zero = int(inp[0])
	one = int(inp[1])
	z = input().strip('0').split('0')
	s2 = []
	s3 = []
	for i in z:
		if i != '':
			s2.append(i)
	c = 1
	for i in z:
		if i != '':
			s3.append(c)
			c = 1
		else:
			c += 1
	s3.sort()
	s3 = s3[1:]
	ans0 = len(s2) * zero
	ans = len(s2) * zero
	for i in s3:
		if ans - zero + i * one <= ans:
			ans = ans - zero + i * one
		else:
			break
	print(min([ans, ans0]))
	test = test - 1


import sys, io, os, time
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
from functools import lru_cache
import collections, sys, threading
sys.setrecursionlimit(10 ** 9)
threading.stack_size(10 ** 8)

def solve():
	for ii in range(int(input())):
		(a, b) = map(int, input().split())
		s = input()
		n = len(s)

		@lru_cache(None)
		def f(ind, prev_activate):
			if ind == n:
				return 0
			ans = 0
			if s[ind] == '1':
				if prev_activate:
					ans = f(ind + 1, prev_activate)
				else:
					ans = f(ind + 1, 1) + a
			elif prev_activate:
				ans = f(ind + 1, 1) + b
				ans = min(f(ind + 1, 0), ans)
			else:
				ans = f(ind + 1, 1) + b + a
				ans = min(f(ind + 1, 0), ans)
			return ans
		print(f(0, 0))
threading.Thread(target=solve).start()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().lstrip('0').rstrip('0') + '0'
	n = len(s)
	(ones, zeros, one, zero) = ([], [0], 0, 0)
	for i in range(n):
		if s[i] == '0':
			if one:
				ones.append(one)
				one = 0
			zero += 1
		else:
			if zero:
				zeros.append(zero)
				zero = 0
			one += 1
	ans = 0
	for (i, j) in zip(ones, zeros):
		if j == 0:
			ans += a
		else:
			ans += j * b if j * b < a else a
	print(ans)


t = int(input())
while t:
	t -= 1
	(x, y) = map(int, input().split(' '))
	a = []
	s = input()
	one = 0
	zero = 0
	for i in range(len(s)):
		if s[i] == '0':
			if one != 0:
				a.append([one, 1])
			zero += 1
			one = 0
		else:
			if zero != 0 and len(a) != 0:
				a.append([zero, 0])
			one += 1
			zero = 0
	if one != 0:
		a.append([one, 1])
	if len(a) == 0:
		if s[0] == '0':
			print(0)
		else:
			print(a)
		continue
	ans = 0
	for i in range(len(a)):
		if a[i][1] == 0:
			ans += min(y * a[i][0], x)
	ans += x
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(a, b) = inp()
	s = ips()
	n = len(s)
	if s.count('0') == n:
		ans = 0
	else:
		gap = []
		ct1 = 0
		ct2 = 0
		for i in range(n):
			if s[i] == '1':
				st = i
				break
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				end = i
				break
		s = s[st:end + 1]
		n = len(s)
		for i in s:
			if i == '1':
				if ct2 != 0:
					gap.append(ct2)
				ct2 = 0
				ct1 += 1
			else:
				if ct1 != 0:
					gap.append(ct1)
				ct1 = 0
				ct2 += 1
		if ct1 != 0:
			gap.append(ct1)
		if ct2 != 0:
			gap.append(ct2)
		ans = 0
		nn = len(gap)
		for i in range(0, nn, 2):
			ans += a
		for i in range(1, nn, 2):
			ans = min(ans, ans + gap[i] * b - a)
	out(ans)


def solv():
	(x, y) = map(int, input().split())
	s = list(map(int, input()))
	res = 0
	ct = 10 ** 10
	for n in s:
		if n:
			res += min(x, ct * y)
			ct = 0
		else:
			ct += 1
	print(res)
for _ in range(int(input())):
	solv()


t = int(input())
for i in range(t):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	res = 0
	cur_cost = -1
	for c in s:
		if c == '1':
			if cur_cost == -1:
				res += a
			elif cur_cost & cur_cost < a:
				res += cur_cost
			else:
				res += a
			cur_cost = 0
		elif cur_cost != -1:
			cur_cost += b
	print(res)


import math

def main_function():
	test_cases = range(int(input()))
	for test_case in test_cases:
		(a, b) = [int(i) for i in input().split(' ')]
		s = [int(i) for i in input()]
		collector = []
		is_one = []
		counter = 0
		current_val = s[0]
		for i in range(len(s)):
			if s[i] == current_val:
				counter += 1
			else:
				is_one.append(current_val)
				collector.append(counter)
				counter = 1
				current_val = s[i]
			if i == len(s) - 1:
				is_one.append(current_val)
				collector.append(counter)
		if is_one[0] == 0 and len(collector) > 0:
			collector.pop(0)
		if is_one[-1] == 0 and len(collector) > 0:
			collector.pop(-1)
		real_counter = math.ceil(len(collector) / 2) * a
		for i in range(len(collector)):
			if i % 2 == 1:
				if a > b * collector[i]:
					real_counter -= a
					real_counter += b * collector[i]
		print(real_counter)
main_function()


import math
import sys
from collections import defaultdict as df

def ini():
	return int(sys.stdin.readline())

def inp():
	return map(int, sys.stdin.readline().strip().split())

def li():
	return list(map(int, sys.stdin.readline().strip().split()))
for _ in range(ini()):
	(a, b) = inp()
	s = input()
	s = s.strip('0')
	if len(s) == 0:
		print(0)
		continue
	t = '1'
	c = 1
	for i in s[1:]:
		if i == '1' and c:
			pass
		elif i == '1':
			c = 1
			t += '1'
		else:
			c = 0
			t += '0'
	o = []
	c = 0
	for i in t:
		if i == '0':
			c += 1
		elif c != 0:
			o += [c]
			c = 0
	if c:
		o += [c]
	s = a
	for i in o:
		s += min(a, i * b)
	print(s)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	(c, d) = (-100000, a)
	for i in input():
		if i == '1':
			if c > 0:
				d += min(a, c * b)
			c = 0
		else:
			c += 1
	print(0 if c < 0 else d)


def testcase():
	(a, b) = list(map(int, input().split()))
	s = input()
	n = len(s)
	if s.count('0') == n:
		print(0)
		return
	data = []
	idx = 0
	while idx < n and s[idx] == '0':
		idx += 1
	last = idx - 1
	for i in range(idx, n - 1):
		if s[i] != s[i + 1]:
			if s[i] == '0':
				data.append(i - last)
			last = i
	cost = a * (len(data) + 1)
	for d in data:
		if d * b < a:
			cost = cost - a + d * b
	print(cost)
	return
import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t):
	testcase()


for _ in range(int(input())):
	(a, p) = map(int, input().split())
	b = input()
	cost = 0
	if p >= a:
		b = b.strip('0')
		for i in b.split('0'):
			if i:
				cost += a
		print(cost)
	else:
		b = b.strip('0')
		n = len(b)
		if n == 0:
			print(0)
			continue
		for i in range(n - 1):
			if b[i] == '1' and b[i + 1] == '0':
				t = 0
				j = i
				while b[j + 1] == '0':
					t += 1
					j += 1
				if t <= a // p:
					cost += t * p
				else:
					cost += a
		cost += a
		print(cost)


import sys

def solve(mines, activate_cost, place_cost):
	soln = 0
	mine_fields = []
	state = 0
	for (i, m) in enumerate(mines):
		if m != state:
			if state == 0:
				if mine_fields:
					delta = i - mine_fields[-1]
					if activate_cost + delta * place_cost < 2 * activate_cost:
						mine_fields.pop()
						soln += delta * place_cost
			else:
				mine_fields.append(i)
		state = m
	if state == 1:
		mine_fields.append(len(mines))
	soln += len(mine_fields) * activate_cost
	return soln

def main():
	test_cases = int(sys.stdin.readline())
	for _ in range(test_cases):
		(activate_cost, place_cost) = (int(i) for i in sys.stdin.readline().split())
		mines = [int(i) for i in sys.stdin.readline().strip()]
		print(solve(mines, activate_cost, place_cost))
main()


import os
import sys
from io import BytesIO, IOBase
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397]
from collections import defaultdict
from math import sqrt
import math

def solution():
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	cost = 0
	dist = 0
	init = 0
	for i in range(n):
		if s[i] == '0':
			dist += 1
		elif init == 0:
			cost += a
			dist = 0
			init = 1
		elif dist != 0:
			cost += min(a, dist * b)
			dist = 0
	print(cost)

def main():
	testcases = 1
	testcases = int(input())
	for _ in range(testcases):
		solution()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
ans = ''
for _ in range(t):
	(a, b) = map(int, input().split())
	map0 = input()
	for i in range(len(map0)):
		if map0[i] == '1':
			map0 = map0[i:]
			break
	map0 = map0 + '00'
	l1 = []
	x = 0
	for i in range(len(map0) - 1):
		if map0[i] == '1' and map0[i + 1] == '0':
			y = i
			l1.append([x, y])
		elif map0[i] == '0' and map0[i + 1] == '1':
			x = i + 1
	if l1 == []:
		ans += str(0) + '\n'
		continue
	money = a
	for i in range(1, len(l1)):
		if a > b * (l1[i][0] - l1[i - 1][1] - 1):
			money += b * (l1[i][0] - l1[i - 1][1] - 1)
		else:
			money += a
	ans += str(money) + '\n'
print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	vals = input().strip('\n')
	grps = []
	i = 0
	while i < len(vals):
		if vals[i] == '0':
			i += 1
			continue
		j = i
		while j + 1 < len(vals) and vals[j + 1] == '1':
			j += 1
		grps.append([i, j])
		i = j + 1
	if not grps:
		print(0)
		continue
	if len(grps) == 1:
		print(a)
		continue
	groups = len(grps)
	ans = 0
	for (x, y) in zip(grps, grps[1:]):
		dist = y[0] - x[1] - 1
		if dist * b + a <= a * 2:
			groups -= 1
			ans += dist * b
	print(groups * a + ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = str(input())
	(start, end) = (-1, -1)
	n = len(s)
	for i in range(n):
		if s[i] == '1':
			start = i
			break
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			end = i + 1
			break
	if start == -1:
		print(0)
	else:
		ans = a
		(zero, one) = (0, 0)
		l = []
		for i in range(start, end):
			if s[i] == '1':
				if zero != 0:
					l.append(zero)
					zero = 0
				one += 1
			else:
				if one != 0:
					l.append(one)
					one = 0
				zero += 1
		if len(l) == 1:
			print(a)
		else:
			for i in range(1, len(l), 2):
				ans += min(a, b * l[i])
			print(ans)


t = int(input())
for c in range(t):
	(a, b) = [int(x) for x in input().strip().split()]
	mines = input()
	mines = mines.replace('0', ' ').strip().replace(' ', '0')
	mines_group = mines.replace('0', ' ').split()
	cost = len(mines_group * a)
	mines_gap = mines.replace('1', ' ').split()
	for each in mines_gap:
		if b * len(each) < a:
			cost = cost - a + b * len(each)
	print(cost)


import sys
from math import *
from fractions import *

def main():

	def modst(a, s):
		ret = 1
		while s:
			if s % 2:
				ret = ret * a % mod
			a = a * a % mod
			s //= 2
		return ret

	def C_n_k(n, k):
		return fact[n] * modst(fact[n - k] * fact[k] % mod, mod - 2) % mod
	(a, b) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline().strip()
	q = []
	(n, flag) = (len(s), -1)
	for i in range(n):
		if s[i] == '1':
			if flag == -1:
				flag = i
		elif flag != -1:
			q.append([flag, i - 1])
			flag = -1
		if flag != -1 and i == n - 1:
			q.append([flag, i])
	l = len(q) - 1
	print(0 if l == -1 else a + sum([min(a, (q[i + 1][0] - q[i][1] - 1) * b) for i in range(l)]))
for i in range(int(sys.stdin.readline().strip())):
	main()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	l = 10 ** 9
	need = 0
	cost = 0
	for i in s:
		if i == '1':
			cost += min(l * m, n)
			l = 0
		else:
			l += 1
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	(ans, le, z) = (0, len(s), 10000)
	for i in range(le):
		if s[i] == '0':
			z += 1
		else:
			ans += min(z * b, a)
			z = 0
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	c = input()
	structure = []
	prev = 0
	count = 0
	for z in c:
		if z == '0':
			prev = 0
			count += 1
		elif z == '1':
			if prev == 0:
				structure.append(count)
				count = 0
				prev = 1
	structure.append(count)
	if len(structure) == 1:
		mincost = 0
		print(mincost)
	else:
		mincost = a
		for i in range(1, len(structure) - 1):
			mincost = min(mincost + a, mincost + b * structure[i])
		print(mincost)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	mapp = list(input())
	cost = 0
	bombs = False
	firstOne = False
	numopen = 0
	for i in range(len(mapp)):
		if mapp[i] == '1':
			firstOne = True
		if mapp[i] == '0' and (not bombs) and firstOne:
			bombs = True
			numopen += 1
		elif bombs and mapp[i] == '0':
			numopen += 1
		elif bombs and mapp[i] == '1':
			c = 0
			if i == 1:
				c = 1
			else:
				c = 2
			if c * a >= numopen * b + a:
				for j in range(numopen + 1):
					mapp[i - j] = '1'
				cost += numopen * b
			bombs = False
			numopen = 0
	for i in range(len(mapp)):
		if mapp[i] == '1':
			cost += a
			for j in range(i, len(mapp)):
				if mapp[j] == '1':
					mapp[j] = '0'
				else:
					break
	print(cost)


t = int(input())
for i in range(0, t):
	(a, b) = map(int, input().split())
	str1 = input()
	y = str1.find('1')
	z = str1.rfind('1')
	if y == -1:
		ans = 0
	else:
		str2 = str1[y:z + 1]
		x = len(str2)
		zz = str2.count('1')
		if zz == x:
			ans = a
		else:
			ct0 = 0
			ct1 = 0
			l1 = []
			l2 = []
			k = 0
			while k < x:
				if str2[k] == '1':
					l2.append(ct0)
					ct0 = 0
					ct1 = 0
					while k < x and str2[k] == '1':
						ct1 += 1
						k += 1
				else:
					l1.append(ct1)
					ct1 = 0
					ct0 = 0
					while str2[k] == '0':
						ct0 += 1
						k += 1
				if k == x:
					l1.append(ct1)
					break
			l3 = []
			l4 = []
			yy = len(l1)
			for j in range(1, yy + 1):
				l3.append(j * a)
			for j in range(0, yy):
				l4.append(l2[j] * b)
			for j in range(1, yy):
				l3[j] = min(l3[j - 1] + a, l3[j - 1] + l4[j])
			ans = l3[-1]
	print(ans)


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	mines = []
	gaps = []
	len_m = 0
	len_g = 0
	if int(s):
		s = str(int(str(int(s))[::-1]))
		cost1 = 0
		tot = 0
		cost = a
		for i in s:
			if i == '0':
				if len_m == 0:
					len_g += 1
				else:
					len_m = 0
					len_g += 1
			elif len_g == 0:
				len_m += 1
			else:
				if len_g * b < a:
					cost += len_g * b
				else:
					cost += a
				len_g = 0
				len_m += 1
		print(cost)
	else:
		print(0)


for _ in range(int(input())):
	(A, B) = [int(i) for i in input().split(' ')]
	S = input()
	Index = 0
	Final = 0
	C = []
	Zero = 0
	D = S.count('1')
	for i in range(len(S)):
		if S[i] == '1':
			Index = i
			break
	for i in range(len(S)):
		if S[-i - 1] == '1':
			Final = -i - 1 + len(S)
			break
	Zero_On = False
	for i in range(Index, Final + 1):
		if S[i] == '0':
			Zero += 1
			Zero_On = True
		else:
			if Zero_On:
				C += [Zero]
				Zero_On = False
			Zero = 0
	if Index == 0 and Final == 0:
		if D == 0:
			print(0)
		else:
			print(A)
	elif len(C) == 0:
		print(A)
	else:
		ActualCost = A * (len(C) + 1)
		for i in range(len(C)):
			if C[i] * B < A:
				ActualCost -= A
				ActualCost += C[i] * B
		print(ActualCost)


import itertools
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	S = input()
	groups = [(int(k), len(list(v))) for (k, v) in itertools.groupby(S)]
	result = 0
	numGroups = sum((1 for (k, _) in groups if k == 1))
	for i in range(1, len(groups) - 1):
		(prev, curr, nxt) = (groups[i - 1], groups[i], groups[i + 1])
		if prev[0] == 1 and nxt[0] == 1:
			dist = curr[1]
			if b * dist < a:
				result += b * dist
				numGroups -= 1
	result += numGroups * a
	print(result)


import math
debug = False

def pprint(*args):
	if debug:
		print(*args)

def solve():
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	subs = []
	index = 0
	while index < n:
		if s[index] == '1':
			start = index
			end = index
			index += 1
			while index < n and s[index] == '1':
				index += 1
				end += 1
			subs.append([start, end])
		index += 1
	pprint(subs)
	ans = 0
	count_explode = 1
	for i in range(len(subs) - 1):
		left = subs[i]
		right = subs[i + 1]
		if (right[0] - left[1] - 1) * b + a <= 2 * a:
			ans += (right[0] - left[1] - 1) * b
			right[0] = left[0]
	pprint(subs)
	if len(subs) == 0:
		return 0
	for i in range(len(subs) - 1):
		if subs[i][1] < subs[i + 1][0]:
			count_explode += 1
	return ans + count_explode * a
t = int(input())
while t > 0:
	t -= 1
	print(solve())


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	if s.count('1') == 0:
		print(0)
		continue
	n = len(s)
	aida = []
	k = 0
	for i in range(n):
		if s[i] == '1' and (i == 0 or s[i - 1] == '0'):
			aida.append(k)
			k = 0
		elif s[i] == '0':
			k += 1
	ans = a
	for i in aida[1:]:
		if a < b * i:
			ans += a
		else:
			ans += b * i
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	holes = []
	current_hole = 0
	idx = s.find('1')
	if idx == -1:
		print('0')
		continue
	for x in s[idx:]:
		if x == '1':
			if current_hole != 0:
				holes.append(current_hole)
			current_hole = 0
		else:
			current_hole += 1
	place_cost = 0
	holes_removed = 0
	for hole in holes:
		if hole * b < a:
			place_cost += hole * b
			holes_removed += 1
	print((len(holes) - holes_removed + 1) * a + place_cost)


def main():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		s = input()
		intervals = []
		n = len(s)
		for i in range(n):
			if s[i] == '1':
				if i == 0 or s[i - 1] == '0':
					intervals.append([i, i])
				else:
					intervals[-1][1] += 1
		cost = 0
		mines = len(intervals)
		for i in range(mines - 1):
			rng = intervals[i + 1][0] - intervals[i][1] - 1
			if rng * b <= a:
				cost += b * rng
				mines -= 1
		cost += a * mines
		print(cost)
main()


from sys import maxsize, stdout, stdin, stderr
import re
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
for _ in range(I()):
	(a, b) = tup()
	s = S().strip('0').split('1')
	if len(s) == 1:
		print(0)
	else:
		ans = a
		for i in s:
			if i:
				ans += min(len(i) * b, a)
		print(ans)


T = int(input())
for _ in range(0, T):
	(a, b) = list(map(int, input().split()))
	s = input()
	ptr1 = len(s)
	ptr2 = 0
	for i in range(0, len(s)):
		if s[i] == '1' and ptr1 == len(s):
			ptr1 = i
		if s[i] == '1':
			ptr2 = i + 1
	if ptr1 == len(s):
		print(0)
	else:
		L1 = []
		L0 = []
		c = 1
		for i in range(ptr1 + 1, ptr2):
			if s[i] == s[i - 1]:
				c += 1
			elif s[i - 1] == '0':
				L0.append(c)
				c = 1
			else:
				L1.append(c)
				c = 1
		L1.append(c)
		if len(L1) == 1:
			print(a)
		else:
			ans = a
			for i in range(0, len(L1) - 1):
				if b * L0[i] <= a:
					ans += b * L0[i]
				else:
					ans += a
			print(ans)


import math, copy
from collections import defaultdict as dft, deque, Counter
from heapq import heappush, heappop

def mp():
	return map(int, input().split())

def ml():
	return list(map(int, input().split()))

def solve():
	(a, b) = mp()
	s = input()
	n = len(s)
	arr = []
	l = -1
	r = -1
	for i in range(n):
		if s[i] == '1':
			if l == -1:
				l = i
				r = i
			else:
				r = i
		elif l != -1:
			arr.append([l, r])
			l = -1
			r = -1
	if l != -1:
		arr.append([l, r])
	if len(arr) == 0:
		print(0)
		return
	stack = [copy.deepcopy(arr[0])]
	price = 0
	for i in range(1, len(arr)):
		if (arr[i][0] - stack[-1][1] - 1) * b <= a:
			price += (arr[i][0] - stack[-1][1] - 1) * b
			stack[-1][1] = arr[i][1]
		else:
			stack.append(copy.deepcopy(arr[i]))
	print(price + a * len(stack))
tc = int(input())
for _ in range(tc):
	solve()


def main():
	t = int(input())
	for __ in range(t):
		(a, b) = map(int, input().split())
		s = input()
		stack = []
		c = False
		cost = 0
		for (i, j) in enumerate(s):
			if j == '1':
				c = True
				if stack:
					if i - stack[-1] == 1:
						stack.pop()
						stack.append(i)
					elif (i - stack[-1] - 1) * b + a < a * 2:
						cost += (i - stack[-1] - 1) * b
						stack.pop()
						stack.append(i)
					else:
						cost += a
						stack.pop()
						stack.append(i)
				else:
					stack.append(i)
		if c:
			print(cost + a)
		else:
			print(0)
main()


def main():
	(a, b) = map(int, input().split())
	s = input()
	s1 = 0
	s01 = []
	n = len(s)
	if s.count('1') == 0:
		print(0)
		return
	for i in range(1, n):
		if s[i] == '0' and s[i - 1] == '1':
			s01.append(0)
			s01[-1] = [i]
		if s[i] == '1' and s[i - 1] == '0':
			if len(s01) > 0:
				s01[-1].append(i)
	if len(s01) == 0:
		print(a)
		return
	if len(s01[-1]) == 1:
		s01.pop()
	c1 = len(s01) + 1
	d = []
	for i in s01:
		d.append(i[1] - i[0])
	f = 0
	df = 0
	for i in d:
		if i * b < a:
			f += 1
			df += i * b
	print(a * (c1 - f) + df)
rw = int(input())
for wewq in range(rw):
	main()


test_cases = int(input())
for test_case in range(test_cases):
	(a, b) = list(map(int, input().split()))
	n = list(map(int, input()))
	ans = 0
	count = 0
	while len(n) != 0:
		if n[-1] == 1:
			break
		else:
			n.pop(-1)
		if len(n) == 0:
			break
	while len(n) != 0:
		if n[0] == 1:
			break
		else:
			n.pop(0)
		if len(n) == 0:
			break
	ones = 1
	zeroes = 0
	for i in range(len(n) - 1):
		if n[i] == 1:
			if n[i + 1] == 0:
				ones += 1
				count = 1
		elif n[i + 1] == 0:
			count += 1
		else:
			if b * count + a < 2 * a:
				zeroes += 1
				ans += b * count
			count = 1
	if len(n) == 0:
		print(0)
	else:
		ans += (ones - zeroes) * a
		print(ans)


import sys
t = int(sys.stdin.readline().strip())
for i in range(t):
	(a, b) = map(int, sys.stdin.readline().strip().split())
	s = sys.stdin.readline().strip()
	c = 0
	last_one = None
	for i in range(0, len(s)):
		if s[i] == '1':
			if last_one is None:
				c += a
			else:
				diff = i - last_one - 1
				if diff > 0:
					c += min(b * diff, a)
			last_one = i
	print(c)


T = int(input())
for z in range(T):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	ll = []
	zeros = 0
	ones = 0
	for e in s:
		if e == '0':
			if len(ll) != 0:
				ll.append(0)
				zeros += 1
		elif len(ll) == 0:
			ll.append(1)
			ones += 1
		elif ll[-1] == 0:
			ll.append(1)
			ones += 1
	count = 0
	add = 0
	for i in range(len(ll)):
		if ll[i] == 0:
			count += 1
		else:
			if b * count < a and count != 0:
				ones -= 1
				add += b * count
			count = 0
	add += ones * a
	print(add)


from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil
from bisect import bisect_left as bl
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
INF = float('inf')
mod = 1000000007

def solve():
	(a, b) = mp()
	s = st()
	one = [i for i in range(len(s)) if s[i] == '1']
	if not one:
		pr(0)
		return
	first = one[0]
	last = one[-1]
	i = first
	ans = a
	prev = s[first]
	c = 0
	while i <= last:
		if s[i] != prev and s[i] == '1':
			ans += min(a, c)
			c = 0
		if s[i] == '0':
			c += b
		prev = s[i]
		i += 1
	pr(ans)
for _ in range(inp()):
	solve()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	note = 1
	if note == 0:
		cnt = 0
		for i in s:
			if i == '1':
				cnt += 1
		print(cnt * a)
	else:
		n = len(s)
		ans = 0
		cnt = 0
		trigger = 0
		for i in range(n):
			if s[i] == '1':
				if trigger == 0:
					ans += a
					trigger = 1
				elif cnt * b > a:
					ans += a
				else:
					ans += cnt * b
				cnt = 0
			else:
				cnt += 1
		print(ans)


t = int(input())
for i in range(t):
	(a, p) = map(int, input().split())
	x = input()
	c = 1000000
	ans = 0
	for i in range(len(x)):
		if x[i] == '0':
			c += 1
		else:
			ans += min(a, p * c)
			c = 0
	print(ans)


t = int(input())
for i in range(t):
	(a, p) = map(int, input().split())
	binarr = list(input())
	gaps = []
	acts = 0
	if binarr[0] == '1':
		acts += 1
	cost = 0
	current = 0
	for bit in binarr:
		if bit == '0':
			current += 1
		elif current:
			gaps.append(current)
			current = 0
			acts += 1
	if current:
		gaps.append(current)
	if binarr[0] == '0':
		del gaps[0]
	if binarr[-1] == '0' and acts:
		del gaps[-1]
	gaps.sort()
	for gap in gaps:
		if gap * p <= a:
			cost += gap * p
			acts -= 1
		else:
			break
	print(cost + acts * a)


import sys
from itertools import permutations
from itertools import combinations
from itertools import combinations_with_replacement

def countOne(s):
	ctr = 0
	for i in s:
		if i == '1':
			ctr += 1
	return ctr
for _ in range(int(input())):
	liStr = input().split()
	a = int(liStr[0])
	b = int(liStr[1])
	s = input()
	cOne = countOne(s)
	if cOne == 0:
		print('0')
		continue
	if cOne == 1:
		print(a)
		continue
	f = 0
	ctr = 0
	li = []
	for i in s:
		if i == '1':
			f = 1
			if ctr != 0:
				li.append(ctr)
				ctr = 0
			continue
		if i == '0' and f == 1:
			ctr += 1
	ans = (len(li) + 1) * a
	for i in li:
		if b * i < a:
			ans -= a
			ans += b * i
	print(ans)


import sys
import math as m

def mod(a, b):
	if a % b == 0:
		return b
	return mod(b, a % b)
t = int(input())
while t:
	t -= 1
	flag = False
	(a, b) = map(int, input().split())
	n = input()
	if len(n) == 1 and n[0] == '0':
		print('0')
		continue
	boom = list()
	for i in range(len(n)):
		count = 0
		if n[i] == '1':
			flag = True
			continue
		if n[i] == '0' and n[i - 1] == '1' and (i != len(n) - 1) and (i > 0):
			while n[i] == '0':
				i += 1
				count += 1
				if i == len(n) - 1:
					break
		if n[i] == '1':
			boom.append(count)
	l = len(boom)
	if flag and l == 0:
		print(a)
		continue
	ans = 0
	for i in range(l):
		if i == 0:
			ans += min(2 * a, boom[i] * b + a)
		else:
			ans += min(a, boom[i] * b)
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	res = []
	res = a
	seg1 = s.split('0')
	seg0 = s.split('1')
	if len(seg0) == 1:
		print(0)
		continue
	start = 0
	if not seg1[0]:
		start += 1
	end = len(seg0) - 1
	if not seg1[-1]:
		end -= 1
	while start <= end:
		res += min(a, b * len(seg0[start]))
		start += 1
	print(res)


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
from heapq import heapify, heappop, heappush
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()[:-1]
	x = []
	c = 1
	n = len(s)
	p = 0
	q = 0
	e = 0
	f = 0
	if s[0] == '0':
		e += 1
	else:
		f += 1
	for i in range(1, n):
		if s[i] == '1':
			f += 1
		else:
			e += 1
		if s[i] == s[i - 1]:
			c += 1
		else:
			if s[i - 1] == '0':
				x.append(-1 * c)
			else:
				x.append(c)
				p += 1
			c = 1
	if s[-1] == '0':
		x.append(-1 * c)
	else:
		x.append(c)
		p += 1
	if e == 0:
		print(a)
	elif f == 0:
		print(0)
	else:
		q = []
		for i in range(len(x)):
			if x[i] > 0:
				break
		for j in range(len(x) - 1, -1, -1):
			if x[j] > 0:
				break
		for k in range(i, j + 1):
			if x[k] < 0:
				q.append(b * abs(x[k]))
		c = 0
		m = len(q)
		if m == 0:
			print(a)
		else:
			f = [0] * m
			for i in range(m):
				if q[i] <= a:
					c += q[i]
					p -= 1
					f[i] = 1
			if c == 0:
				print(a * p)
			else:
				print(c + a * p)


from sys import stdin, stdout

def INI():
	return int(stdin.readline())

def INL():
	return [int(_) for _ in stdin.readline().split()]

def INS():
	return stdin.readline()

def MOD():
	return pow(10, 9) + 7

def OPS(ans):
	stdout.write(str(ans) + '\n')

def OPL(ans):
	[stdout.write(str(_) + ' ') for _ in ans]
	stdout.write('\n')
for _ in range(INI()):
	(a, b) = INL()
	S = INS()
	n = len(S)
	c = 0
	ans = 0
	f = -1
	for i in range(n):
		if S[i] == '1':
			if f == -1:
				f = 0
				c = 0
				ans += a
			else:
				ans += min(c * b, a)
				c = 0
		else:
			c += 1
	OPS(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	s = list(s)
	n = len(s)
	ans = 0
	for i in range(n):
		if s[i] == '1':
			curr = a
			nex = 0
			i += 1
			while i < n and s[i] != '1':
				nex += b
				if nex > a:
					break
				i += 1
			if i >= n or nex > a:
				ans += a
			else:
				ans += nex
				i -= 1
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = str(input())
	X = []
	cur = s[0]
	cnt = 0
	n = len(s)
	for i in range(n):
		if s[i] == cur:
			cnt += 1
		else:
			X.append((cur, cnt))
			cur = s[i]
			cnt = 1
	else:
		X.append((cur, cnt))
	m = len(X)
	if m == 1:
		if X[0][0] == '1':
			print(a)
		else:
			print(0)
		continue
	INF = 10 ** 18
	dp = [[INF] * 2 for _ in range(m)]
	if X[0][0] == '1':
		dp[0][1] = a
	else:
		dp[0][0] = 0
		dp[0][1] = a + b * X[0][1]
	for i in range(m):
		(cur, cnt) = X[i]
		if cur == '1':
			dp[i][1] = min(dp[i][1], min(dp[i - 1][0] + a, dp[i - 1][1]))
		else:
			dp[i][0] = min(dp[i][0], dp[i - 1][1])
			dp[i][1] = min(dp[i][1], dp[i - 1][1] + b * cnt)
	print(min(dp[m - 1][0], dp[m - 1][1]))


T = int(input())
for t in range(T):
	(a, b) = map(int, input().split())
	s = input()
	d = []
	for i in range(len(s)):
		if s[i] == '1':
			d.append(i)
	res = 0
	for j in range(1, len(d)):
		if abs(d[j] - d[j - 1] - 1) * b <= a:
			res += abs(d[j] - d[j - 1] - 1) * b
		else:
			res += a
	print(res + a if d else 0)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(ca, cb) = map(int, input().split())
	al = list(input().rstrip())
	cntl = []
	prev = al[0]
	cnt = 1
	for a in al[1:]:
		if prev == a:
			cnt += 1
		else:
			cntl.append((prev, cnt))
			cnt = 1
			prev = a
	cntl.append((prev, cnt))
	if cntl[0][0] == '0':
		if len(cntl) == 1:
			print(0)
			continue
		cntl = cntl[1:]
	if cntl[-1][0] == '0':
		cntl = cntl[:-1]
	cost = ca * (len(cntl) + 1) // 2
	for i in range(len(cntl) // 2):
		d = cntl[1 + i * 2][1]
		dc = d * cb
		if dc < ca:
			cost -= ca - dc
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	c1 = s.find('1')
	c2 = s.rfind('1')
	if c1 == -1:
		print(0)
	elif c1 == c2:
		print(a)
	else:
		s = s[c1:c2 + 1]
		s = s.replace('10', '1#0')
		s = s.replace('01', '0#1')
		s = s.split('#')
		ans = 0
		zero = []
		for i in range(len(s)):
			if s[i][0] == '1':
				ans += a
			else:
				zero.append(len(s[i]))
		zero.sort()
		res = ans
		for i in range(1, len(zero)):
			zero[i] = zero[i] + zero[i - 1]
		for i in range(len(zero)):
			x = res - a * (i + 1) + b * zero[i]
			ans = min(ans, x)
		print(ans)


n = int(input())
for i in range(0, n):
	inab = input().split(' ')
	a = int(inab[0])
	b = int(inab[1])
	inmines = input()
	if not '1' in inmines:
		print(0)
		continue
	L = []
	count = 0
	for i in range(0, len(inmines)):
		if inmines[i] == '1':
			if count == 0:
				continue
			if i == count:
				count = 0
			else:
				L.append(count)
				count = 0
		else:
			count += 1
	if len(L) == 0:
		print(a)
		continue
	m = a
	for l in L:
		m = min(a + m, b * l + m)
	print(m)


def f():
	(a, b) = map(int, input().split())
	s = input()
	ToF = False
	c = 0
	ans = 0
	for item in s:
		if ToF:
			if item == '0':
				c += 1
			else:
				ans += min(c * b, a)
				c = 0
		if item == '1':
			ToF = True
	print(ans + a * ToF)
t = int(input())
for i in range(t):
	f()


casosN = int(input())
casos = []
for i in range(casosN):
	temp = [int(j) for j in input().split(' ')]
	temp2 = input()
	casos.append([temp, temp2])
for caso in casos:
	bombMap = []
	indice = -1
	indice2 = -1
	for x in range(len(caso[1])):
		if caso[1][x] == '1':
			if indice == -1:
				indice = x
			indice2 = x
		elif indice != -1:
			bombMap.append([indice, indice2])
			indice = -1
	if indice != -1:
		bombMap.append([indice, indice2])
	costo = 0
	for i in range(len(bombMap)):
		if i == len(bombMap) - 1:
			costo += caso[0][0]
		else:
			costoB = (bombMap[i + 1][0] - 1 - bombMap[i][1]) * caso[0][1] + caso[0][0]
			costoA = 2 * caso[0][0]
			if costoB <= costoA:
				costo += costoB - caso[0][0]
			else:
				costo += costoA - caso[0][0]
	print(costo)


t = int(input())
for j in range(t):
	(a, b) = map(int, input().split())
	s = input()
	qz = []
	w = 0
	f = -1
	for i in range(len(s)):
		if s[i] == '1' and f == -1:
			f = i
		if s[i] == '0':
			w += 1
			qz.append(w)
		else:
			w = 0
			qz.append(w)
	dp = []
	if s[0] == '1':
		dp.append(a)
	else:
		dp.append(0)
	for i in range(1, len(s)):
		if s[i] == '0':
			e = dp[-1]
			dp.append(e)
		elif s[i - 1] == '1':
			e = dp[-1]
			dp.append(e)
		else:
			e = dp[-1]
			if i > f:
				dp.append(min(a, b * qz[i - 1]) + e)
			else:
				dp.append(a + e)
	print(dp[-1])


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		(a, b) = ria()
		s = list(rs())
		l = 0
		r = len(s) - 1
		for i in range(len(s)):
			if s[i] == '1':
				l = i
				break
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '1':
				r = i
				break
		g = s[l:r + 1]
		gaps = []
		gap = 0
		k = 0
		for i in range(len(g)):
			if g[i] == '0':
				gap += 1
			else:
				k = 1
				if gap != 0:
					gaps.append(gap)
				gap = 0
		if k == 0:
			print(0)
			continue
		n1 = len(gaps) + 1
		gaps = sorted(gaps)
		costs = [n1 * a]
		costnow = 0
		for i in range(len(gaps)):
			n1 -= 1
			costnow += b * gaps[i]
			costs.append(costnow + n1 * a)
		print(min(costs))
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


t = int(input())
while t > 0:
	t -= 1
	(a, b) = map(int, input().split())
	s = input()
	i = -1
	j = -1
	ans = 0
	l = []
	k = 0
	n = len(s)
	se = 0
	while k < n:
		if s[k] == '0':
			k += 1
		else:
			se = 1
			if i != -1:
				j = k
				l.append(j - i - 1)
			else:
				i = k
			k += 1
			while k < n:
				if s[k] == '0':
					break
				else:
					k += 1
			i = k - 1
	flag = 0
	if len(l) == 0:
		if se == 0:
			ans = 0
		else:
			ans = a
	else:
		for i in l:
			if flag == 0:
				if i * b + a < a * 2:
					ans += i * b + a
					flag = 1
				else:
					ans += a
					flag = 0
			elif i * b < a:
				ans += i * b
				flag = 1
			else:
				flag = 0
		if flag == 0:
			ans += a
	print(ans)


import sys, math, itertools, collections, copy
input = sys.stdin.readline
INF = 10 ** 9 + 20

def solve():
	(a, b) = map(int, input().split())
	s = input()
	s = s[:-1]
	N = len(s)
	if N == 0:
		print(0)
		return
	dp = [INF] * N
	was = False
	cur = 0
	if s[0] == '1':
		dp[0] = a
		was = True
	else:
		dp[0] = 0
	for i in range(1, N):
		if s[i] == '1':
			if not was:
				dp[i] = dp[i - 1] + a
			elif cur == 0 and was:
				dp[i] = dp[i - 1]
			else:
				dp[i] = min(dp[i], dp[i - cur] + b * cur, dp[i - 1] + a)
			cur = 0
			was = True
		else:
			if was:
				cur += 1
			else:
				cur = 0
			dp[i] = dp[i - 1]
	print(dp[-1])
for _ in range(int(input())):
	solve()


def solve(a, b, s):
	arr = []
	mines = s.count('1')
	if mines == 0:
		return 0
	if mines == len(s):
		return a
	if s[0] == '0':
		arr.append(1)
	for i in range(1, len(s)):
		if s[i] == '0':
			if s[i - 1] == '1':
				arr.append(1)
			else:
				arr[-1] += 1
	if s[0] == '0':
		arr.pop(0)
	if s[-1] == '0':
		arr.pop()
	if not arr:
		return a
	dp0 = a
	dp1 = a + arr[0] * b
	for i in range(1, len(arr)):
		x = min(dp0 + a, dp1)
		y = x + b * arr[i]
		(dp0, dp1) = (x, y)
	return min(dp0 + a, dp1)
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	print(solve(a, b, s))


x = int(input())
for i in range(x):
	(a, b) = map(int, input().split())
	s = input()
	ans = []
	count = 1
	if s.count('0') == len(s):
		print(0)
		continue
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			count += 1
		else:
			ans.append([count, int(s[i - 1])])
			count = 1
	ans.append([count, int(s[-1])])
	if len(ans) and ans[0][1] == 0 and len(ans):
		ans.pop(0)
	if len(ans) and ans[-1][1] == 0 and len(ans):
		ans.pop()
	total = a
	for i in range(2, len(ans), 2):
		total += min(a, ans[i - 1][0] * b)
	print(total)


t = int(input())
for q in range(t):
	c = input()
	L = [int(i) for i in c.split()]
	(a, b) = (L[0], L[1])
	ch = input()
	L2 = ch.split('1')
	L = []
	for i in range(1, len(L2) - 1):
		if len(L2[i]) > 0:
			L.append(len(L2[i]))
	dp = [a] * (len(L) + 1)
	for i in range(len(L)):
		dp[i + 1] = dp[i] + min(a, L[i] * b)
	if ch.count('1'):
		print(dp[-1])
	else:
		print(0)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	d = list(s)
	e = int(min(d))
	f = int(max(d))
	if e == f and e == 1:
		print(a)
	elif e == f and e == 0:
		print(0)
	else:
		zero = 0
		s += '0'
		for i in range(len(s)):
			if s[i] == '1':
				k = i
				break
		for i in range(k, len(s)):
			if s[i] == '0':
				k = i
				zero = zero + a
				break
		p = 0
		while k < len(s):
			if s[k] == '1':
				zero += min(p * b, a)
				p = 0
				k = k + 1
			elif s[k] == '0':
				p += 1
				k += 1
		if len(s) > 0:
			print(zero)


for _ in range(int(input())):
	(d, b) = map(int, input().split())
	s = input()
	a = []
	j = 0
	c = 0
	k = 0
	for i in range(len(s)):
		if s[i] == '0':
			j = j + 1
		else:
			if i != 0 and s[i - 1] == '0':
				a.append(j)
				j = 0
			k = k + 1
	if s[0] == '0':
		a = a[1:]
	for i in a:
		c = c + min(b * i, d)
	if k != 0:
		print(c + d)
	else:
		print(c)


import sys
input = sys.stdin.readline

def prog():
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		s = input().strip()
		s2 = ''
		idx1 = 0
		idx2 = -1
		for i in range(len(s)):
			if s[i] == '1':
				idx2 = i
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '1':
				idx1 = i
		s2 = s[idx1:idx2 + 1]
		s = s2 + '#'
		gaps = []
		curr = 1
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				curr += 1
			else:
				gaps.append([s[i], curr])
				curr = 1
		ones = 0
		zeros = []
		for seg in gaps:
			if seg[0] == '1':
				ones += 1
			else:
				zeros.append(seg[1])
		zeros.sort()
		cost = 0
		for i in range(len(zeros)):
			if b * zeros[i] <= a:
				ones -= 1
				cost += b * zeros[i]
		cost += ones * a
		print(cost)
prog()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	(count, ans, ln) = (9999999999999, 0, len(s))
	for i in range(ln):
		if s[i] == '0':
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


def answer():
	is1 = True
	ans = 0
	val = a
	for i in st[:len(st) - last]:
		if i == '1':
			if is1:
				ans += min(a, val)
				val = 0
				is1 = False
		else:
			val += b
			is1 = True
	return ans
for T in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	st = ''
	first = False
	for i in s:
		if i == '1' or first:
			st += i
			first = True
	last = 0
	for i in st[::-1]:
		if i == '1':
			break
		else:
			last += 1
	if st != '':
		print(answer())
	else:
		print(0)


import sys
T = int(input())
for _ in range(T):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	ipos = -1
	fpos = -1
	for i in range(n):
		if s[i] == '1':
			ipos = i
			break
	for i in range(n):
		if s[n - i - 1] == '1':
			fpos = n - i
			break
	ans = 0
	if ipos == -1:
		ans = 0
		print(ans)
	else:
		ans = a
		i = ipos
		while i < fpos:
			c = 0
			while s[i] == '0':
				c = c + 1
				i = i + 1
			if c * b < a:
				ans = ans + c * b
			else:
				ans = ans + a
			i = i + 1
		print(ans)


n = int(input())
for i in range(0, n):
	o = input().rstrip().split(' ')
	s = input().rstrip()
	x = list(s)
	a = int(o[0])
	b = int(o[1])
	if len(list(set(x))) == 1:
		if x[0] == '0':
			print(0)
		else:
			print(a)
	else:
		A = 0
		L = []
		Q = []
		B = 0
		for j in range(0, len(x)):
			if x[j] == '1':
				A += 1
				if B > 0:
					Q.append(B)
				B = 0
			else:
				if A > 0:
					L.append(A)
				B += 1
				A = 0
		if A > 0:
			L.append(A)
		if x[0] == '0':
			del Q[0]
		if len(L) == 1:
			print(a)
		elif 1:
			if 1:
				A = a // b
				S = 0
				H = 0
				P = -1
				ans = 0
				while 1:
					S = 0
					H = 0
					for j in range(0, len(Q)):
						if Q[j] <= A:
							S += Q[j]
							H += 1
						else:
							break
					if S != 0:
						ans += S * b
						ans += a
						P = 0
					else:
						P = 1
						ans += a
					for j in range(0, H + 1):
						del L[0]
					for j in range(0, H + 1):
						if len(Q) == 0:
							break
						del Q[0]
					if len(Q) == 0:
						break
				if len(L) != 0:
					ans += a
				print(ans)


for _ in range(int(input())):
	(a, b) = [int(i) for i in input().split()]
	s = input()
	l = []
	n = len(s)
	x = s.count('1')
	if x == 1:
		print(a)
	elif x == 0:
		print(0)
	else:
		found = 0
		if s[0] == '1' and s[1] == '0':
			l.append([0, 0])
		for i in range(1, n):
			if i != n - 1 and s[i] == '1' and (s[i - 1] == '0') and (s[i + 1] == '0'):
				l.append([i, i])
				continue
			if s[i - 1] == '1' and s[i] == '1':
				if found == 0:
					found += 2
					st = i - 1
				else:
					found += 1
			else:
				if found > 0:
					l.append([st, i - 1])
				found = 0
		if n > 2:
			if s[n - 1] == '1' and s[n - 2] == '0':
				l.append([n - 1, n - 1])
		if found > 0:
			l.append([st, n - 1])
		if len(l) == 1:
			print(a)
		elif len(l) == 0:
			print(0)
		else:
			ans = 0
			for i in range(1, len(l)):
				l1 = l[i]
				l2 = l[i - 1]
				if (l1[0] - l2[1] - 1) * b > a:
					ans += a
				else:
					ans += (l1[0] - l2[1] - 1) * b
			ans += a
			print(ans)


t = int(input())
itog = []
for l in range(t):
	(a, b) = list(map(int, input().split()))
	mass = input()
	dp = []
	zero = 0
	if mass[0] == '0':
		zero += 1
		dp.append(0)
	else:
		dp.append(a)
	for i in range(1, len(mass)):
		if mass[i - 1] == '0' and mass[i] == '1':
			if dp[i - 1] == 0:
				dp.append(a)
			else:
				dp.append(min(zero * b + dp[i - 1], dp[i - 1] + a))
		elif mass[i - 1] == '1' and mass[i] == '0':
			dp.append(dp[i - 1])
			zero = 1
		elif mass[i - 1] == '0' and mass[i] == '0':
			dp.append(dp[i - 1])
			zero += 1
		else:
			dp.append(dp[i - 1])
	itog.append(dp[-1])
for i in itog:
	print(i)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations, combinations

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = 998244353
for t in range(int(data())):
	(a, b) = mdata()
	s = list(data())
	ans = 0
	ind = -INF
	for i in range(len(s)):
		if s[i] == '1':
			if i - ind - 1 > 0:
				if (i - ind - 1) * b < a:
					ans += (i - ind - 1) * b
				else:
					ans += a
			ind = i
	out(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	a1 = []
	f = False
	n1 = 0
	cost = a
	for c in s:
		if c == '1':
			if n1 != 0:
				a1.append(n1)
				n1 = 0
			f = True
		elif f:
			n1 += 1
	if a1 == []:
		if f:
			print(cost)
		else:
			print(0)
	else:
		for i in a1:
			if i * b < a:
				cost += i * b
			else:
				cost += a
		print(cost)


for _ in range(int(input())):
	ans = 0
	(a, b) = map(int, input().split())
	s = input()
	pref = '0'
	arr = []
	x = 0
	for i in range(len(s)):
		if s[i] == '0':
			if pref == '1':
				x = 1
			else:
				x += 1
		elif pref == '0':
			arr.append(x)
		pref = s[i]
	if len(arr) == 0:
		print(0)
	else:
		arr.pop(0)
		if len(arr) == 0:
			print(a)
		else:
			ans += min(arr[0] * b + a, 2 * a)
			for k in range(1, len(arr)):
				ans += min(b * arr[k], a)
			print(ans)


from sys import *
for _ in range(int(input())):
	(n, m) = map(int, iter(stdin.readline().split()))
	s = input()
	(count, ans) = (10 ** 6, 0)
	for i in s:
		if i == '0':
			count += 1
		else:
			ans += min(n, m * count)
			count = 0
	stdout.write(str(ans) + '\n')


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().strip().split())
	string = input().strip()
	left = 0
	for i in range(len(string)):
		left = i
		if string[i] == '1':
			break
	string = string[left:]
	ans = 0 if string == '0' else a
	for i in range(len(string)):
		if string[i] == '0':
			left = i
			break
	string = string[left:]
	add = 0
	for i in string:
		if i == '1':
			ans = ans + min(add, a)
			add = 0
		else:
			add += b
	print(ans)


import io
import os
from collections import Counter, defaultdict, deque

def solve(A, B, S):
	chunks = []
	for x in S:
		if not chunks or chunks[-1][0] != x:
			chunks.append([x, 1])
		else:
			chunks[-1][1] += 1
	cost = 0
	while chunks:
		chunk = chunks.pop()
		if chunk[0] == '0':
			continue
		c = A
		if len(chunks) >= 2:
			if chunks[-1][1] * B < A:
				c = chunks[-1][1] * B
				chunks[-1].pop()
		cost += c
	return cost
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(A, B) = [int(x) for x in input().split()]
	S = input().decode().rstrip()
	ans = solve(A, B, S)
	print(ans)


import sys
input = iter(sys.stdin.read().splitlines()).__next__
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	mines = input()
	first_1 = mines.find('1')
	if first_1 < 0:
		print(0)
		continue
	consecutive_zeros = [0]
	for i in range(first_1 + 1, len(mines)):
		if mines[i] == '1' and mines[i - 1] == '0':
			consecutive_zeros.append(0)
		elif mines[i] == '0':
			consecutive_zeros[-1] += 1
	consecutive_zeros.pop()
	cost = a
	for num_zeros in consecutive_zeros:
		cost += min(b * num_zeros, a)
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	one = []
	ans = 0
	n = len(s)
	if s == '0' * n:
		print(0)
		continue
	ind = s.index('1')
	for i in range(ind, n):
		if s[i] == '0':
			ans += 1
		else:
			if ans:
				one.append(ans)
			ans = 0
	ans = 10 ** 12
	one.sort(reverse=True)
	ans = sum(one) * b + a
	su = sum(one)
	cnt = 1
	for i in one:
		su = su - i
		cnt += 1
		ans = min(ans, su * b + a * cnt)
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	ss = input()
	s = list(ss)
	i1 = ss.find('1')
	i2 = ss.rfind('1')
	if i1 == -1:
		print(0)
	else:
		ans1 = 0
		ans2 = 0
		kkk = 0
		ch = '1'
		while i1 <= i2:
			k = 0
			while i1 <= i2 and s[i1] == ch:
				i1 += 1
				k += 1
			if ch == '1':
				ans1 += 1
				ch = '0'
			else:
				ch = '1'
				if k * b < a:
					ans2 += 1
					kkk += k
		print(kkk * b + (ans1 - ans2) * a)


import math
import sys

def chek(m, b, c, li):
	for i in range(li):
		if m[i] + b[i] > c:
			return False
	return True
input = lambda : sys.stdin.readline().rstrip()
f = int(input())
for _ in range(f):
	(a, b) = list(map(int, input().split()))
	s = input()
	mas = []
	c = 1
	k = len(s)
	cur = 1
	while c != k:
		if s[c] == s[c - 1]:
			cur += 1
		elif len(mas) != 0:
			mas.append(cur)
			cur = 1
		elif s[c] == '0':
			mas.append(cur)
			cur = 1
		else:
			cur = 1
		c += 1
	if s[c - 1] == '1':
		mas.append(cur)
	ans = 0
	for i in range(len(mas)):
		if i % 2 == 0:
			ans += a
		elif a > b * mas[i]:
			ans += b * mas[i]
			ans -= a
	print(ans)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	res = []
	for (ch, g) in groupby(it):
		res.append((ch, len(list(g))))
	return res
t = N()
for i in range(t):
	(a, b) = RL()
	s = input()
	res = GP(s)
	ans = 0
	if res:
		if res[-1][0] == '0':
			res.pop()
		if res:
			ans = a
			n = len(res)
			for i in range(n - 3, -1, -2):
				ans = min(ans + a, ans + b * res[i + 1][1])
	print(ans)


t = int(input())
for _ in range(t):
	(act, place) = map(int, input().split())
	s = input()
	sz = len(s)
	num_zeroes = 0
	first_1 = s.find('1')
	strt = max(1, first_1)
	total_cost = act if first_1 != -1 else 0
	if total_cost == 0:
		print(0)
		continue
	for i in range(strt, sz):
		if s[i - 1] == '1' and s[i] == '0':
			num_zeroes = 1
			continue
		if s[i] == '0':
			num_zeroes += 1
			continue
		if s[i] == '1' and s[i - 1] == '0':
			total_cost += min(place * num_zeroes, act)
			num_zeroes = 0
	print(total_cost)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
from itertools import groupby
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	price = 0
	s = s.strip('0')
	segs = s.split('1')
	for seg in segs:
		if len(seg):
			price += min(a, len(seg) * b)
	print(price + (a if '1' in s else 0))


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	f = -1
	l = -1
	for i in range(len(s)):
		if s[i] == '1':
			f = i
			break
	for i in range(len(s)):
		if s[i] == '1':
			l = i
	if f == l and l == -1:
		print(0)
	else:
		ans = a
		while f < l:
			c = 0
			while s[f] == '0':
				c += 1
				f += 1
			ans += min(a, c * b)
			f += 1
		print(ans)


from sys import stdin, stdout
from collections import deque
import bisect
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))
T = int(input())
for _ in range(T):
	(a, b) = RL(int)
	s = R()
	zs = []
	flag = 0
	cnt = 0
	for i in range(len(s)):
		if s[i] == '0':
			cnt += 1
		else:
			flag = 1
			zs.append(cnt)
			cnt = 0
	if s[0] == '0' and len(zs) > 0:
		zs.pop(0)
	mi = a // b
	if flag:
		ans = a
	else:
		ans = 0
	for i in range(len(zs)):
		if zs[i] > mi:
			ans += a
		else:
			ans += zs[i] * b
	print(ans)


def saving(a, b, s):
	s = s.strip('0')
	if len(s) == 0:
		return 0
	ans = 0
	for x in s.split('1'):
		ans += min(a, b * len(x))
	return ans + a
for i in range(int(input())):
	[a, b] = list(map(int, input().split()))
	l = input()
	print(saving(a, b, l))


from sys import stdin
from math import inf
input = lambda : stdin.readline().strip()
wtf = int(input())
for i in range(wtf):
	(a, b) = map(int, input().split())
	s = [0] + list(map(int, input()))
	n = len(s)
	dp = [[a, 0]]
	for i in range(1, n):
		if s[i]:
			dp.append([min(dp[-1][0], dp[-1][1] + a), inf])
		else:
			dp.append([min(dp[-1][0] + b, dp[-1][1] + a + b), min(dp[-1])])
	print(min(dp[-1]))


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	mines = list(input())
	location = dict()
	for i in range(len(mines)):
		if mines[i] == '1':
			location[i] = [1, 1]
	for j in range(len(mines)):
		if location.get(i):
			if location.get(i - 1):
				location[i][0] = 0
			if location.get(i + 1):
				location[i][1] = 0
	curr = 0
	segments = []
	last_stop = None
	for i in range(len(mines)):
		if location.get(i):
			if location[i][0] == 1:
				if last_stop is not None:
					segments.append(abs(last_stop - i) - 1)
				else:
					curr = a
			if location[i][1] == 1:
				last_stop = i
	for p in segments:
		curr += min(p * b, a)
	print(curr)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	j = 0
	while j < n and s[j] == '0':
		j += 1
	c = 0
	r = a
	if j == n:
		r = 0
	damp = 0
	for i in range(j, n):
		if s[i] == '1':
			if damp > 0:
				r += min(a, damp * b)
			damp = 0
		else:
			damp += 1
	print(r)


import sys
sys.setrecursionlimit(10 ** 6)
t = int(input())
while t:
	t -= 1
	(a, b) = list(map(int, input().split()))
	s = input()
	count = float('inf')
	ans = 0
	for char in range(len(s)):
		if s[char] == '0':
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	tot = 0
	n = len(s)
	tot = 0
	if b >= a:
		i = 0
		while i < n:
			if s[i] == '1':
				while i < n and s[i] == '1':
					i += 1
				tot += a
			i += 1
		print(tot)
	else:
		i = 0
		while i < n:
			if s[i] == '1':
				tot += a
				f = 0
				while i < n:
					if s[i] == '1':
						while i < n and s[i] == '1':
							i += 1
					else:
						last0 = i
						while i < n and s[i] == '0':
							i += 1
						if i == n:
							break
						else:
							c1 = b * (i - last0)
							if c1 <= a:
								tot += c1
							else:
								f = 1
								break
				if f == 1:
					continue
			i += 1
		print(tot)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for _ in range(n):
	l = inlt()
	a = l[0]
	b = l[1]
	s = insr()
	gap = a // b
	cost = 0
	zero_cnt = 0
	idx = 0
	unbombed = 0
	for x in s:
		if x == '0' and idx != 0:
			zero_cnt += 1
			if zero_cnt > gap and unbombed:
				cost += a
				unbombed = 0
		elif x == '1':
			unbombed = 1
			if zero_cnt <= gap and idx != 0:
				cost += b * zero_cnt
			zero_cnt = 0
			idx = 1
	if unbombed:
		cost += a
	print(cost)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	(a, b) = RL()
	s = input().strip()
	n = len(s)
	k = a // b
	(l, r) = (0, n)
	while l < n and s[l] == '0':
		l += 1
	while r > 0 and s[r - 1] == '0':
		r -= 1
	if l >= r:
		print(0)
	else:
		data = []
		tmp = 1
		for i in range(l + 1, r):
			if s[i] != s[i - 1]:
				data.append(tmp)
				tmp = 1
			else:
				tmp += 1
		data.append(tmp)
		res = 0
		i = 0
		while i < len(data):
			res += a
			i += 1
			while i < len(data) and data[i] <= k:
				res += b * data[i]
				i += 2
			i += 1
		print(res)


def RL():
	return map(int, input().split())

def RLL():
	return list(map(int, input().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	(a, b) = RL()
	s = input()
	c = 0
	blow = 0
	num = 0
	blow_1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			if blow == 0:
				continue
			else:
				num += 1
		elif num > 0:
			if b * num < a:
				c += b * num
				num = 0
			else:
				num = 0
				c += a
		elif blow == 0:
			blow = 1
			c += a
	print(c)


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(a1, b1) = map(int, input().split())
	s = list(input())
	i = 0
	la = []
	while i < len(s):
		j = i
		if s[i] == '1':
			while i < len(s) and s[i] == '1':
				i += 1
			la.append([i - j, j, i - 1])
		else:
			i += 1
	if len(la) == 1:
		print(a1)
		continue
	if len(la) == 0:
		print(0)
		continue
	ans = 0
	for i in range(1, len(la)):
		(a, b, c) = la[i - 1]
		(x, y, z) = la[i]
		if b1 * (y - c - 1) < a1:
			ans += b1 * (y - c - 1)
		else:
			ans += a1
	ans += a1
	print(ans)


from sys import stdin
ip = stdin.readline
for _ in range(int(ip())):
	(a, b) = map(int, ip().split())
	s = ip()
	n = len(s)
	i = 0
	while i < n and (not s[i] == '1'):
		i += 1
	if i == n:
		print('0')
		continue
	cnt0 = 0
	ans = a
	last = '1'
	for j in range(i + 1, n):
		if s[j] == '0':
			cnt0 += 1
		elif s[j] == '1' and (not s[j] == last):
			ans += min(a, cnt0 * b)
			cnt0 = 0
		last = s[j]
	print(ans)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	j = 0
	is1 = False
	dp = []
	start = 0
	for j in range(len(s)):
		if is1 and s[j] == '0':
			start = j
			is1 = False
		elif not is1 and s[j] == '1':
			if dp:
				dp.append([min(dp[-1]) + a, min(dp[-1]) + (j - start) * b])
			else:
				dp.append([a, a])
			start = j
			is1 = True
	if dp:
		print(min(dp[-1]))
	else:
		print(0)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	(a, b) = ma()
	s = st()
	s = s.rstrip('0')
	s = s[::-1]
	s = s.rstrip('0')
	s += '#'
	sr = []
	tu = 0
	p = s[0]
	ci = 0
	me = 0
	co = 0
	for i in range(len(s)):
		if p == s[i]:
			ci += 1
			continue
		sr.append(ci)
		ci = 1
		p = s[i]
		if tu % 2 == 0:
			if len(sr) >= 2:
				if sr[-2] * b < a:
					co += sr[-2] * b
					me += 1
		tu += 1
	ff = 0
	for i in range(len(sr)):
		if i % 2 == 0:
			ff += 1
	co += (ff - me) * a
	print(co)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split(' '))
	mines = list(input())
	stretch_fill_cost = 0
	start_filling = 0
	cost = 0
	for (i, e) in enumerate(mines):
		if e == '0':
			if i == 0:
				continue
			if mines[i - 1] == '0' and start_filling:
				stretch_fill_cost += b
			if mines[i - 1] == '1':
				start_filling = 1
				stretch_fill_cost += b
				cost += a
		if e == '1':
			if i > 0 and mines[i - 1] == '0':
				if start_filling:
					start_filling = 0
					if stretch_fill_cost < a:
						cost += stretch_fill_cost
						cost -= a
					stretch_fill_cost = 0
			if i == len(mines) - 1:
				cost += a
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	arr = list(input())
	n = len(arr)
	if arr == ['0'] * n:
		print(0)
	else:
		one_check = int(arr[0])
		for i in range(n - 1):
			if arr[i] == '0' and arr[i + 1] == '1':
				one_check += 1
		check = []
		i = 0
		while i < n - 1:
			if arr[i] == '1' and arr[i + 1] == '0':
				check.append(0)
				i += 1
				while i < n and arr[i] == '0':
					check[-1] += 1
					i += 1
				i -= 1
			i += 1
		if arr[-1] == '0':
			check.pop(-1)
		if not arr:
			print(a)
		else:
			x = len(check)
			ans = 100000000000000
			k = 0
			check.sort()
			for j in range(x):
				k += check[j]
				temp = k * b + (one_check - j - 1) * a
				ans = min(ans, temp)
			print(min(ans, one_check * a))


test = int(input())
while test > 0:
	test -= 1
	(a, b) = map(int, input().split())
	s = input()
	ones = []
	for i in range(len(s)):
		if s[i] == '1':
			ones.append(i)
	if len(ones) == 0:
		print(0)
		continue
	ans = a
	prev = ones[0]
	ones.pop(0)
	while len(ones) > 0:
		if a <= (ones[0] - prev - 1) * b:
			ans += a
		else:
			ans += (ones[0] - prev - 1) * b
		prev = ones.pop(0)
	print(ans)


for t in range(int(input())):
	(a, b) = map(int, input().split())
	x = list(map(int, input()))
	l = []
	for i in range(len(x)):
		if x[i] == 1:
			l.append(i + 1)
	ans = 0
	if len(l) <= 1:
		if len(l) == 0:
			print(0)
		else:
			print(a)
	else:
		ans = ans + a
		for i in range(1, len(l)):
			d = l[i] - l[i - 1] - 1
			if d * b < a:
				ans = ans + d * b
			else:
				ans = ans + a
		print(ans)


import sys
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def Solution(s, a, b):
	n = len(s)
	(l, r) = (0, n - 1)
	while l <= r:
		if s[l] == '1' and s[r] == '1':
			break
		if s[l] != '1':
			l += 1
		if s[r] != '1':
			r -= 1
	if l > r:
		print(0)
		return
	ans = a
	i = l
	while i < r:
		c = 0
		while i < r and s[i] == '0':
			i += 1
			c += 1
		ans += min(a, b * c)
		i += 1
	print(ans)

def main():
	for _ in range(get_int()):
		(a, b) = get_ints_in_variables()
		s = get_string()
		Solution(s, a, b)
main()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	v = input()
	c = 0
	last = '1'
	s = ''
	f = 0
	for i in v:
		if f == 1:
			s += i
		if i == '1':
			f = 1
			s += i
	if '1' in s:
		ans = a
	else:
		ans = 0
	n = len(s)
	for i in range(1, n):
		if s[i] == '0':
			c += 1
			last = '0'
		else:
			if last == '0':
				if c * b < a:
					ans += c * b
				else:
					ans += a
			c = 0
			last = '1'
	print(ans)


def get_ans(a, b, mines):
	ans = 0
	comps = []
	n = len(mines)
	for i in range(1, n):
		if mines[i] == '0' and mines[i - 1] == '1':
			comps.append(i - 1)
		if mines[i] == '1' and mines[i - 1] == '0' and comps:
			l = i - comps[-1] - 1
			if l * b + a < 2 * a:
				ans += l * b
				del comps[-1]
	if mines[-1] == '1':
		comps.append(n - 1)
	return ans + a * len(comps)
anss = []
t = int(input())
for _ in range(t):
	inp = list(map(int, input().split()))
	mns = input()
	anss.append(get_ans(inp[0], inp[1], mns))
for i in anss:
	print(i)


t = int(input())
while t:
	t -= 1
	(act, plc) = map(int, input().split())
	s = input()
	a = []
	m = i = 0
	n = len(s)
	while i < n:
		if s[i] == '1':
			j = i
			while i < n and s[i] == '1':
				i += 1
			a.append([j, i - 1])
			m += act
		else:
			i += 1
	i = 0
	while i < len(a) - 1:
		k = m - 2 * act + act + plc * (a[i + 1][0] - a[i][1] - 1)
		if k < m:
			m = k
			a[i] = [a[i][0], a[i + 1][1]]
			a.pop(i + 1)
			i -= 1
		i += 1
	print(m)


import os, io, sys, time, collections, math, pprint, itertools as it, operator as op, bisect as bs, functools as fn
(maxx, localsys, mod) = (float('inf'), 0, int(1000000000.0 + 7))
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)
ceil = lambda n, x: (n + x - 1) // x
(osi, oso) = ('/home/priyanshu/Documents/cp/input.txt', '/home/priyanshu/Documents/cp/output.txt')
if os.path.exists(osi):
	sys.stdin = open(osi, 'r')
	sys.stdout = open(oso, 'w')
input = sys.stdin.readline

def maps():
	return map(int, input().split())
for _ in range(int(input())):
	(A, B) = maps()
	a = input().strip('\n')
	(ans, cnt) = (0, maxx)
	for i in a:
		if i == '0':
			cnt += 1
		else:
			ans += min(A, cnt * B)
			cnt = 0
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		(a, b) = LI()
		s = S()
		c = 0
		if s[0] == '1':
			c += 1
		l = len(s)
		for i in range(1, l):
			if s[i] == '1' and s[i - 1] == '0':
				c += 1
		r = a * c
		for i in range(l - 2):
			if s[i:i + 2] != '10':
				continue
			ok = 0
			d = 1
			for j in range(i + 2, l):
				if s[j] == '1':
					ok = 1
					break
				d += 1
			if ok and a > b * d:
				r -= a - b * d
		rr.append(r)
	return JA(rr, '\n')
print(main())


t = int(input())
while t:
	(d, b) = [int(x) for x in input().split()]
	s = list(input())
	a = []
	q = s[0]
	k = 1
	for i in s[1:]:
		if i == q:
			k += 1
		else:
			a.append(q * k)
			k = 1
			q = i
	a.append(q * k)
	for i in range(len(a)):
		if a[i][0] == '1':
			a[i] = 'A'
		if a[i][0] == '0':
			a[i] = len(a[i])
	if len(a) == 1 and a[0] != 'A':
		print(0)
		t -= 1
		continue
	if a[0] != 'A':
		a = a[1:]
	if a[-1] != 'A':
		a = a[:-1]
	zak = 0
	for i in range(len(a)):
		if a[i] != 'A':
			if a[i] * b > d:
				zak += d
			else:
				zak += a[i] * b
	zak += d
	print(zak)
	t -= 1


for t in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	cnt = cost = i = 0
	flag = False
	while i < len(s):
		if s[i] == '0':
			cnt += 1
		else:
			if flag:
				cost += min(cnt * b, a)
			else:
				cost = a
				flag = True
			while i < len(s) and s[i] == '1':
				i += 1
			i -= 1
			cnt = 0
		i += 1
	print(cost)


import sys
tests = int(sys.stdin.readline())
for _ in range(tests):
	(a, b) = list(map(int, sys.stdin.readline().split()))
	line = sys.stdin.readline().strip().strip('0')
	groups = 0
	cost = 0
	count = 0
	if not '1' in line:
		print('0')
		continue
	counted = True
	for x in line:
		if x == '0':
			count += 1
			counted = False
		elif not counted:
			counted = True
			if count and count * b < a:
				cost += count * b
				count = 0
			else:
				cost += a
				count = 0
	print(cost + a)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = list(input())
	i = 0
	l = len(s)
	m = 0
	while i < l:
		if s[i] == '0':
			i += 1
		else:
			j = i + 1
			temp = 0
			while j < l:
				if s[j] == '0':
					j += 1
					temp += 1
				else:
					break
			if j != l:
				if temp * b <= a:
					m += temp
					for x in range(i + 1, j):
						s[x] = '1'
				i = j
			else:
				break
	n = 0
	i = 0
	while i < l:
		if s[i] == '0':
			i += 1
		else:
			n += 1
			j = i + 1
			while j < l:
				if s[j] == '1':
					j += 1
				else:
					i = j
					break
			if j == l:
				break
	ans = n * a + m * b
	print(ans)


def solve():
	(a, b) = map(int, input().split())
	s = input()
	lst = -1
	dist = []
	cost = 0
	for i in range(len(s)):
		if s[i] == '1':
			if lst == -1 or lst == i - 1:
				pass
			else:
				dist.append(i - lst - 1)
			lst = i
	if lst != -1:
		cost = a
	for d in dist:
		if d * b <= a:
			cost += d * b
		else:
			cost += a
	print(cost)
for _ in range(int(input())):
	solve()


for t in range(int(input())):
	ab = list(map(int, input().split()))
	city = str(input())
	last = -1
	cost = 0
	lr = []
	i = 0
	while i < len(city):
		if i == 0 and city[i] == '1':
			lr.append([0])
		elif i != 0:
			if city[i] == '0' and city[i - 1] == '1':
				lr[-1].append(i)
			elif city[i] == '1' and city[i - 1] == '0':
				lr.append([i])
		i += 1
	if len(lr) > 0 and len(lr[len(lr) - 1]) == 1:
		lr[len(lr) - 1].append(i)
	otr = len(lr)
	for i in range(1, len(lr)):
		if (lr[i][0] - lr[i - 1][1]) * ab[1] <= ab[0]:
			otr -= 1
			cost += ab[1] * (lr[i][0] - lr[i - 1][1])
	cost += otr * ab[0]
	print(cost)


def solve(A, a, b):
	ans = 0
	j = -1
	bomb_set = False
	for i in range(0, len(A)):
		if A[i] == '1':
			if i == 0 or A[i - 1] == '0':
				if not bomb_set:
					ans += a
					bomb_set = True
				else:
					ans += min((i - j - 1) * b, a)
			j = i
	return ans

def main(input_path=0):
	with open(input_path) as f:
		t = int(f.readline())
		for _ in range(t):
			(a, b) = [int(x) for x in f.readline().strip().split(' ')]
			A = f.readline().strip()
			print(solve(A, a, b))
main()


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = str(input())
	z = []
	if s.find('1') == -1:
		print(0)
		continue
	elif s.find('1') == s.rfind('1'):
		print(a)
		continue
	cnt = 0
	ca = 0
	for i in range(s.find('1') + 1, s.rfind('1') + 2):
		if i == len(s):
			ca += a
			break
		if s[i - 1] == '1' and s[i] == '0':
			ca += a
		if s[i] == '0':
			cnt += 1
		elif s[i] == '1' and s[i - 1] == '0':
			z.append(cnt)
			cnt = 0
	z.sort()
	if len(z) == 0:
		print(a)
		continue
	ans = ca
	for i in range(len(z)):
		ca += b * z[i] - a
		ans = min(ca, ans)
	print(ans)


t = int(input())
for case in range(t):
	(a, b) = [int(x) for x in input().split(' ')]
	s = input()
	n = len(s)
	for j in range(2):
		for i in range(len(s)):
			if s[i] != '0':
				break
		s = s[i:]
		s = s[::-1]
	if s == '0':
		s = ''
	y = [u for u in s.split('0') if u]
	c = len(y) * a
	x = [u for u in s.split('1') if u]
	for z in x:
		if len(z) * b < a:
			c -= a
			c += len(z) * b
	print(c)


import sys
import math
from collections import defaultdict, deque
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	t = int(input())
	for _ in range(t):
		(a, b) = inar()
		st = input().strip()
		n = len(st)
		ls = []
		count = 0
		l = 10 ** 9
		r = 0
		for i in range(n):
			if st[i] == '1':
				l = min(l, i)
				r = max(r, i)
				count += 1
			elif count:
				ls.append([l, r])
				count = 0
				l = 10 ** 9
				r = 0
		if count:
			ls.append([l, r])
			l = 10 ** 9
			r = 0
		arr = []
		for i in range(1, len(ls)):
			arr.append(ls[i][0] - ls[i - 1][1] - 1)
		ans = 0
		for i in range(len(arr)):
			if b * arr[i] < a:
				ans += arr[i] * b
			else:
				ans += a
		if len(ls) == 0:
			print(0)
		else:
			print(ans + a)
main()


import math
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = list(map(int, input()))
	new = []
	for i in range(len(s)):
		if s[i] == 1 and i == 0 or (s[i] == 1 and s[i - 1] != 1):
			new.append(1)
		elif s[i] == 0:
			new.append(0)
	dist = []
	for i in range(len(new)):
		if new[i] == 1:
			dist.append(i)
	su = 0
	for i in range(len(dist) - 1):
		diff = abs(dist[i + 1] - dist[i]) - 1
		if diff * b <= a:
			su += diff * b
		else:
			su += a
	su += a
	if len(set(s)) == 1 and s[0] == 0:
		print(0)
	else:
		print(su)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	mines = input()
	lent = len(mines)
	if mines.count('1') == 0:
		print(0)
		continue
	for i in range(lent):
		if mines[i] == '1':
			start = i
			break
	for i in range(lent - 1, -1, -1):
		if mines[i] == '1':
			end = i
			break
	mod = mines[start:end + 1]
	new = ''
	found = False
	for i in range(len(mod)):
		if mod[i] == '1' and (not found):
			new += mod[i]
			found = True
		if mod[i] == '0':
			new += mod[i]
			found = False
	coins_req = a
	start = -1
	end = -1
	zeros = 0
	found = False
	for i in range(len(new)):
		if new[i] == '0':
			zeros += 1
		else:
			coins_req += min(a, b * zeros)
			zeros = 0
	print(coins_req)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	k = -1
	for i in range(len(s)):
		if s[i] == '1':
			k = i
			break
	if k == -1:
		print(0)
	else:
		a1 = a
		f = 0
		for i in range(k + 1, len(s)):
			if s[i] == '1':
				a1 += min(a, f * b)
				f = 0
			else:
				f += 1
		print(a1)


import sys, os.path
from collections import *
from heapq import *
from copy import *
import math
mod = 10 ** 9 + 7
read = lambda : map(int, input().split())
inf = float('inf')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	(a, b) = read()
	s = input()
	n = len(s)
	if s.count('0') == n:
		print(0)
	else:
		first = -1
		last = -1
		for i in range(n):
			if s[i] == '1':
				first = i
				break
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				last = i
				break
		ans = a
		c = 0
		for i in range(first, last + 1):
			if s[i] == '0':
				c += 1
			else:
				ans += min(a, b * c)
				c = 0
		print(ans)


from sys import stdin
_input = stdin.readline
(_range, _str, _int, _len) = (range, str, int, len)

def solution():
	for _ in _range(_int(_input())):
		(a, b) = [_int(i) for i in _input().split()]
		s = _input().rstrip('\n').lstrip('0').rstrip('0')
		if _len(s) == 0:
			print(0)
			continue
		len_0 = 0
		ans = a
		for i in s:
			if i == '0':
				len_0 += 1
			else:
				if len_0 * b <= a:
					ans += len_0 * b
				else:
					ans += a
				len_0 = 0
		print(ans)
solution()


def main():
	inpt = input().split()
	a = int(inpt[0])
	b = int(inpt[1])
	s = input()
	if sum(list(map(int, s))) == 0:
		print(0)
		return
	elif sum(list(map(int, s))) == 1:
		print(a)
		return
	gaps = []
	for i in range(len(s)):
		if s[i] == '1':
			last = i
			break
	for i in range(len(s)):
		if s[i] == '1' and i - last >= 2:
			gaps.append(i - last - 1)
		if s[i] == '1':
			last = i
	gaps.sort()
	cost = 0
	if not gaps:
		print(a)
		return
	mines = gaps[0] * b
	calc = False
	while mines < a:
		cost += mines
		if len(gaps) == 1:
			calc = True
			cost = cost + a
			break
		else:
			del gaps[0]
			mines = gaps[0] * b
	if not calc:
		cost = cost + (len(gaps) + 1) * a
	print(cost)
n = int(input())
for i in range(n):
	main()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	data = list(map(int, input()))
	result = 0
	if 1 not in data:
		print(0)
		continue
	now = data.index(1)
	cur = now
	cnt = 0
	for i in range(now + 1, len(data)):
		if data[i] == 1:
			if cnt == 0:
				continue
			if 2 * a > a + cnt * b:
				for j in range(cur, i):
					data[j] = 1
				result += cnt * b
			cur = i
			cnt = 0
		else:
			cnt += 1
	if data[0] == 1:
		check = True
	else:
		check = False
	data.append(0)
	cnt = 0
	for i in range(1, len(data)):
		if data[i] == 0:
			if check:
				cnt += 1
			check = False
		else:
			check = True
	result += cnt * a
	print(result)


t = int(input())
while t:
	t -= 1
	p = 0
	q = 0
	c = 0
	(a, b) = map(int, input().split())
	s = input()
	l = list()
	for i in range(len(s)):
		if s[i] == '0':
			p += 1
		else:
			if i != 0 and s[i - 1] == '0':
				l.append(p)
				p = 0
			q += 1
	if s[0] == '0':
		l = l[1:]
	for m in l:
		c += min(b * m, a)
	if q:
		c += a
		print(c)
	else:
		print(c)


from collections import defaultdict
from functools import reduce
from bisect import bisect_right
from bisect import bisect_left
import copy

def main():
	for _ in range(int(input())):
		(a, b) = list(map(int, input().split()))
		d = defaultdict(int)
		l = list(map(int, ' '.join(input()).split()))
		arr = []
		c = 0
		ones = 0
		if l.count(1) == 0:
			print(0)
			continue
		start = l.index(1)
		end = len(l) - l[::-1].index(1)
		l.append(2)
		for i in range(start, end):
			if l[i] == 0:
				c += 1
			elif c != 0:
				arr.append(c)
				c = 0
			if l[i] == 1 and l[i + 1] == 0 or (l[i] == 1 and l[i + 1] == 2):
				ones += 1
		arr.sort()
		mini = ones * a
		for i in arr:
			mini = min(mini, mini - a + i * b)
		print(mini)
main()


t = int(input())
for _ in range(t):
	(a, b) = list(map(int, input().split()))
	s = input()
	n = len(s)
	ind1 = 0
	ind2 = n - 1
	while ind1 != n and s[ind1] == '0':
		ind1 += 1
	while ind2 != -1 and s[ind2] == '0':
		ind2 -= 1
	if ind1 == n:
		print(0)
		continue
	arr = []
	count = 0
	for i in range(ind1, ind2 + 1):
		if s[i] == '0':
			count += 1
		else:
			if count != 0:
				arr += [count]
			count = 0
	if count != 0:
		arr += [count]
	ans = a * (len(arr) + 1)
	arr.sort()
	tot = 0
	for i in range(len(arr)):
		tot += arr[i]
		ans = min(ans, b * tot + a * (len(arr) - i))
	print(ans)


import sys
t = int(input())
for test in range(t):
	[a, b] = list(map(int, input().split(' ')))
	s = input()
	ns = len(s)
	r = a / b
	prev = False
	nf = ns * a * b * 2
	nt = 0
	res = 0
	for i in range(ns):
		if s[i] == '1':
			if nt == 0:
				if nf < r:
					res = res + b * nf
				else:
					res += a
			nt += 1
			nf = 0
		else:
			nf += 1
			nt = 0
	print(res)


for _ in ' ' * int(input()):
	(a, b) = map(int, input().split())
	(c, ans) = (10 ** 6, 0)
	for x in input():
		if x == '0':
			c += 1
		else:
			ans += min(c * b, a)
			c = 0
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	if not s:
		print('0')
		continue
	cost = a
	end = len(s)
	i = 0
	while i < end:
		if s[i] == '1':
			i += 1
		else:
			cnt_zero = 0
			while i < end and s[i] == '0':
				i += 1
				cnt_zero += 1
			if cnt_zero * b <= a:
				cost += cnt_zero * b
			else:
				cost += a
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	res = []
	f = True
	c = 0
	for i in range(len(s)):
		if s[i] == '1':
			if f:
				res.append(1)
				f = False
		elif s[i] == '0':
			res.append(0)
			f = True
	if b >= a:
		print(res.count(1) * a)
	else:
		p = a // b
		ind = []
		for j in range(len(res)):
			if res[j] == 1:
				ind.append(j)
		ans = len(ind)
		cost = 0
		for i in range(1, len(ind)):
			if ind[i] - ind[i - 1] - 1 <= p:
				ans -= 1
				cost += (ind[i] - ind[i - 1] - 1) * b
		print(ans * a + cost)


from sys import stdin

def main():
	input = lambda : stdin.readline()[:-1]
	T = int(input())
	for _ in [0] * T:
		(A, B) = map(int, input().split())
		MAP = input().strip('0')
		(mp, prev) = ('', '')
		for s in MAP:
			if s == '1' and s == prev:
				continue
			mp += s
			prev = s
		ans = mp.count('1') * A
		zero = mp.strip('1').split('1')
		for n in zero:
			if n:
				ans = min(ans, ans - A + len(n) * B)
		print(ans)
main()


import sys
from collections import Counter

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))
sys.setrecursionlimit(20000)
T = int(input())
for _ in range(T):
	(a, b) = get_ints()
	M = input()
	min_cost = 0
	zero_count = 0
	start_counting = False
	for i in range(len(M)):
		if not start_counting:
			if M[i] == '1':
				start_counting = True
				min_cost += a
			continue
		if M[i] == '0':
			zero_count += 1
		elif zero_count == 0:
			min_cost += 0
		else:
			min_cost += min(zero_count * b, a)
			zero_count = 0
	print(min_cost)


t = int(input())
for tests in range(t):
	(a, b) = map(int, input().split())
	S = list(input().strip())
	while S and S[-1] == '0':
		S.pop()
	if '1' in S:
		S = S[S.index('1'):]
	if S == []:
		print(0)
	else:
		ANS = a
		NOW = 0
		for s in S:
			if s == '0':
				NOW += 1
			if s == '1' and NOW > 0:
				ANS += min(a, b * NOW)
				NOW = 0
		print(ANS)


from collections import Counter
from collections import OrderedDict
from collections import defaultdict
import bisect
import math

def freq_count(mylist):
	return Counter(mylist)

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	arr = [int(x) for x in input()]
	diff = a // b
	li0 = []
	co = 0
	arived = False
	for i in range(len(arr)):
		if arr[i] == 0:
			co += 1
		else:
			if co > 0:
				if arived:
					li0.append(co)
			co = 0
			arived = True
	li1 = []
	co = 0
	for i in range(len(arr)):
		if arr[i] == 1:
			co += 1
		else:
			if co > 0:
				li1.append(co)
			co = 0
	if co > 0:
		li1.append(co)
	ans = 0
	for each in li0:
		if each <= diff:
			ans += b * each
			li1.pop()
	ans += len(li1) * a
	print(ans)


import fileinput

def main():
	newline = fileinput.input()
	line = newline.readline().rstrip()
	tests = int(line)
	for test in range(tests):
		line = newline.readline().rstrip().split()
		aCost = int(line[0])
		pCost = int(line[1])
		mines = str(newline.readline().rstrip())
		start = False
		tCoins = 0
		pCoins = 0
		for i in range(len(mines)):
			if start:
				if mines[i] == '0':
					pCoins += pCost
				elif mines[i - 1] == '0':
					tCoins += min(aCost, pCoins)
					pCoins = 0
			elif mines[i] == '1':
				start = True
				tCoins += aCost
		print(tCoins)
main()


t = int(input())
for j in range(t):
	(a, b) = map(int, input().split())
	s = input()
	cnt = 0
	r = 0
	l = []
	for i in range(len(s)):
		if s[i] == '1':
			cnt += 1
		else:
			if cnt > 0:
				l.append((i - cnt, i - 1))
			cnt = 0
	if cnt > 0:
		l.append((len(s) - cnt, len(s) - 1))
	if len(l) == 0:
		print(0)
	else:
		f = l[0]
		ans = 0
		q = False
		for j in range(1, len(l)):
			if (l[j][0] - f[1] - 1) * b > a:
				ans += a
				f = l[j]
				q = False
			else:
				ans += (l[j][0] - f[1] - 1) * b
				f = l[j]
				q = True
		ans += a
		print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	l = []
	c0 = 0
	c1 = 0
	f1 = 0
	f0 = 0
	n = len(s)
	for i in range(n):
		if s[i] == '0':
			c0 += 1
			f0 = 1
		else:
			if c0:
				l.append(c0)
			c1 += 1
			f1 = 1
			c0 = 0
	if c0:
		l.append(c0)
	if f1 == 0:
		print(0)
		continue
	elif f0 == 0:
		print(a)
		continue
	if s[0] == '0':
		l = l[1:]
	if s[-1] == '0':
		l = l[:-1]
	if len(l) == 0:
		print(a)
	else:
		i = 0
		c = 0
		si = len(l)
		val = si + 1
		ans = a
		while i < si:
			if l[i] * b <= a:
				c += 2
				ans += l[i] * b
				i += 1
			else:
				c += 2
				ans += a
				i += 1
		if c < val:
			ans += a
		print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	gaps = input().strip('0').split('1')
	ans = 0 if len(gaps) == 1 else a
	for gap in gaps:
		if gap != '':
			ans += len(gap) * b if len(gap) * b <= a else a
	print(ans)


def solve(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	ones = s.count('1')
	ans = 0
	cnt = 0
	for i in range(n):
		if i > 0 and s[i - 1] == '1' and (s[i] == '0'):
			cnt = 1
		elif s[i] == '0' and cnt:
			cnt += 1
		elif s[i] == '1' and cnt:
			if a - b * cnt > 0:
				ans -= a - b * cnt
			cnt = 0
		if s[i] == '1' and (i == 0 or s[i - 1] == '0'):
			ans += a
	print(ans)

def main():
	T = int(input())
	for t in range(T):
		solve(t + 1)
main()


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(a, b) = map(int, stdin.readline().split())
	s = stdin.readline()[:-1]
	if b >= a:
		count = 0
		counter = 0
		for i in s:
			if i == '1':
				if counter == 0:
					count += 1
				counter = 1
			else:
				counter = 0
		print(count * a)
	else:
		res = []
		i = 0
		n = len(s)
		while i < n and s[i] == '0':
			i += 1
		counter = 0
		cost = 0
		prevcount = 10 ** 9
		while i < n:
			if s[i] == '1':
				while i < n and s[i] == '1':
					i += 1
				cost += a
			if i < n and s[i] == '0':
				count = 0
				while i < n and s[i] == '0':
					count += 1
					i += 1
				if i < n and s[i] == '1':
					if count * b < a:
						cost += count * b
						while i < n and s[i] == '1':
							i += 1
		print(cost)


import sys
input = sys.stdin.readline
num = int(input())
while num > 0:
	num = num - 1
	(a, b) = map(int, input().split())
	pp = input()
	ans = 0
	k = 0
	q = -1
	for i in range(len(pp)):
		if pp[i] == '1':
			q = 0
			if a > b * k:
				ans += b * k
			else:
				ans += a
			k = 0
		if k != 0:
			k += 1
		if i > 0 and pp[i - 1] == '1' and (pp[i] == '0'):
			k = 1
	if q == 0:
		ans += a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	city = str(input())
	ans = 0
	count = 10 ** 5
	for i in range(len(city)):
		if city[i] == '1':
			ans += min(count * b, a)
			count = 0
		else:
			count += 1
	print(ans)


t = int(input())
for case in range(t):
	(a, b) = list(map(int, input().split()))
	s = input()
	z = 10000
	total = 0
	act = False
	for i in range(len(s)):
		cur = s[i]
		if cur == '0':
			z += 1
			act = False
		elif not act:
			act = True
			total += min(a, b * z)
			z = 0
	print(total)


def cost(s, a, b):
	for i in range(len(s)):
		if s[i] == '1':
			start = i
			break
	else:
		return 0
	count = 0
	arr = []
	for i in range(start, len(s)):
		if s[i] == '0':
			count += 1
		elif count > 0:
			arr.append(count)
			count = 0
	c = (len(arr) + 1) * a
	for i in range(len(arr)):
		c = min(c, b * arr[i] + (c - a))
	return c
T = int(input())
for t in range(T):
	(a, b) = [int(i) for i in input().split(' ')]
	s = input()
	print(cost(s, a, b))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	gaps = []
	cnt = 0
	(i, j) = (0, 0)
	while i < len(s):
		while j < len(s) and s[i] == s[j]:
			j += 1
		if s[i] == '1':
			cnt += 1
		elif i > 0 and j < len(s):
			gaps.append(j - i)
		i = j
	ans = cnt * a
	for i in range(len(gaps)):
		if b * gaps[i] < a:
			ans += b * gaps[i] - a
	print(ans)


def helper(a, b, s):
	if not s:
		print(0)
		return
	arr = []
	c = 0
	for i in range(1, len(s)):
		if s[i] == '0':
			c += 1
		elif s[i - 1] == '0':
			arr.append(c)
			c = 0
	bombs = 0
	expl = 1
	for i in range(len(arr)):
		if arr[i] * b < a:
			bombs += arr[i]
		else:
			expl += 1
	print(expl * a + bombs * b)
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	helper(a, b, s)


import math, sys, os
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def minp():
	return map(int, sys.stdin.readline().rstrip().split())

def linp():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def inp():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(inp()):
	(a, b) = minp()
	s = list(map(int, input()))
	n = len(s)
	count = 999999999
	ans = 0
	for i in range(n):
		if s[i] == 0:
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = list(input())
	l = [i for i in range(len(s)) if s[i] == '1']
	if not l:
		print(0)
	else:
		f = l[0]
		last = l[-1]
		i = f
		ans = a
		p = s[f]
		c = 0
		while i <= last:
			if s[i] != p and s[i] == '1':
				ans += min(a, c)
				c = 0
			if s[i] == '0':
				c += b
			p = s[i]
			i += 1
		print(ans)


import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()[::-1]

def input():
	return _INPUT_LINES.pop()
res = []
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	if not s:
		res.append(0)
	else:
		s = [len(v) for v in s.split('1') if v]
		ans = a
		for v in s:
			if v * b < a:
				ans += v * b
			else:
				ans += a
		res.append(ans)
print('\n'.join(map(str, res)))


t = int(input())
for i in range(t):
	(a, b) = input().split()
	(a, b) = (int(a), int(b))
	mapa = input()
	if '1' not in mapa:
		print(0)
		continue
	mapa = mapa[mapa.index('1'):]
	mapa = mapa[::-1]
	mapa = mapa[mapa.index('1'):]
	mapa = mapa[::-1]
	sem = mapa.split('1')
	com = mapa.split('0')
	custofinal = 0
	custo1 = len(com) * a
	for i in sem:
		if len(i) * b + a <= 2 * a:
			custofinal += len(i) * b
		else:
			custofinal += a
	custofinal += a
	print(custofinal)


import math

def solve():
	(a, b) = map(int, input().split())
	mines = str(input())
	mines = mines.lstrip('0')
	if len(mines) == 0:
		return 0
	zeroes = 0
	exploded = True
	res = a
	for m in mines[1:]:
		if m == '1' and (not exploded):
			res += a if a < zeroes * b else b * zeroes
			exploded = True
			zeroes = 0
		elif m == '0':
			zeroes += 1
			exploded = False
	return res
for t in range(int(input())):
	print('{0}'.format(solve()))


from sys import stdin, stdout
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	arr = []
	p = -1
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			p = i
			break
	if p == -1:
		print(0)
		continue
	x = 0
	ans = 0
	c = 0
	idk = 0
	for i in range(n):
		if x:
			if s[i] == '0':
				c += 1
				idk = 1
			else:
				if idk:
					arr.append(c)
					c = 0
				idk = 0
				if i == p:
					break
		if s[i] == '1':
			x = 1
			if i == p:
				ans += a
				break
	if ans:
		print(ans)
		continue
	ans = a
	for i in arr:
		if i * b < a:
			ans += i * b
		else:
			ans += a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	z = list(filter(None, s.split('1')))
	o = list(filter(None, s.split('0')))
	c = 0
	cost = 0
	for num in z:
		if len(num) * b < a:
			c += 1
			cost += len(num) * b
	cost = (len(o) - c) * a + cost
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	arr = list(str(input()))
	(first, last) = (-1, -1)
	for i in range(len(arr)):
		if arr[i] == '1':
			first = i
			break
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] == '1':
			last = i
			break
	if first == last == -1:
		print(0)
	else:
		ans = a
		i = first
		while i < last:
			c = 0
			while arr[i] == '0':
				i += 1
				c += 1
			ans += min(b * c, a)
			i += 1
		print(ans)


import math, sys
from collections import defaultdict, Counter, deque
from functools import reduce
INF = float('inf')
MOD = int(1000000000.0) + 7

def solve():
	(a, b) = vars()
	s = input()
	n = len(s)
	(ans, z) = (0, INF)
	for i in range(n):
		if s[i] == '1':
			ans += min(a, b * z)
			z = 0
		else:
			z += 1
	print(ans)

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(str(sep))
		sys.stdout.write(str(arg))
		first = False
	sys.stdout.write(end)

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))
main()


cases = int(input())
for t in range(cases):
	(a, b) = list(map(int, input().split()))
	s = input()
	st = s.find('1')
	if st == -1:
		print(0)
	else:
		et = s.rfind('1')
		gap = []
		c = 0
		f = 1
		for i in range(st, et + 1):
			if s[i] == '0':
				c += 1
				f = 0
			else:
				if f == 0:
					gap.append(c)
					f = 1
				c = 0
		gap = sorted(gap)
		lg = len(gap)
		ones = lg + 1
		for i in range(1, lg):
			gap[i] = gap[i] + gap[i - 1]
		cost = [ones * a]
		for i in gap:
			ones -= 1
			cost.append(ones * a + i * b)
		print(min(cost))


rn = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rns = lambda : map(int, input().split())
rs = lambda : input()
yn = lambda x: print('Yes') if x else print('No')
YN = lambda x: print('YES') if x else print('NO')
for _ in range(rn()):
	(a, b) = rns()
	s = rs()
	if len(s) == 0:
		print(0)
	else:
		ids = [s[0]]
		accs = [0]
		for i in s:
			if i == ids[-1]:
				accs[-1] += 1
			else:
				ids.append(i)
				accs.append(1)
		ans = 0
		for i in range(len(ids)):
			if ids[i] == '0' and i > 0 and (i < len(ids) - 1):
				if a > b * accs[i]:
					ans += b * accs[i]
					ids[i] = '1'
		for i in range(len(ids)):
			if ids[i] == '1':
				if i == 0:
					ans += a
				elif i > 0 and len(ids) > 1 and (ids[i - 1] == '0'):
					ans += a
				elif len(ids) == 1:
					ans += a
		print(ans)


for tt in range(int(input())):
	(a, b) = map(int, input().split())
	s = [i for i in input()]
	for it in range(2):
		while len(s) and s[-1] == '0':
			s.pop()
		s = s[::-1]
	n = len(s)
	if n == 0:
		print(0)
		continue
	(cnt, has, seg) = (0, 0, [0])
	for i in s:
		if i == '1':
			has = 1
			seg.append(0)
		else:
			cnt += has
			has = 0
			seg[-1] += 1
	cnt += has
	ans = 0
	for i in seg:
		if i == 0:
			continue
		if cnt == 1:
			break
		if i * b + a <= a * 2:
			ans += i * b
			cnt -= 1
	print(ans + cnt * a)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	otr = []
	flag = False
	for i in range(len(s)):
		if flag:
			if s[i] == '0':
				otr[-1][1] = i - 1
				flag = False
		elif s[i] == '1':
			otr.append([i, 0])
			flag = True
	if len(otr) != 0:
		ans = a
	else:
		ans = 0
	for i in range(len(otr) - 1):
		ans += min(a, b * (otr[i + 1][0] - otr[i][1] - 1))
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = '0{}0'.format(input())
	n = len(s)
	ans = 0
	zero = 0
	for i in range(1, n - 1):
		if s[i - 1] == '0' and s[i] == '1':
			if ans == 0:
				ans += a
			else:
				ans += min(a, zero * b)
			zero = 0
		if s[i] == '0':
			zero += 1
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	(activate, implant) = [int(x) for x in input().split()]
	s = input()
	n = len(s)
	i = 0
	while i < n:
		if s[i] != '1':
			i += 1
		else:
			break
	s = s[i:]
	i = len(s) - 1
	while i >= 0:
		if s[i] != '1':
			i -= 1
		else:
			break
	s = s[:i + 1]
	i = 0
	ans = []
	n = len(s)
	while i < n - 1:
		if s[i] == '1':
			count = 0
			while s[i + 1] == '0':
				count += 1
				i += 1
			if count > 0:
				ans.append(count)
			i += 1
	if n > 0:
		result = activate
	else:
		result = 0
	for item in ans:
		result += min(activate, item * implant)
	if result == activate:
		result += 0
	print(result)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	begin = s.find('1')
	end = s.rfind('1')
	arr_1 = []
	arr_0 = []
	if begin == -1 or end == -1:
		print(0)
	elif begin == end:
		print(a)
	else:
		first_1 = 0
		c = 1
		ans = [0] * len(s)
		i = begin + 1
		ans[i - 1] = a
		while i < end + 1:
			if s[i] == '1':
				ans[i] = ans[i - 1]
				i += 1
			else:
				for j in range(i, end + 1):
					if s[j] == '1':
						first_1 = j
						break
				for z in range(i, first_1):
					ans[z] = ans[i - 1]
				x = first_1 - i
				ans[first_1] = min(ans[first_1 - 1] + b * x, ans[first_1 - 1] + a)
				if ans[first_1] == (c + 1) * a:
					c += 1
				i = first_1 + 1
		print(max(ans))


for i in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	ind = []
	for j in range(len(s)):
		if s[j] == '1':
			ind.append(j + 1)
	var = []
	for j in range(len(ind) - 1):
		if ind[j + 1] - ind[j] != 1:
			var.append(ind[j + 1] - ind[j] - 1)
	answer = []
	ans = 0
	for j in range(len(var)):
		if var[j] * b <= a:
			ans += var[j] * b
		else:
			answer.append(ans + a)
			ans = 0
		if j == len(var) - 1:
			answer.append(ans + a)
	if s.count('1') == 0:
		print(0)
	elif len(answer) == 0:
		print(a)
	else:
		print(sum(answer))


def saving_the_city():
	t = int(input())
	for _ in range(t):
		(a, b) = [int(x) for x in input().split(' ')]
		building = input()
		start = 0
		end = len(building)
		for i in range(len(building)):
			if building[i] == '0':
				start = i + 1
			else:
				break
		for i in range(len(building) - 1, -1, -1):
			if building[i] == '0':
				end = i
			else:
				break
		building += '0'
		zeros = []
		ones = []
		counter_z = 0
		counter_o = 0
		for i in range(start, end + 1, 1):
			if building[i] == '1' or i == end:
				if counter_z > 0:
					zeros += [counter_z]
					counter_z = 0
			else:
				counter_z += 1
			if building[i] == '0' or i == end:
				if counter_o > 0:
					ones += [counter_o]
					counter_o = 0
			else:
				counter_o += 1
		output = len(ones) * a
		for x in zeros:
			if x * b < a:
				output = output - a + x * b
		print(output)
saving_the_city()


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input().strip()
	s = s.strip('0')
	n = len(s)
	q = 0
	re = []
	if '1' not in s:
		print(0)
		continue
	for j in range(n):
		if s[j] == '0':
			q = q + 1
		elif q > 0:
			re.append(q)
			q = 0
	m = 1
	r = 0
	for v in re:
		if v * b >= a:
			m = m + 1
		else:
			r = r + v
	print(m * a + r * b)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	arr = list(map(int, list(input()))) + [0]
	n = len(arr)
	now = 0
	lastEnds = 0
	fl = False
	lenghts = []
	dists = []
	for i in range(n):
		if fl and (not arr[i]):
			if len(lenghts):
				dists.append(i - lastEnds - now)
			lenghts.append(now)
			fl = False
			now = 0
			lastEnds = i
		elif fl and arr[i]:
			now += 1
		elif not fl and arr[i]:
			fl = True
			now = 1
	price = a
	if len(lenghts) == 0:
		price = 0
	for i in range(len(dists)):
		if dists[i] * b < a:
			price += dists[i] * b
		else:
			price += a
	print(price)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	building = input()
	count = 10 ** 6
	accum = 0
	for i in building:
		if i == '0':
			count += 1
		else:
			accum += min(a, count * b)
			count = 0
	print(accum)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for i in range(inp()):
	(a, b) = ip()
	s = input().strip()
	ans = 0
	prev = -1
	z = 0
	flag = 0
	for i in s:
		if prev != '1' and i == '1':
			if flag:
				ans += min(a, b * z)
				z = 0
			else:
				ans += a
				flag = 1
				z = 0
		elif i == '0':
			z += 1
		prev = i
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from itertools import groupby

def main():
	for _ in range(int(input())):
		(ca, cp) = map(int, input().split(' '))
		s = input()
		a = [[x, len(list(y))] for (x, y) in groupby(s)]
		n = len(a)
		if n == 1 and a[0][0] == '0':
			print(0)
		else:
			if a[n - 1][0] == '0':
				a.pop(n - 1)
			if a[0][0] == '0':
				a.pop(0)
			ans = ca
			n = len(a)
			x = 0
			while x < n:
				if a[x][0] == '0':
					if a[x][1] * cp <= ca:
						ans += a[x][1] * cp
						x += 2
					else:
						ans += ca
						x += 1
				else:
					x += 1
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(cost1, cost2) = value()
	a = [int(i) for i in input()]
	n = len(a)
	have = []
	i = 0
	last = -1
	while i < n:
		if a[i] == 0:
			i += 1
		else:
			st = i
			i += 1
			while i < n and a[i]:
				i += 1
			if last == -1:
				last = st
			have.append(st - last)
			last = i
	ans = 0
	if len(have):
		ans += cost1
	for i in range(1, len(have)):
		ans += min(have[i] * cost2, cost1)
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	mines = input().strip('0')
	ans = 0
	zero = 0
	flag = 0
	if len(mines) == 0:
		ans = 0
	else:
		for i in mines:
			if i == '0':
				zero += 1
			elif i == '1':
				if flag == 1:
					ct = b * zero
					ans += min(a, ct)
					zero = 0
				else:
					ans += a
					flag = 1
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	s += '0'
	x = 0
	if s.count('0') == len(s):
		print(0)
		s = ''
	for i in range(len(s)):
		if s[i] == '1':
			x = i
			break
	ans = 0
	for i in range(x, len(s)):
		if s[i] == '0':
			x = i
			ans += a
			break
	c = 0
	while x < len(s):
		if s[x] == '1':
			ans += min(c * b, a)
			c = 0
			x += 1
		elif s[x] == '0':
			c += 1
			x += 1
	if len(s) > 0:
		print(ans)


def solve():
	(a, b) = [int(v) for v in input().split()]
	s = list(input())
	n = len(s)
	lst1 = len(s) + 1
	cst = 0
	for i in range(n):
		if s[i] == '1':
			nz = i - lst1 - 1
			lst1 = i
			if nz <= 0:
				continue
			if nz * b + a < 2 * a:
				cst += nz * b
				j = i - 1
				while j >= 0 and s[j] == '0':
					s[j] = '1'
					j -= 1
	g1 = 0
	for i in range(n):
		if s[i] == '1':
			g1 += 1
			if i and s[i - 1] == '1':
				g1 -= 1
	print(g1 * a + cst)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
cases = []
for i in range(t):
	case = []
	case = [int(p) for p in input().split()]
	case.append(input())
	cases.append(case)
for case in cases:
	activate = case[0]
	place = case[1]
	buildings = list(case[2])
	if '1' not in buildings:
		print(0)
	else:
		mines = 0
		places = 0
		b_l = 0
		while buildings[b_l] == '0':
			b_l += 1
		b_r = len(buildings) - 1
		while buildings[b_r] == '0':
			b_r -= 1
		buildings = buildings[b_l:b_r + 1]
		i = 0
		in_chain = False
		gaps = []
		gap_size = 0
		while i < len(buildings):
			if buildings[i] == '0':
				if in_chain:
					in_chain = False
					mines += 1
					gap_size = 1
				else:
					gap_size += 1
			if buildings[i] == '1':
				if not in_chain:
					in_chain = True
					if gap_size > 0:
						gaps.append(gap_size)
			i += 1
		if in_chain:
			mines += 1
		elif gap_size > 0:
			gaps.append(gap_size)
		for gap in gaps:
			if gap * place < activate:
				mines -= 1
				places += gap
		print(mines * activate + places * place)


t = int(input())
for _ in range(t):
	out = 0
	(detonate, plant) = [int(i) for i in input().split()]
	a = [int(i) for i in list(input())]
	if sum(a) == 0:
		print(0)
		continue
	coef = detonate / plant
	(start, end) = (0, len(a) - 1)
	while a[start] == 0:
		start += 1
	while a[end] == 0:
		end -= 1
	zeros = 0
	for i in range(start, end + 1):
		if a[i] == 1:
			if zeros <= coef:
				for j in range(i - zeros, i):
					a[j] = 1
				out += zeros * plant
			zeros = 0
		else:
			zeros += 1
	f = True
	dets = 1
	for i in range(start, end + 1):
		if a[i] == 1:
			f = True
		if a[i] == 0 and f:
			dets += 1
			f = False
	out += dets * detonate
	print(out)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	s = s.strip('0')
	l = []
	flag = 1
	for i in s:
		if i == '1' and flag == 1:
			l.append(1)
			flag = 0
		elif i == '1':
			continue
		else:
			l.append(0)
			flag = 1
	i = 0
	n = len(l)
	f = -1
	s = -1
	flag = 1
	ans = 0
	prev = True
	flag1 = 0
	while i < n:
		flag1 = 1
		if l[i] == 1 and flag == 1:
			f = i
			flag = 0
		elif l[i] == 1 and flag == 0:
			s = i
			flag = 1
			p = (s - f - 1) * b
			if p <= a:
				prev = True
				ans += p
				f = s
				flag = 0
				s = -1
			else:
				prev = False
				ans += a
				f = s
				s = -1
				flag = 0
		i += 1
	if prev == True and flag1 == 1 or (f != -1 and s == -1):
		ans += a
	print(ans)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	(a, b) = MI()
	s = list(SI())
	x = []
	y = []
	count = 1
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			count += 1
		else:
			if s[i - 1] == '0':
				y.append(0)
			else:
				y.append(1)
			x.append(count)
			count = 1
	if len(s) != 0 and s[-1] == '1':
		y.append(1)
		x.append(count)
	if len(y) != 0 and y[0] == 0:
		y.pop(0)
		x.pop(0)
	y1 = []
	ans = 0
	for i in range(len(y)):
		if y[i] == 0:
			y1.append(x[i])
		else:
			ans += a
	for i in y1:
		if i * b < a:
			ans -= a
			ans += i * b
	print(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi, ceil, log, inf, gcd
from itertools import permutations
from copy import deepcopy
from heapq import *
from sys import setrecursionlimit

def main():
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		s = list(input().rstrip())
		n = len(s)
		if s.count('0') == n:
			print(0)
			continue
		i = 0
		l = []
		while i < n:
			if s[i] == '0':
				j = i + 1
				while j < n and s[j] == '0':
					j += 1
				if i != 0 and j != n:
					l.append(j - i)
				i = j
			i += 1
		ans = a
		for i in range(len(l)):
			if b * l[i] < a:
				ans += b * l[i]
			else:
				ans += a
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	cost = input().split()
	a = int(cost[0])
	b = int(cost[1])
	s = input()
	first = s.find('1')
	last = s.rfind('1')
	if first == -1 and last == -1:
		print(0)
		continue
	ans = a
	i = first
	while i < last:
		c = 0
		while s[i] == '0':
			c += 1
			i += 1
		ans += min(a, b * c)
		i += 1
	print(ans)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = []
	k = input()
	n = len(k)
	for j in k:
		s.append(int(j))
	res = []
	c = -1
	for j in range(n):
		if s[j] == 0:
			res.append(j - c - 1)
			c = j
	if len(res) == 0:
		print(a)
	else:
		l = len(res)
		dp = [[float('inf'), float('inf')] for i in range(l)]
		if res[0] > 0:
			dp[0][0] = a
		else:
			dp[0][0] = 0
		dp[0][1] = b + a
		j = 1
		while j < l:
			if res[j] == 0:
				dp[j][0] = min(dp[j - 1][0], dp[j - 1][1])
				dp[j][1] = min(dp[j][1], dp[j - 1][0] + b + a, dp[j - 1][1] + b)
			else:
				dp[j][0] = min(dp[j - 1][0] + a, dp[j - 1][1])
				dp[j][1] = min(dp[j][1], dp[j - 1][0] + a + b, dp[j - 1][1] + b)
			j += 1
		if s[-1] == 0:
			print(min(dp[-1][0], dp[-1][1]))
		else:
			print(min(dp[-1][0] + a, dp[-1][1]))


t = int(input())
while t != 0:
	(a, b) = list(map(int, input().split()))
	lst = input()
	s = 0
	n = len(lst)
	while s < n and lst[s] == '0':
		s = s + 1
	f = n - 1
	while f >= 0 and lst[f] == '0':
		f = f - 1
	lst = lst[s:f + 1]
	n = f + 1 - s
	i = 0
	ans = 0
	p = 0
	c1 = 0
	c0 = 0
	while i < n:
		c1 = 0
		c0 = 0
		while i < n and lst[i] == '1':
			c1 += 1
			i += 1
		while i < n and lst[i] == '0':
			c0 += 1
			i += 1
		if i == n:
			if p == 0:
				ans += a
			break
		if a < b * c0:
			if p == 0:
				ans += a
			else:
				p = 0
		else:
			ans += b * c0
			if p == 0:
				ans += a
				p = 1
	print(ans)
	t = t - 1


for _ in ' ' * int(input()):
	(a, b) = map(int, input().split())
	s = list(input())
	while s and s[-1] == '0':
		s.pop()
	s = s[::-1]
	while s and s[-1] == '0':
		s.pop()
	s = s[::-1]
	gaps = []
	cost = a if s else 0
	st = False
	gl = 0
	for i in s:
		if i == '0':
			if not st:
				st = not st
				gl = 1
			else:
				gl += 1
		else:
			if st:
				gaps.append(gl)
			st = False
			gl = 0
	for c in gaps:
		cost += min(a, c * b)
	print(cost)


for _ in range(int(input())):
	(m, j) = map(int, input().split())
	s = list(map(len, input().strip('0').split('1')))
	if len(s) == 1 and s[0] == 0:
		print(0)
		continue
	s = [x for x in s if x != 0]
	a = m * (len(s) + 1)
	for i in s:
		if i * j < m:
			a = a - m + i * j
	print(a)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	(a, b) = map(int, input().split())
	s = [k for k in input()]
	from collections import deque
	s = deque(s)
	while s and s[0] == '0':
		s.popleft()
	while s and s[-1] == '0':
		s.pop()
	if not s:
		print(0)
		continue
	div = []
	cur = 1
	st = s[0]
	for i in range(1, len(s)):
		if s[i] == st:
			cur += 1
		else:
			div += [cur]
			cur = 1
		st = s[i]
	div += [cur]
	ans = a
	for i in range(1, len(div), 2):
		ans += min(b * div[i], a)
	print(ans)


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	s = input()
	l = list(s)
	n = len(l)
	pos = 0
	while pos < n and l[pos] == '0':
		pos += 1
	if pos == n:
		print('0')
	else:
		gap = []
		c = 0
		for i in range(pos, n):
			if l[i] == '1':
				if c:
					gap.append(c)
				c = 0
			else:
				c += 1
		res = 0
		for i in gap:
			if i * b > a:
				res += a
			else:
				res += i * b
		print(res + a)


t = int(input())
for i in range(t):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	lst1 = input()
	ans = 0
	pos = []
	i = 0
	prev = 0
	while i < len(lst1):
		if lst1[i] == '1':
			i += 1
		else:
			j = i
			while j < len(lst1):
				if lst1[j] == '1':
					break
				else:
					j += 1
			if j != len(lst1):
				pos.append(j - i)
				i = j
			else:
				i = j
	pos1 = pos + []
	if pos == [] and lst1[0] == '0':
		print(0)
	elif pos == [] and lst1[0] == '1':
		print(a)
	else:
		if lst1[0] == '0':
			pos.pop(0)
		for i in range(len(pos)):
			ans += min(b * pos[i], a)
		if pos1[0] == len(lst1):
			print(ans)
		elif pos == []:
			print(a)
		else:
			print(ans + a)


import sys, math
mod = 10 ** 9 + 7
md = 998244353
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(a, b) = inp()
	s = str(input())
	ans = []
	c = 0
	for i in s:
		if i == '1':
			c += 1
		else:
			if c == 0:
				continue
			ans.append(c)
			c = 0
	if c > 0:
		ans.append(c)
	flag = False
	c = 0
	res = []
	for i in s:
		if i == '1':
			flag = True
		if flag == True:
			if i == '0':
				c += 1
			else:
				if c == 0:
					continue
				res.append(c)
				c = 0
	fin = 0
	if len(ans) > 0:
		fin += a
	if len(ans) > 1:
		for i in range(len(res)):
			if res[i] * b > a:
				fin += a
			else:
				fin += res[i] * b
	print(fin)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	l = list(map(int, input()))
	cost = 0
	seen = []
	i = 0
	current = l[0]
	one_cnt = 0
	zero_cnt = 0
	while i < len(l):
		if l[i] == 1 and current == 1:
			one_cnt += 1
		if l[i] == 1 and current == 0:
			seen.append([0, zero_cnt])
			current = 1
			one_cnt = 1
			zero_cnt = 0
		if l[i] == 0 and current == 0:
			zero_cnt += 1
		if l[i] == 0 and current == 1:
			seen.append([1, one_cnt])
			current = 0
			zero_cnt = 1
			one_cnt = 0
		i += 1
	if zero_cnt != 0:
		seen.append([0, zero_cnt])
	if one_cnt != 0:
		seen.append([1, one_cnt])
	if seen[0][0] == 0:
		seen.pop(0)
	cost = 0
	if len(seen) != 0:
		if seen[-1][0] == 0:
			seen.pop(-1)
		for i in range(len(seen)):
			if seen[i][0] == 0:
				if seen[i][1] * b <= a:
					seen[i][0] = 1
					cost += seen[i][1] * b
		for i in range(len(seen)):
			if seen[i][0] == 0:
				cost += a
		cost += a
	print(cost)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if int(s, 2):
		k = s.strip('0')
		t = k.split('0')
		ans = a
		count = 1
		for i in range(1, len(t)):
			if t[i] == '':
				count += 1
			else:
				if count * b <= a:
					ans += count * b
				else:
					ans += a
				count = 1
		print(ans)
	else:
		print(0)


def main():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		s = input()
		n = len(s)
		if not n:
			print(0)
			continue
		og_segments = 0
		on_segment = False
		gaps = []
		current_gap = 0
		zero_in = one_in = False
		for (c, i) in enumerate(s):
			if i == '0':
				zero_in = True
				if on_segment:
					og_segments += 1
					on_segment = False
				current_gap += 1
			else:
				one_in = True
				if not on_segment:
					on_segment = True
					if current_gap:
						gaps.append(current_gap)
					current_gap = 0
		if on_segment:
			og_segments += 1
		if not zero_in:
			print(a)
			continue
		if not one_in:
			print(0)
			continue
		if s[0] == '0':
			gaps.pop(0)
			if len(gaps) == 0:
				print(a)
				continue
		num_a = og_segments
		num_b = 0
		threshold = a // b
		for gap in gaps:
			if gap <= threshold:
				num_a -= 1
				num_b += gap
		print(a * num_a + b * num_b)
main()


from math import *
import threading
import sys
mod = 10 ** 9 + 7

def npr(n, r):
	return factorial(n) // factorial(n - r) if n >= r else 0

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = -1
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = mid
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	li = []
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, len(prime)):
		if prime[p]:
			li.append(p)
	return li

def primefactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n //= 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n //= i
	if n > 2:
		factors.append(n)
	return factors

def prod(li):
	ans = 1
	for i in li:
		ans *= i
	return ans
sys.setrecursionlimit(300000)
for _ in range(int(input()) if True else 1):
	(a, b) = map(int, input().split())
	s = list(input())
	al = [int(x) for x in s]
	n = len(al)
	al.append('x')
	cur = al[0]
	temp = 1
	elem = [[], []]
	for i in range(n):
		if al[i] != al[i + 1]:
			elem[cur].append(temp)
			cur ^= 1
			temp = 1
		else:
			temp += 1
	if len(elem[0]) == 1 and len(elem[1]) == 0:
		print(0)
	elif len(elem[0]) == 0:
		print(a)
	else:
		if s[0] == '0':
			elem[0] = elem[0][1:]
		if s[-1] == '0':
			elem[0] = elem[0][:-1]
		ans = 0
		elem[0].insert(0, 1000000)
		for i in range(len(elem[0])):
			ans += min(a, b * elem[0][i])
		print(ans)


t = int(input())
(aa, bb) = ([], [])
ss = []
for _ in range(t):
	(a, b) = input().split()
	aa.append(int(a))
	bb.append(int(b))
	ss.append(input())
for i in range(t):
	n = aa[i] // bb[i]
	s = ss[i]
	if '1' in s:
		fi = s.index('1')
		li = len(s) - s[::-1].index('1') - 1
		gaps = fills = 0
		c = 0
		for d in s[fi + 1:li + 1]:
			if d == '0':
				c += 1
			else:
				if c > n:
					gaps += 1
				else:
					fills += c
				c = 0
		print(bb[i] * fills + aa[i] * (gaps + 1))
	else:
		print(0)


for i in range(int(input())):
	(p, q) = list(map(int, input().split()))
	z = input()
	n = len(z)
	(a, b) = (-1, -1)
	for i in range(n):
		if z[i] == '1':
			a = i
			break
	for i in range(n - 1, -1, -1):
		if z[i] == '1':
			b = i
			break
	if a == -1:
		print(0)
	elif a == b:
		print(p)
	else:
		f = 1
		y = 0
		l = []
		for i in range(a, b + 1):
			if z[i] == '1':
				if f == 0:
					l.append(y * q)
					y = 0
				f = 1
			else:
				if f == 1:
					l.append(0)
				y += 1
				f = 0
		l.append(0)
		c = p
		for i in range(1, len(l), 2):
			c += min(l[i], p)
		print(c)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	arr = input()
	n = len(arr)
	if arr == '0' * n:
		print(0)
	else:
		one_check = int(arr[0])
		for i in range(n - 1):
			if arr[i] == '0' and arr[i + 1] == '1':
				one_check += 1
		chk = []
		i = 0
		while i < n - 1:
			if arr[i] == '1' and arr[i + 1] == '0':
				chk.append(0)
				i += 1
				while i < n and arr[i] == '0':
					chk[-1] += 1
					i += 1
				i -= 1
			i += 1
		if arr[-1] == '0':
			chk.pop(-1)
		if not arr:
			print(a)
		else:
			x = len(chk)
			ans = 1000000000000000000000
			t = 0
			chk.sort()
			for j in range(x):
				t += chk[j]
				ans = min(ans, t * b + (one_check - j - 1) * a)
			print(min(ans, one_check * a))


d = int(input())
for i in range(d):
	(a, b) = map(int, input().split())
	c = input().strip('0')
	e = 0
	f = 0
	g = 0
	for j in range(len(c)):
		if c[j] == '1':
			if g:
				if g >= a:
					g = 0
					f += a
					e = 0
				else:
					f += g
					g = 0
			e = 1
		else:
			g += b
	print(f + a * e)


for _ in range(int(input())):
	(a, b) = [int(i) for i in input().split()]
	s = input().lstrip('0').rstrip('0')
	if len(s) == 0:
		print(0)
		continue
	len_0 = 0
	ans = a
	for i in s:
		if i == '0':
			len_0 += 1
		else:
			if len_0 * b <= a:
				ans += len_0 * b
			else:
				ans += a
			len_0 = 0
	print(ans)


cases = int(input())
inp = []
for _ in range(cases):
	inp.append([])
	(a, b) = input().split(' ')
	inp[-1] = [int(a), int(b)]
	inp[-1].append(input())
for (a, b, s) in inp:
	e = a // b
	res = []
	(o, z) = (0, 0)
	for c in s:
		if c == '1':
			o += 1
			if z != 0:
				res.append((z, 0))
				z = 0
		if c == '0':
			z += 1
			if o != 0:
				res.append((o, 1))
				o = 0
	if o != 0:
		res.append((o, 1))
	if z != 0:
		res.append((z, 0))
	ans = a
	if res and res[0][1] == 0:
		res.pop(0)
	if res and res[-1][1] == 0:
		res.pop(-1)
	if not res:
		print(0)
		continue
	c = 1
	while c < len(res):
		if res[c][0] > e:
			c += 2
			ans += a
		elif res[c][0] <= e:
			ans += res[c][0] * b
			c += 2
	print(ans)


import sys
input = sys.stdin.readline
from collections import defaultdict
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	lis = list(input().strip())
	n = len(lis)
	for i in range(n):
		lis[i] = int(lis[i])
	s = []
	c = 0
	f = 0
	for i in range(n):
		if lis[i] == 1:
			f = 1
			if c > 0:
				s.append(c)
			c = 0
		elif f == 1:
			c += 1
	if f == 0:
		print(0)
		continue
	if len(s) == 0:
		print(a)
		continue
	s.sort()
	pre = [0]
	pre[0] = s[0]
	for i in s[1:]:
		pre.append(pre[-1] + i)
	x = len(s) + 1
	c = a * x
	for i in pre:
		x -= 1
		c = min(c, b * i + a * x)
	print(c)


import sys
from array import array
from typing import List, Tuple, TypeVar, Generic, Sequence, Union

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	from itertools import groupby
	t = int(input())
	ans = [0] * t
	for i in range(t):
		(a, b) = map(int, input().split())
		s = [(k, len(list(v))) for (k, v) in groupby(input().rstrip())]
		if s[0][0] == '0':
			s.pop(0)
		if s and s[-1][0] == '0':
			s.pop()
		if not s:
			continue
		cost = a
		for j in range(1, len(s), 2):
			cost += min(a, s[j][1] * b)
		ans[i] = cost
	sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
main()


f = int(input())
for j in range(f):
	A = 0
	B = 0
	(a, b) = map(int, input().split())
	s = input()
	A += 1 * int(s.find('1') != -1)
	i = s.find('1') + 1
	m = 0
	while m == 0:
		if s[i:].find('1') == -1:
			break
		if s[i:].find('1') != 0:
			if s[i:].find('1') < a / b:
				B += s[i:].find('1')
			else:
				A += 1
		i += s[i:].find('1') + 1
	print(A * a + B * b)


import itertools, collections
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input().strip('0')
	if not s:
		print(0)
		continue
	group = []
	for (i, (c, g)) in enumerate(itertools.groupby(s)):
		g = len(list(g))
		group.append(g)
	n = len(group) // 2 + 1
	dp = [0] * n
	dp[0] = a
	for i in range(1, n):
		cnt = group[2 * i - 1]
		dp[i] = min(dp[i - 1] + a, dp[i - 1] + b * cnt)
	print(dp[-1])


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = list(map(int, list(input().strip())))
	n = len(s)
	cnt0 = 0
	cnt1 = 0
	l = 0
	while l < n and s[l] == 0:
		l += 1
	r = n - 1
	while r >= 0 and s[r] == 0:
		r -= 1
	x = a // b
	res = 0
	for i in range(l, r + 1, 1):
		if s[i] == 0:
			cnt0 += 1
		else:
			if cnt0:
				if cnt0 <= x:
					res += cnt0 * b
				else:
					res += a
			cnt0 = 0
	if cnt0:
		if cnt0 <= x:
			res += cnt0 * b
		else:
			res += a
	if l <= r:
		res += a
	print(res)


import bisect
import math

def solve(a, b, s):
	if '1' not in s:
		return 0
	current_zeros = 0
	s = s.strip('0')
	answer = 0
	s_ans = '1'
	for j in range(1, len(s)):
		if s[j] == '0':
			current_zeros += 1
		else:
			if current_zeros == 0:
				s_ans += '1'
			elif current_zeros * b < a:
				answer += current_zeros * b
				for k in range(j - current_zeros, j + 1):
					s_ans += '1'
			else:
				for k in range(j - current_zeros, j):
					s_ans += '0'
				s_ans += '1'
			current_zeros = 0
	s_ans = s_ans.split('0')
	return answer + (len(s_ans) - s_ans.count('')) * a
t = int(input())
ans = []
for i in range(t):
	arr = [int(x) for x in input().split()]
	a = arr[0]
	b = arr[1]
	s = input()
	ans.append(solve(a, b, s))
for test in ans:
	print(test)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input()
	if s.count('0') == len(s):
		print(0)
		continue
	flag = False
	costs = []
	cnt = 0
	ren = 0
	for i in range(s.index('1'), len(s)):
		if flag == False:
			if s[i] == '0':
				ren += 1
			elif s[i] == '1':
				costs.append(ren)
				cnt += 1
				flag = True
		elif s[i] == '0':
			flag = False
			ren = 1
	costs = sorted(costs)
	sums = 0
	ans = 10 ** 18
	for i in range(cnt):
		sums += costs[i]
		ans = min(ans, a * (cnt - i) + b * sums)
	print(ans)


t = int(input())
for ti in range(t):
	(a, b) = map(int, input().split())
	s = input()
	ans = 0
	l = -1
	dl = 0
	pred = ''
	for i in range(len(s)):
		if s[i] == '0':
			if pred == '0' and l != -1:
				dl += 1
			if pred == '1':
				dl = 1
				l = i
		elif pred == '0':
			ans += min(a, dl * b)
			dl = 0
		pred = s[i]
	print(ans + a * ('1' in s))


import os
import sys
from io import BytesIO, IOBase
import math
from itertools import permutations
from decimal import Decimal, getcontext
getcontext().prec = 25
MOD = pow(10, 9) + 7
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import deque
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	l = input()
	m = 0
	le = len(l)
	start = 0
	end = le
	for i in range(le):
		if l[i] != '0':
			break
		start += 1
	for i in range(le - 1, start, -1):
		if l[i] != '0':
			break
		end -= 1
	c = 0
	for i in range(start, end):
		if l[i] == '1':
			if m == 0:
				m += n
			else:
				m = min(m + n, m + c * k)
				c = 0
		if l[i] == '0':
			c += 1
	print(m)


noOfTestCases = int(input())
for testCase in range(noOfTestCases):
	(activationCost, placeMine) = [int(i) for i in input().split()]
	buildings = input()
	noOfBuildings = len(buildings)
	totalCost = 0
	atIndex = 0
	previousMine = None
	while atIndex < noOfBuildings:
		if buildings[atIndex] == '0':
			pass
		else:
			if previousMine is None:
				pass
			elif (atIndex - previousMine - 1) * placeMine < activationCost:
				totalCost += (atIndex - previousMine - 1) * placeMine
			else:
				totalCost += activationCost
			previousMine = atIndex
		atIndex += 1
	if previousMine is not None:
		totalCost += activationCost
	print(totalCost)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def fact(n):
	m = 1000000007
	f = 1
	if n == 0:
		return 1
	else:
		for i in range(1, n + 1):
			f = f * i
			f %= m
		return int(f)
for xyz in range(0, int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if '1' not in s:
		print(0)
	else:
		gaps = []
		i = s.index('1')
		dis1 = 0
		n = len(s)
		while i < n:
			if s[i] == '0':
				t = countcon(s, i)
				i += t
				gaps.append(t)
			else:
				t = countcon(s, i)
				dis1 += 1
				i += t
		if s[-1] == '0':
			gaps.pop()
		gaps.sort()
		mi = a // b
		ans = 0
		for i in range(0, len(gaps)):
			if mi >= gaps[i]:
				dis1 -= 1
				ans += gaps[i] * b
		print(ans + a * dis1)


from collections import Counter
import math
import sys
import heapq
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for i in range(INT()):
	(a, b) = MAP()
	s = input()
	n = len(s)
	x = -1
	y = -1
	for i in range(n):
		if s[i] == '1':
			x = i
			break
	c0 = 0
	c1 = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			y = i
			break
	if x == -1 or y == -1:
		print(0)
	else:
		ans = a
		while x <= y:
			c = 0
			if s[x] == '0':
				while s[x] == '0':
					c += 1
					x += 1
			else:
				x += 1
			ans += min(a, c * b)
		print(ans)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	s = input()
	c = 0
	h = 0
	f = 1
	for i in range(len(s)):
		if s[i] == '1':
			if f == 1:
				f = 0
				h = 0
				c = c + a
			else:
				c = c + min(h * b, a)
				h = 0
		else:
			h = h + 1
	print(c)


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input().strip()
	n = len(s)
	l = []
	start = -1
	end = 0
	ans = 0
	prev = -1
	for j in range(n):
		if s[j] == '1':
			flag = 1
			if start == -1:
				start = j
			end = j
		elif start != -1:
			if prev != -1:
				ans += min(a, (start - prev - 1) * b)
			else:
				ans += a
			prev = end
			start = -1
	if start != -1:
		if prev != -1:
			ans += min(a, (start - prev - 1) * b)
		else:
			ans += a
	print(ans)


from collections import Counter

def saveTheCity(city, activate, place):
	city = city.strip('0')
	if not city:
		print(0)
		return
	ans = activate
	i = len(city) - 1
	while i:
		c = 0
		i -= 1
		while i > 0 and city[i] == '0':
			c += 1
			i -= 1
		ans += min(activate, place * c)
	print(ans)
testcases = int(input())
while testcases:
	testcases -= 1
	(activate, place) = list(map(int, input().split()))
	city = input()
	saveTheCity(city, activate, place)


for _ in range(int(input())):
	(a, b) = map(int, input().split(' '))
	arr = [int(w) for w in input()]
	if len(set(arr)) == 1 and arr[0] == 0:
		ans = 0
	else:
		n = len(arr)
		i = 0
		while i < n:
			if arr[i] == 1:
				start = i
				break
			i = i + 1
		i = n - 1
		while i >= 0:
			if arr[i] == 1:
				end = i
				break
			i = i - 1
		i = start
		ans = a
		while i < end + 1:
			j = i + 1
			if arr[i] == 0:
				c = 1
				while j < end + 1 and arr[j] == 0:
					c = c + 1
					j = j + 1
				if c * b <= a:
					ans = ans + c * b
				else:
					ans = ans + a
			i = j
	print(ans)


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())

def solve():
	(a, b) = mpint()
	s = inp()
	idx = s.find('1')
	if idx == -1:
		return 0
	ans = a
	zero = 0
	for c in s[idx + 1:]:
		if c == '0':
			zero += 1
		else:
			ans += min(a, b * zero)
			zero = 0
	return ans

def main():
	for _ in range(itg()):
		print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1443/problem/B'
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
elif DEBUG == 2:
	main()
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	main()


for tc in range(int(input())):
	(a, b) = map(int, input().split(' '))
	mines = list(input())
	segments = []
	gaps = []
	i = 0
	while i < len(mines):
		if mines[i] == '1':
			l = i
			r = i
			while r + 1 < len(mines):
				if mines[r + 1] == '1':
					r += 1
				else:
					break
			segments.append([l, r])
			i = r + 1
		else:
			i += 1
	cost = 0
	i = 0
	while i < len(segments) - 1:
		if (segments[i + 1][0] - segments[i][1] - 1) * b < a:
			cost += (segments[i + 1][0] - segments[i][1] - 1) * b
			segments[i] = [segments[i][0], segments[i + 1][1]]
			segments.pop(i + 1)
		else:
			i += 1
	cost += a * len(segments)
	ans = cost
	print(ans)


def solution(a, b, mp):
	idx = 0
	remain = 0
	sum = 0
	while idx < len(mp):
		if mp[idx] == '1':
			if remain <= 0:
				sum += a
			elif remain < a:
				sum += a - remain
			remain = a
			idx += 1
		else:
			remain -= b
			idx += 1
	print(sum)
	return
t = int(input())
for tt in range(t):
	(a, b) = map(int, input().split())
	mp = input()
	solution(a, b, mp)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=300006, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
for ik in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	ans = 0
	res = 0
	w = n
	t = 0
	if s.count('1') == 0:
		print(0)
		continue
	for i in range(n):
		if s[i] == '1':
			w = i
			break
	for i in range(w, n):
		if s[i] == '1':
			if t == 1:
				if b * res < a:
					ans += b * res
				else:
					ans += a
				res = 0
				t = 0
		else:
			res += 1
			t = 1
	print(ans + a)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	ones = []
	for j in range(len(s)):
		if s[j] == '1':
			ones.append(j)
	if len(ones) == 0:
		print(0)
	else:
		ans = a
		for j in range(len(ones) - 1):
			if ones[j + 1] - ones[j] - 1 > 0:
				if (ones[j + 1] - ones[j] - 1) * b < a:
					ans += (ones[j + 1] - ones[j] - 1) * b
				else:
					ans += a
		print(ans)


for test in range(int(input())):
	(a, b) = map(int, input().split())
	s = str(input())
	cur = 0
	li = []
	while cur < len(s):
		if s[cur] == '1':
			r = cur
			while r < len(s) and s[cur] == s[r]:
				r += 1
			li.append((cur, r - 1))
			cur = r
		else:
			cur += 1
	ans = 0
	cnt = len(li)
	for i in range(0, len(li) - 1):
		if a > (li[i + 1][0] - li[i][1] - 1) * b:
			ans += (li[i + 1][0] - li[i][1] - 1) * b
			cnt -= 1
	ans += cnt * a
	print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	n = len(s)
	blocks = [[s[0], 1, 0]]
	for i in range(1, n):
		if s[i] == s[i - 1]:
			blocks[-1][1] += 1
		else:
			blocks += [[s[i], 1, i]]
	one = []
	for i in blocks:
		if i[0] == '1':
			one.append(i)
	ans = 0
	for i in range(1, len(one)):
		if (one[i][2] - (one[i - 1][2] + one[i - 1][1])) * b <= a:
			ans += (one[i][2] - (one[i - 1][2] + one[i - 1][1])) * b
		else:
			ans += a
	if len(one):
		ans += a
	print(ans)


import pdb
q = int(input())
while q != 0:
	(a, b) = map(int, input().split())
	s = input()
	dp = [[0 for i in range(3)] for i in range(len(s) + 5)]
	if s[0] == '1':
		dp[0][0] = int(1e+18 + 5)
		dp[0][1] = a
	else:
		dp[0][0] = 0
		dp[0][1] = a + b
	for i in range(1, len(s)):
		if s[i] == '0':
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = min(dp[i - 1][0] + a, dp[i - 1][1]) + b
		else:
			dp[i][0] = int(1e+18 + 5)
			dp[i][1] = min(dp[i - 1][0] + a, dp[i - 1][1])
	print(min(dp[len(s) - 1][0:2]))
	q -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(x, y) = list(map(int, input().split()))
	s = list(input())
	if '1' not in s:
		print(0)
	else:
		c = s.index('1')
		d = len(s) - s[::-1].index('1')
		s = s[c:d]
		b = []
		c = 0
		for i in range(len(s)):
			if s[i] == '0':
				c += 1
			else:
				if c != 0:
					b.append(c)
				c = 0
		s = x
		for i in range(len(b)):
			if b[i] * y > x:
				s += x
			else:
				s += b[i] * y
		print(s)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	q = input()
	s = ''
	t = 1
	z = 0
	for i in range(len(q)):
		if q[i] == '0' and z == 1:
			s += '0'
			t = 1
		if q[i] == '1' and t == 1:
			z = 1
			s += '1'
			t = 0
	cnt = 0
	if s == '':
		print(0)
		continue
	dp = [0] * len(s)
	dp[0] = a
	for i in range(1, len(s)):
		if s[i] == '0':
			cnt += 1
			dp[i] = dp[i - 1]
		else:
			dp[i] = min(dp[i - 1] + a, dp[i - cnt - 1] + cnt * b)
			cnt = 0
	print(dp[-1])


t = int(input())
ans = []
for i in range(0, t):
	(a, b) = list(map(int, input().split()))
	s = input()
	segm = []
	j = 0
	while j < len(s) and s[j] == 0:
		j += 1
	seg_1 = [j]
	while j < len(s):
		while j < len(s) and s[j] == '0':
			j += 1
		seg_1 = [j]
		while j < len(s) and s[j] == '1':
			j += 1
		seg_1.append(j - 1)
		j += 1
		if seg_1[0] <= seg_1[1]:
			segm.append(seg_1)
		seg_1 = []
	tt = len(segm)
	ans1 = 0
	for j in range(0, tt - 1):
		r1 = segm[j][1]
		l2 = segm[j + 1][0]
		if a > b * (l2 - r1 - 1):
			ans1 += b * (l2 - r1 - 1)
			segm[j + 1][0] = segm[j][0]
		else:
			ans1 += a
	if tt > 0:
		ans1 += a
	ans.append(ans1)
for ans2 in ans:
	print(ans2)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = input()
	cost = 0
	if '1' in l:
		l = l[l.index('1'):]
		cost = a
	gaps = []
	gap = 0
	for c in l:
		if c == '1':
			if gap != 0:
				gaps.append(gap)
			gap = 0
		else:
			gap += 1
	for gap in gaps:
		cost += min(a, gap * b)
	print(cost)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())
for t in range(int(input())):
	(a, b) = get_ints()
	arr = list(input())
	while arr[0] == '0':
		arr.pop(0)
		if len(arr) == 0:
			break
	if len(arr) > 0:
		while arr[-1] == '0':
			arr.pop(-1)
			if len(arr) == 0:
				break
	if len(arr) == 0:
		print(0)
	else:
		maxim = a // b
		count = []
		for j in range(len(arr)):
			i = arr[j]
			if i == '1':
				count.append(j)
		c = 1
		val = 0
		for j in range(1, len(count)):
			gap = count[j] - count[j - 1]
			if gap == 1:
				continue
			elif (gap - 1) * b < a:
				val += (gap - 1) * b
			else:
				val += a
		print(val + a)


import collections
import math
from heapq import *
for t in range(int(input())):
	(a, b) = map(int, input().split())
	s = collections.deque(input())
	out = 0
	while s and s[0] == '0':
		s.popleft()
	while s and s[-1] == '0':
		s.pop()
	if not s:
		print(0)
		continue
	pre = s.popleft()
	count = 1
	temp = []
	v1 = v2 = 0
	while s:
		val = s.popleft()
		if val == '0':
			v2 += 1
		if val == pre:
			count += 1
		else:
			if pre == '1':
				v1 += 1
			else:
				temp.append(count)
			count = 1
			pre = val
	if pre == '1':
		v1 += 1
	else:
		temp.append(count)
	out = v1 * a
	temp.sort()
	ans = out
	for i in temp:
		out = out + i * b - a
		ans = min(out, ans)
	print(ans)


from sys import stdin
from math import *
for __ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	ans = 0
	g = []
	x = 0
	for i in range(len(s)):
		if s[i] == '1':
			x = i
			break
	y = x + 1
	while y < len(s):
		if s[y] == '1':
			if y - x - 1 != 0:
				g.append(y - x - 1)
			x = y
		y += 1
	ans = 0
	if '1' in s:
		ans = n
	for i in range(len(g)):
		if g[i] * m < n:
			ans += g[i] * m
		else:
			ans += n
	print(ans)


import sys, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	(a, b) = map(int, sys.stdin.readline().split())
	c = a // b
	arr = list(map(int, sys.stdin.readline().rstrip()))
	temp = []
	ans = 0
	for i in range(len(arr)):
		if arr[i]:
			temp.append(i)
	chunk = []
	for i in range(1, len(temp)):
		if temp[i] - temp[i - 1] > 1:
			chunk.append(temp[i] - temp[i - 1] - 1)
	if not chunk and (not temp):
		print(0)
	elif not chunk and temp:
		print(a)
	else:
		ans = 0
		check = [False] * len(chunk)
		left = 0
		for i in range(len(chunk)):
			if chunk[i] <= c:
				check[i] = True
				ans += chunk[i] * b
		for i in range(len(chunk)):
			if check[i] == False:
				left += 1
		ans += a * (left + 1)
		print(ans)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = str(input())
	n = len(s)
	p = 0
	d = 0
	i = 0
	c = 0
	while i < n and s[i] == '0':
		i += 1
	if i < n:
		c = a
		p = a
		d = a
		while i < n:
			if s[i] == '0':
				p = c
				while i < n and s[i] == '0':
					p += b
					i += 1
			else:
				d += a
				c = min(d, p)
				d = c
				while i < n and s[i] == '1':
					i += 1
	print(c)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)

def RLE(data):
	from itertools import groupby
	return [(x, len(list(grp))) for (x, grp) in groupby(data)]
for _ in range(INT()):
	(a, b) = MAP()
	S = input()
	rle = RLE(S)
	if len(rle) == 1:
		if rle[0][0] == '0':
			print(0)
		else:
			print(a)
	else:
		if rle[-1][0] == '0':
			rle.pop()
		if rle[0][0] == '0':
			rle.pop(0)
		ans = 0
		for (s, cnt) in rle:
			if s == '0':
				ans += min(0, cnt * b - a)
			else:
				ans += a
		print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	build = input()
	flag = 0
	start = -1
	end = -1
	for i in range(len(build)):
		if flag == 0 and build[i] == '1':
			start = i
			flag = 1
		if build[i] == '1':
			end = i
	cost = 0
	comb_cost = 0
	seg_part = 1
	zero = 0
	prev = '0'
	for i in range(len(build)):
		if i >= start and i <= end:
			if build[i] == '0':
				zero += 1
				prev = '0'
			else:
				if prev == '0':
					if 2 * a <= zero * b + a:
						seg_part += 1
					else:
						comb_cost += zero * b
					prev = '1'
				zero = 0
	cost += comb_cost + seg_part * a
	if start != -1:
		print(cost)
	else:
		print(0)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	c = input().strip('0')
	arr = []
	(o, z) = (0, 0)
	for i in c:
		if i == '1':
			if z:
				arr.append(z)
				z = 0
			o += 1
		else:
			if o:
				o = 0
			z += 1
	ans = a if '1' in c else 0
	for i in arr:
		if i * b > a:
			ans += a
		else:
			ans += i * b
	print(ans)


t = int(input())
for _ in range(t):
	(a, b) = list(map(int, input().split()))
	s = input()
	(i, j) = (0, len(s) - 1)
	while s[i] == '0':
		i = i + 1
		if i == len(s):
			break
	while s[j] == '0':
		j = j - 1
		if j == -1:
			break
	z = []
	x = i
	while x < j:
		if s[x] == '0':
			c = 0
			while s[x] == '0':
				x = x + 1
				c = c + 1
				if x == j:
					break
			z.append(c)
		else:
			x = x + 1
	if i > j:
		print(0)
	else:
		ans = a
		for i in range(len(z)):
			if ans + b * z[i] < ans + a:
				ans = ans + b * z[i]
			else:
				ans = ans + a
		print(ans)


t = int(input())
while t:
	(a, b) = [int(i) for i in input().split()]
	s = input()
	l = len(s)
	i = 0
	cluster = 0
	z = []
	first = False
	while i < l:
		if first == False:
			if s[i] == '1':
				first = True
				cluster += 1
				j = i
				while s[j] == '1':
					j += 1
					if j == l:
						break
				i = j
			else:
				i += 1
		elif s[i] == '0':
			c = 0
			j = i
			while s[j] == '0':
				j += 1
				c += 1
				if j == l:
					break
			i = j
			z.append(c)
		else:
			cluster += 1
			j = i
			while s[j] == '1':
				j += 1
				if j == l:
					break
			i = j
	if cluster == 0:
		print(0)
	elif cluster == 1:
		print(a)
	else:
		if s[-1] == '0':
			z.pop()
		mincost = cluster * a
		y = 0
		for i in z:
			mincost = min(mincost, (cluster - 1) * a + b * (y + i))
			if mincost == (cluster - 1) * a + b * (y + i):
				cluster -= 1
				y += i
		print(mincost)
	t -= 1


import math

def dtb(n):
	return bin(n).replace('0b', '')

def btd(n):
	return int(n, 2)
t = int(input())
for k in range(t):
	(a, b) = map(int, input().split())
	s = input()
	(ans, count) = (0, a + 1)
	for i in range(len(s)):
		if s[i] == '0':
			count += 1
		else:
			ans += min(a, b * count)
			count = 0
	print(ans)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	s = input()
	first = 0
	second = 0
	p = 0
	total = 0
	for i in range(len(s)):
		if p == 0 and s[i] == '1':
			first = i
			second = i
			total += a
			p = 1
		elif s[i] == '1':
			first = second
			second = i
			total = min(total + (second - first - 1) * b, total + a)
	print(total)


t = int(input())
while t > 0:
	(a, b) = input().split()
	ls = input()
	a = int(a)
	b = int(b)
	c = 0
	n = 0
	ans = a
	for i in range(len(ls)):
		if ls[i] == '1':
			break
	for j in reversed(range(len(ls))):
		if ls[j] == '1':
			break
	for m in range(len(ls)):
		if ls[m] == '0':
			n = n + 1
	if n != len(ls):
		for k in range(i, j + 1):
			if ls[k] == '0':
				c = c + 1
			else:
				if c != 0:
					ans += min(a, c * b)
				c = 0
		print(ans)
	else:
		print(0)
	t = t - 1


import math
t = int(input())
for _ in range(t):
	(a, b) = [int(x) for x in input().split()]
	s = list(input())
	cnt = float('inf')
	ans = 0
	for i in range(len(s)):
		if s[i] == '0':
			cnt += 1
		else:
			ans += min(a, b * cnt)
			cnt = 0
	print(ans)


for _ in range(int(input())):
	[x, y] = list(map(int, input().split()))
	n = str(input())
	output = 0
	gear = 0
	temp = 0
	flag = 0
	go = 0
	for i in range(len(n)):
		if n[i] == '1':
			go = 1
			break
	if go == 1:
		while n[0] == '0' and len(n) != 0:
			n = n[1:]
		while n[-1] == '0' and len(n) != 0:
			n = n[:len(n) - 1]
		for i in range(len(n)):
			if n[i] == '1':
				if temp <= x and gear != 0:
					output += temp
					temp = 0
					flag = 1
				if flag == 0:
					flag = 1
					output += x
					temp = 0
					gear = 0
			if n[i] == '0':
				flag = 0
				temp += y
				if temp <= x:
					gear = 1
	else:
		output = 0
	print(output)


T = int(input())
for t in range(T):
	(a, b) = [int(x) for x in input().split()]
	S = input()
	state = 1
	counter = 0
	acost = 1
	minecosts = []
	onefound = False
	for i in range(S.find('1'), len(S)):
		c = S[i]
		if c == '0':
			state = 0
			counter += 1
		if c == '1':
			if not onefound:
				onefound = True
			if state != 1:
				minecosts.append(counter)
				counter = 0
				state = 1
				acost += 1
	minecosts.sort()
	total = 0
	for x in minecosts:
		if b * x < a:
			total += b * x
			acost -= 1
		else:
			break
	if not onefound:
		print(0)
		continue
	print(total + a * acost)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
T = int(input())
for t in range(T):
	(explode, new) = map(int, input().split())
	s = input()
	s = s.lstrip('0').rstrip('0')
	last_one = -10101010101
	ans = 0
	for (i, ch) in enumerate(s):
		if ch == '0':
			continue
		connect_with_prev = (i - 1 - last_one) * new
		ans += min(connect_with_prev, explode)
		last_one = i
	print(ans)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	(a, b) = read()
	s = string()
	i = 0
	while i < len(s) and s[i] == '0':
		i += 1
	if i == len(s):
		print(0)
		return
	cnt = 0
	ans = 0
	ref = 0
	f = 0
	for j in range(i, len(s)):
		if s[j] == '0':
			cnt += 1
			f = 1
		else:
			ref = a
			if f == 1:
				ans += min(a, b * cnt)
				f = 0
			cnt = 0
	ans += a
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	s = input()
	index = 0
	all0 = True
	for i in range(len(s)):
		if s[i] != '0':
			all0 = False
	while index < len(s) and s[index] == '0':
		index += 1
	endIndex = len(s) - 1
	while endIndex > 0 and s[endIndex] == '0':
		endIndex -= 1
	between = []
	count = 0
	for i in range(index, endIndex):
		if s[i] == '1':
			count = 0
		if s[i] == '0':
			if count == 0:
				count += 1
				between.append(0)
			between[len(between) - 1] += 1
	ans = 0
	for i in range(len(between)):
		if between[i] * b < a:
			ans += between[i] * b
		else:
			ans += a
	ans += a
	if all0:
		print(0)
	else:
		print(ans)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = input()
	lst = []
	ind = 0
	n = len(s)
	while True:
		if ind > n - 1:
			break
		elif s[ind] == '1':
			c = 0
			e = 0
			for h in range(ind + 1, n):
				if s[h] == '0':
					e += 1
					break
				else:
					c += 1
			lst.append([ind, ind + c])
			ind = ind + c + 1
		else:
			ind += 1
	c = 0
	if lst != []:
		if '0' not in s or len(lst) == 1:
			c = a
		else:
			l1 = lst[0][0]
			l2 = lst[1][0]
			r1 = lst[0][1]
			r2 = lst[1][1]
			rones = r2 - l2 + 1
			lones = r1 - l1 + 1
			zer = l2 - r1 - 1
			if zer * b + a <= 2 * a:
				c += zer * b + a
			else:
				c += 2 * a
			l = len(lst)
			for h in range(2, l):
				zer = lst[h][0] - lst[h - 1][1] - 1
				if zer * b <= a:
					c += zer * b
				else:
					c += a
	print(c)