#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long int ll;
typedef pair<int,int> pii;

#define all(x) (x).begin(),(x).end()
#define sz(x) x.size()
#define mp(x,y) make_pair((x),(y))

void solve(){

    int a,b;
    cin>>a>>b;

    string s;
    cin>>s;
    vector<pii> v;
    int i=0;
    int n=sz(s);
    for(;i<n;i++){
        if(s[i]=='1'&& (!i || s[i-1]=='0')){
            int l = i;
            while(s[i]=='1' && i<n)
                i++;

            int r = i-1;
            v.push_back(mp(l,r));
        }
    }
    if(!sz(v))
    {
        cout<<0<<endl;
        return ;
    }
    ll cost=a;
    for(int i=1;i<sz(v);i++){
        if((v[i].first-v[i-1].second)*b<a)
            cost+=(v[i].first-v[i-1].second)*b;
        else
            cost+=a;
    }
    cout<<cost<<endl;

}

 int  main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int  t;
    cin>>t;
    //t=1;
    while(t--)
        solve();

}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long int ll;
typedef pair<int,int> pii;

#define all(x) (x).begin(),(x).end()
#define sz(x) x.size()

void solve(){

    int a,b;
    cin>>a>>b;

    vector<int> pos;
    string s;
    cin>>s;

    int n=s.size();

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

        if(x)
            pos.push_back(i);
    }

    int N = pos.size();

    vector<int> cost(N,0);

    cost[0] = a;

    for(int i=1;i<N;i++){
        cost[i] = min(b*(pos[i]-pos[i-1]-1)+cost[i-1],a+cost[i-1]);
    }

    cout<<cost[N-1]<<endl;
}

 int  main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int  t;
    cin>>t;
    //t=1;
    while(t--)
        solve();

}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int n = s.size();
    if (count(s.begin(), s.end(), '0') == n)
    {
        cout << 0 << endl;
        return;
    }
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b, a);
                while (i + 1 < n and s[i + 1] == '1')
                    i++;
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b, a);
                while (s[i] == '1')
                    i++;
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans = min(cnt * b + a, 2 * a);
                else
                    ans = min(cnt * b + a,  a);
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << ans << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
bool is(int x, int y)
{
    int f = __gcd(x, y);
    if (f == 1)return 0;
    if (x % y == 0 or y % x == 0)return 0;
    return 1;
}
void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    if (s.size() <= 2)
    {
        if (count(s.begin(), s.end(), '1'))
            cout << a;
        else
            cout << b + a;
        cout << endl; return;
    }
    int ans = 0;
    int n = s.size();
    ans += (s.substr(0, 2) == "01");
    ans += (s.substr(n - 2, 2) == "10");
    int cnt = count(s.begin(), s.end(), '0');
    ans = (cnt - ans) * b + a;
    cout << ans << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (ok == 1)
                    ans = min(cnt * b + a, 2 * a);
                else
                    ans = min(cnt * b + a,  a);
            }
            if (z == 0)ok = 1;
            else ok++;
            cnt = 0;
        }
        else
            cnt++, z++;
    }
    cout << ans << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b + a,  a);
                while (s[i] == '1')
                    i++;
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b + a,  a);
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    bool ok = 0;
    int ans = 0;
    int cnt = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                ans = min(cnt * b + a, 2 * a);
            }
            ok = 1;
            cnt = 0;
        }
        else
            cnt++;
    }
    cout << ans << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b, a);
                while (i + 1 < n and s[i + 1] == '1')
                    i++;
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
        // cout << ans << endl;
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

void hello()
{
    int a, b; cin >> a >> b;
    string s; cin >> s;
    int ok = 0;
    int ans = 0;
    int cnt = 0;
    int  z = 0;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            if (ok and cnt)
            {
                if (z == 0)
                    ans += min(cnt * b + a, 2 * a);
                else
                    ans += min(cnt * b, a);
                while (i < n and s[i] == '1')
                    i++;
                z++;
            }
            ok++;
            cnt = 0;
        }
        else {
            cnt++;
        }
    }
    cout << max(ans, a) << endl;
}

signed main() {
    int test; cin >> test; while (test--)
        hello();
    return 0;
}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {
        int a, b; cin >> a >> b;
        string s; cin >> s;

        string str = "";
        for(auto it : s) {
            if( str == "" ) str.push_back(it);
            else if( str.back() == '1' and it == '1' ) {

            } else {
                str.push_back(it);
            }
        }
        int n = (int)str.size();

        long long ans = 0LL;
        if( b < a ) {
            for(int i = 1; i < n - 1; i++) {
                if( str[i] == '0' and str[i] != str[i - 1] and str[i] != str[i + 1] ) {
                    ans += a + b;
                    str[i - 1] = str[i + 1] = str[i];
                }
            }
        }

        for(int i = 0; i < n; i++) {
            if( str[i] == '1' ) {
                ans += a;
            }
        }

        cout << ans << '\n';

    }

}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {

        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > in;
        for(auto it : s) {
            if(it == '1') in.push_back(1);
            else in.push_back(0);
        }

        vector< int > v;
        for(auto it : in) {
            if( v.empty() ) {
                v.push_back(it);
                continue;
            }
            if( it == 1 ) {
                if( v.back() == 0 ) v.push_back(it);
            } else {
                v.push_back(it);
            }
        }

        int n = (int)v.size();
        long long ans = 0LL;

        if( b < a ) {
            for(int i = 1; i < n - 1; i++) {
                if( !v[i] and v[i + 1] and v[i - 1] ) {
                    ans += a + b;
                    v[i + 1] = v[i - 1] = 0;
                }
            }
        }

        for(int i = 0; i < n; i++) ans += a * v[i];

        cout << ans << '\n';

    }

}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {

        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > in;
        for(auto it : s) {
            if(it == '1') in.push_back(1);
            else in.push_back(0);
        }

        vector< int > v;

        for(auto it : in) {
            if( v.empty() ) {
                v.push_back(it);
                continue;
            }

            if( it == 1 ) {
                if( v.back() == 0 ) v.push_back(it);
            } else {
                v.push_back(it);
            }
        }

        int n = (int)v.size();

        long long ans = 0LL;

        if( b <= a ) {
            for(int i = 1; i < n - 1; i++) {
                if( !v[i] and v[i + 1] and v[i - 1] ) {
                    ans += a + b;
                    v[i + 1] = v[i - 1] = 0;
                }
            }
        }

        for(int i = 0; i < n; i++) ans += a * v[i];

        cout << ans << '\n';

    }

}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {
        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > st, ed;

        s = s + "0";
        int n = (int)s.size();
        int cnt = 0;

        for(int i = 0; i < n; i++) {
            if( s[i] == '1' ) {
                if( !cnt ) st.push_back(i);
                cnt++;
            } else {
                if(cnt) ed.push_back(i);
                cnt = 0;
            }
        }

        int len = (int)st.size();
        vector< int > vis(len, 0);

        long long ans = 0LL;
        for(int i = 1; i < len; i++) {
            if( vis[i - 1] ) continue;
            int x = (st[i] - ed[i - 1]) * b;
            if( x <= a ) {
                ans += x + a;
                vis[i] = vis[i - 1] = 1;
            }
        }

        for(auto it : vis)if(!it)ans += a;

        cout << ans << '\n';

    }

    return 0;
}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {
        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > st, ed;

        s = s + "0";
        int n = (int)s.size();
        int cnt = 0;

        for(int i = 0; i < n; i++) {
            if( s[i] == '1' ) {
                if( !cnt ) st.push_back(i);
                cnt++;
            } else {
                if(cnt) ed.push_back(i);
                cnt = 0;
            }
        }

        int len = (int)st.size();
        vector< int > vis(len, 0);

        long long ans = 0LL;
        for(int i = 1; i < len; i++) {
            if( vis[i - 1] ) continue;
            int x = (st[i] - ed[i - 1]) * b;
            if( x < a ) {
                ans += x + a;
                vis[i] = vis[i - 1] = 1;
            }
        }

        for(auto it : vis)if(!it)ans += a;

        cout << ans << '\n';

    }

    return 0;
}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {

        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > in;

        for(auto it : s) {
            if(it == '1') in.push_back(1);
            else in.push_back(0);
        }

        vector< int > v;

        for(auto it : in) {
            if( v.empty() ) {
                v.push_back(it);
                continue;
            }

            if( it == 1 ) {
                if( v.back() == 0 ) v.push_back(it);
            } else {
                v.push_back(it);
            }
        }

        long long temp = 0LL;
        for(auto it : v) {
            if(it) temp += a;
        }

        int n = (int)v.size();

        long long ans = 0LL;

        if( b <= a ) {
            for(int i = 1; i < n - 1; i++) {
                if( !v[i] and v[i + 1] and v[i - 1] ) {
                    ans += a + b;
                    v[i + 1] = v[i - 1] = 0;
                }
            }
        }

        for(int i = 0; i < n; i++) {
            if( v[i] ) ans += a;
        }

        ans = min(ans, temp);

        cout << ans << '\n';

    }

}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {

        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > in;

        for(auto it : s) {
            if(it == '1') in.push_back(1);
            else in.push_back(0);
        }

        vector< int > v;

        for(auto it : in) {
            if( v.empty() ) {
                v.push_back(it);
                continue;
            }

            if( it == 1 ) {
                if( v.back() == 0 ) v.push_back(it);
            } else {
                v.push_back(it);
            }
        }

        long long temp = 0LL;
        for(auto it : v) {
            if(it) temp += a;
        }

        int n = (int)v.size();

        long long ans = 0LL;

        for(int i = 1; i < n - 1; i++) {
            if( !v[i] and v[i + 1] and v[i - 1] ) {
                ans += a + b;
                v[i + 1] = v[i - 1] = 0;
            }
        }

        for(int i = 0; i < n; i++) {
            if( v[i] ) ans += a;
        }

        ans = min(ans, temp);

        cout << ans << '\n';

    }

}


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

int main() {

#ifndef astro_lion
    ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif // astro_lion

    int t; cin >> t;
    while(t--) {
        int a, b; cin >> a >> b;
        string s; cin >> s;

        vector< int > st, ed;

        s = s + "0";
        int n = (int)s.size();
        int cnt = 0;

        for(int i = 0; i < n; i++) {
            if( s[i] == '1' ) {
                if( !cnt ) st.push_back(i);
                cnt++;
            } else {
                if(cnt) ed.push_back(i);
                cnt = 0;
            }
        }

        int len = (int)st.size();

        assert( st.size() == ed.size() );

        int total = len;

        long long ans = 0LL;
        for(int i = 1; i < len; i++) {
            int x = (st[i] - ed[i - 1]) * b;
            assert( x > 0 );
            if( x <= a ) {
                ans += x;
                total--;
            }
        }

        ans += 1LL * total * a;

        cout << ans << '\n';

    }

    return 0;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	ll int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	ll int ans=0;
	ll int i=0;
	while(s[i]!='1') i++;
	ans+=a;
	ll int l=s.size()-1;
	while(s[l]!='1') l--;
	for(ll int j=i;j<=l;)
	{
	    if(s[j]=='1') j++;
	    else{
	        ll int k,count=0;
	        for( k=j;;k++)
	        {
	            if(s[k]=='0') count++;
	            else break;
	        }
	        if(count*b>a) ans+=a;
	        else ans+=(count*b);

	        j=k;
	    }
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	ll int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	ll int ans=0;
	ll int i=0,flag=0;
	//while(s[i]!='1') i++;
	for(;i<s.size();i++)
	{
	    if(s[i]=='1') {flag=1; break;}
	}
	if(flag) ans+=a;
	ll int m=s.size()-1;
	//while(s[m]!='1') m--;
	for (; m >=0; m--)
    {
        if (s[m] == '1') break;
    }
	for(ll int j=i;j<=m;j++)
	{
	    int count=0;
	    while(s[j]=='0')
	    {
	        count++;
	        j++;
	    }
	    ans+=min(a,count*b);
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	int k,ans=0;
	int i=0;
	while(s[i]!='1') i++;
	ans+=a;
	int l=s.size()-1;
	while(s[l]!='1') l--;
	for(int j=i;j<=l;)
	{
	    if(s[j]=='1') j++;
	    else{
	        int k,count=0;
	        for( k=j;;k++)
	        {
	            if(s[k]=='0') count++;
	            else break;
	        }
	        if(count*b>2*a) ans+=a;
	        else ans+=(count*b);

	        j=k;
	    }
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


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

    #define F first
    #define S second
    #define pb push_back
    #define mp make_pair

    #define ll long long
    #define ld long double

    #define vi vector <int>
    #define vb vector <bool>
    #define vc vector <char>
    #define vll vector <ll>
    #define vs vector <string>

    #define pll pair < ll , ll >
    #define pld pair < ld , ld >
    #define pii pair < int , int >

    #define vpld vector < pld >
    #define vpii vector < pii >
    #define vpll vector < pll >

    #define vvi vector < vector < int > >
    #define vvll vector < vector < ll > >
    #define vvld vector < vector < ld > >
    #define vvb vector < vector < bool > >
    #define vvpii vector < vector < pii > >
    //vector<vector<int>> v(n, vector<int>(n));

    #define all(v) (v).begin() , (v).end()
    #define allrev(v) (v).rbegin() , (v).rend()

    #define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

    const int MOD = 1e9 + 7 ;

    bool isprime (ll num) {
        for (ll i = 2 ; i * i <= num ; ++i) {
    		if (num % i == 0) return false ;
    	}
        return true;
    }

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

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

    ll cel (ll x1, ll y1) {
        if ((x1) <= 0) return (x1) / (y1) ;
        return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
    }

    float squareRoot(float n)
        {
            float x = n;
            float y = 1;
            float e = 0.000001;
            while (x - y > e) {
                x = (x + y) / 2;
                y = n / x;
            }
            return x;
        }

    void search(string pat, string txt)
    {
        int M = pat.size();
        int N = txt.size();
        int i = 0;

        while (i <= N - M)
        {
            int j;
            for (j = 0; j < M; j++)
                if (txt[i + j] != pat[j])
                    break;
            if (j == M)
            {
                cout << "Pattern found at index " << i << endl;
                i = i + M;
            }
            else if (j == 0)
                i = i + 1;
            else
                i = i + j;
        }
    }

    int power(int x, unsigned int y)
    {
        if (y == 0)
            return 1;
        else if (y%2 == 0)
            return power(x, y/2)*power(x, y/2);
        else
            return x*power(x, y/2)*power(x, y/2);
    }
    /*int INT(string s)
    {
        return stoi(s);
    }*/

    int sum_of_digits(long long n) {
    	int res = 0;
    	while (n > 0) {
    		res += n % 10;
    		n /= 10;
    	}
    	return res;
    }

    void solve (int test_case) {
    	int a,b;
    	cin>>a>>b;
    	string s;
    	cin>>s;
  	    ll int ans=0;
    	ll int i=0,flag=0;
    	for(;i<s.size();i++)
    	{
    	    if(s[i]=='1') {flag=1; break;}
    	}
    	if(flag) ans+=a;
    	ll int m=s.size()-1;
    	for (; m >=0; m--)
        {
            if (s[m] == '1') break;
        }
    	for(int j=i;j<=m;)
    	{
    	    if(s[j]=='1') j++;
    	    else{
    	        int k,count=0;
    	        for( k=j;;k++)
    	        {
    	            if(s[k]=='0') count++;
    	            else break;
    	        }
    	        if(count*b>a) ans+=a;
    	        else ans+=(count*b);

    	        j=k;
    	    }
    	}
    	cout<<ans<<endl;

    }

    int main() {
    	ios_base :: sync_with_stdio (false) ;
    	cin.tie(0) ;
    	cout.tie(0) ;
    	//freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
    	int tc = 1 ;
    	cin >> tc ;
    	while (tc--) {
    		solve (tc) ;
    	}
    	return 0 ;
    }


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	ll int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	ll int ans=0;
	ll int i=0;
	while(s[i]!='1') i++;
	ans+=a;
	ll int l=s.size()-1;
	while(s[l]!='1') l--;
	for(ll int j=i;j<=l;j++)
	{
	    int count=0;
	    while(s[j]=='0')
	    {
	        count++;
	        j++;
	    }
	    ans+=min(a,count*b);
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	ll int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	ll int ans=0;
	ll int i=0;
	//while(s[i]!='1') i++;
	for(;i<s.size();i++)
	{
	    if(s[i]=='1') break;
	}
	ans+=a;
	ll int m=s.size()-1;
	//while(s[m]!='1') m--;
	for (; m >=0; m--)
    {
        if (s[m] == '1') break;
    }
	for(ll int j=i;j<=m;j++)
	{
	    int count=0;
	    while(s[j]=='0')
	    {
	        count++;
	        j++;
	    }
	    ans+=min(a,count*b);
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define ll long long
#define ld long double

#define vi vector <int>
#define vb vector <bool>
#define vc vector <char>
#define vll vector <ll>
#define vs vector <string>

#define pll pair < ll , ll >
#define pld pair < ld , ld >
#define pii pair < int , int >

#define vpld vector < pld >
#define vpii vector < pii >
#define vpll vector < pll >

#define vvi vector < vector < int > >
#define vvll vector < vector < ll > >
#define vvld vector < vector < ld > >
#define vvb vector < vector < bool > >
#define vvpii vector < vector < pii > >
//vector<vector<int>> v(n, vector<int>(n));

#define all(v) (v).begin() , (v).end()
#define allrev(v) (v).rbegin() , (v).rend()

#define dist(x1 , y1 , x2 , y2) sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))

const int MOD = 1e9 + 7 ;

bool isprime (ll num) {
    for (ll i = 2 ; i * i <= num ; ++i) {
		if (num % i == 0) return false ;
	}
    return true;
}

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

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

ll cel (ll x1, ll y1) {
    if ((x1) <= 0) return (x1) / (y1) ;
    return (((x1) / (y1)) + (((x1) % (y1)) != 0)) ;
}

float squareRoot(float n)
    {
        float x = n;
        float y = 1;
        float e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }

void search(string pat, string txt)
{
    int M = pat.size();
    int N = txt.size();
    int i = 0;

    while (i <= N - M)
    {
        int j;
        for (j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
        if (j == M)
        {
            cout << "Pattern found at index " << i << endl;
            i = i + M;
        }
        else if (j == 0)
            i = i + 1;
        else
            i = i + j;
    }
}

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
/*int INT(string s)
{
    return stoi(s);
}*/

int sum_of_digits(long long n) {
	int res = 0;
	while (n > 0) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

void solve (int test_case) {
	ll int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	ll int ans=0;
	ll int i=0;
	while(s[i]!='1') i++;
	ans+=a;
	ll int l=s.size()-1;
	while(s[l]!='1') l--;
	for(ll int j=i;j<=l;)
	{
	    if(s[j]=='1') j++;
	    else{
	        ll int k,count=0;
	        for( k=j;;k++)
	        {
	            if(s[k]=='0') count++;
	            else break;
	        }
	        if(count*b>2*a) ans+=a;
	        else ans+=(count*b);

	        j=k;
	    }
	}
	cout<<ans<<endl;

}

int main() {
	ios_base :: sync_with_stdio (false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int tc = 1 ;
	cin >> tc ;
	while (tc--) {
		solve (tc) ;
	}
	return 0 ;
}


/*
  Hey , lakshay 05
  while( ! ( succeed == try)); // Follow this
  Don't compare yourself with anyone in the world,if you do so it means you are insulting yourself -> Bill Gates

*/

#include <bits/stdc++.h>
#define ll long long
#define RUNFAST  ios_base::sync_with_stdio(false);    cin.tie(NULL);
#define vec  vector<ll>
#define pi vector<pair<ll,ll>>
#define fi first
#define se second
#define pb push_back
#define sz size()
#define pt1(a) cout<<a<<" ";
#define pt2(a,b) cout<<a<<" "<<b<<" ";
#define pt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" ";
#define sorti(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define issorted(v) is_sorted(v.begin(),v.end())
#define eb emplace_back
#define mp make_pair
#define MOD 1000000007 // 998244353
using namespace std;

ll visited[210000]={0};
vector<ll>vg[210000];

// Print vector

void printv(vector<ll> v)
{
   for(auto x:v)
   cout<<x<<" ";
}

// Important Functions

// DFS
void dfs(ll current)
{
    ll i,y;
    visited[current]=1;
 //   cout<<current<<" ";
    for(i=0;i<vg[current].size();i++)
    {
        y=vg[current][i];
        if(visited[y]==0)
        dfs(y);
    }
}

// BFS

void bfs(ll current)
{
    ll x,y,i;
    queue<ll>q;
    q.push(current);
    visited[current]=1;
    while(!q.empty())
    {
        x=q.front();
   //     cout<<x<<" ";
        q.pop();
        visited[x]=1;
        for(i=0;i<vg[x].size();i++)
        {
            y=vg[x][i];
            if(visited[y]==0)
            {
                visited[y]=1;
                q.push(y);
            }
        }
    }
}

ll fact(ll n)
{
    ll p=1;
    for(ll i=1;i<=n;i++)
    p*=i;
    return p;
}

// Prime Numbers
void checkprime(ll N) {
        ll count = 0;
        for( ll i = 1;i * i <=N;++i ) {
             if( N % i == 0) {
                 if( i * i == N )
                         count++;
                 else       // i < sqrt(N) and (N / i) > sqrt(N)
                         count += 2;
              }
        }
        if(count == 2)
        {
           // prime number
        }
}

// IO File
void IO()
{
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
}

// Multiplicative Inverse

ll power(ll x, unsigned ll y, unsigned ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : (x * p) % m;
}

ll modInverse(ll a,ll m)
{
     return power(a, m - 2, m);

}

// LCM

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

/* ---------------------------------------------------------------------------------------------------------*/

 void lakshay()
 {
          ll i,z=0,c,d,y=0,x,l=0,A[1000],B[1000],a,b,s=0,j,m,n,p,q,k,maxi=INT_MIN,mini=INT_MAX;
          bool ok=true;
          cin>>a>>b;
       string S;
       cin>>S;
          vec v;
          for(i=0;i<S.length();i++)
          {
              if(S[i]=='1')
              v.pb(i);
          }

          if(v.sz)
          {
              if(v.sz==1)
              {
                  s+=a;
              }
              else
              {
                  k=v[0];
                  for(i=1;i<v.sz;i++)
                  {
                      if(v[i]==k+1)
                      {
                          k=v[i];
                          continue;
                      }
                      else
                      {
                          if(ok)
                          {
                              s+=min((v[i]-k-1)*b+a,2*a);
                          }
                          else
                          {
                              s+=min((v[i]-k-1)*b,2*a);
                          }
                          k=v[i];
                          ok=false;
                      }
                  }
              }
          }
          pt1(s);
 }

int main()
{  RUNFAST
    ll num;
num=1;
cin>>num;
while(num--)
{
lakshay();
cout<<"\n";
}
return 0;
}


/*
  Hey , lakshay 05
  while( ! ( succeed == try)); // Follow this
  Don't compare yourself with anyone in the world,if you do so it means you are insulting yourself -> Bill Gates

*/

#include <bits/stdc++.h>
#define ll long long
#define RUNFAST  ios_base::sync_with_stdio(false);    cin.tie(NULL);
#define vec  vector<ll>
#define pi vector<pair<ll,ll>>
#define fi first
#define se second
#define pb push_back
#define sz size()
#define pt1(a) cout<<a<<" ";
#define pt2(a,b) cout<<a<<" "<<b<<" ";
#define pt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" ";
#define sorti(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define issorted(v) is_sorted(v.begin(),v.end())
#define eb emplace_back
#define mp make_pair
#define MOD 1000000007 // 998244353
using namespace std;

ll visited[210000]={0};
vector<ll>vg[210000];

// Print vector

void printv(vector<ll> v)
{
   for(auto x:v)
   cout<<x<<" ";
}

// Important Functions

// DFS
void dfs(ll current)
{
    ll i,y;
    visited[current]=1;
 //   cout<<current<<" ";
    for(i=0;i<vg[current].size();i++)
    {
        y=vg[current][i];
        if(visited[y]==0)
        dfs(y);
    }
}

// BFS

void bfs(ll current)
{
    ll x,y,i;
    queue<ll>q;
    q.push(current);
    visited[current]=1;
    while(!q.empty())
    {
        x=q.front();
   //     cout<<x<<" ";
        q.pop();
        visited[x]=1;
        for(i=0;i<vg[x].size();i++)
        {
            y=vg[x][i];
            if(visited[y]==0)
            {
                visited[y]=1;
                q.push(y);
            }
        }
    }
}

ll fact(ll n)
{
    ll p=1;
    for(ll i=1;i<=n;i++)
    p*=i;
    return p;
}

// Prime Numbers
void checkprime(ll N) {
        ll count = 0;
        for( ll i = 1;i * i <=N;++i ) {
             if( N % i == 0) {
                 if( i * i == N )
                         count++;
                 else       // i < sqrt(N) and (N / i) > sqrt(N)
                         count += 2;
              }
        }
        if(count == 2)
        {
           // prime number
        }
}

// IO File
void IO()
{
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
}

// Multiplicative Inverse

ll power(ll x, unsigned ll y, unsigned ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : (x * p) % m;
}

ll modInverse(ll a,ll m)
{
     return power(a, m - 2, m);

}

// LCM

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

/* ---------------------------------------------------------------------------------------------------------*/

 void lakshay()
 {
          ll i,z=0,c,d,y=0,x,l=0,A[1000],B[1000],a,b,s=0,j,m,n,p,q=0,k,maxi=INT_MIN,mini=INT_MAX;
          bool ok=true;
          cin>>a>>b;
       string S;
       cin>>S;
          vec v;
          for(i=0;i<S.length();i++)
          {
              if(S[i]=='1')
              {
                  q++;
                  v.pb(i);
              }
      //        v.pb(i);
          }

         n=S.length();

          if(q==n)
          {
              cout<<a;
              return;
          }

          if(v.sz)
          {
              if(v.sz==1)
              {
                  s+=a;
              }
              else
              {
                  k=v[0];
                  for(i=1;i<v.sz;i++)
                  {
                      if(v[i]==k+1)
                      {
                          k=v[i];
                          l++;
                          continue;
                      }
                      else
                      {
                          l=0;
                          if(ok)
                          {
                              p=min((v[i]-k-1)*b+a,2*a);
                              if(p==2*a and k==0)
                              {
                                  y=min((v[i]-k-1)*b+a,b+2*a);
                                  s+=y;
                              }
                              else
                              s+=p;
                          }
                          else
                          {
                              p=min((v[i]-k-1)*b,2*a);
                              if(p==2*a and v[i]==n-1)
                              {
                                  y=min((v[i]-k-1)*b,b+2*a);
                                  s+=y;
                              }
                              else
                              s+=p;
                          }
                          k=v[i];
                          ok=false;
                      }
                  }
              }
          }

          if(ok and v.sz)
          s+=a;

          pt1(s);
 }

int main()
{  RUNFAST
    ll num;
num=1;
cin>>num;
while(num--)
{
lakshay();
cout<<"\n";
}
return 0;
}


/*
  Hey , lakshay 05
  while( ! ( succeed == try)); // Follow this
  Don't compare yourself with anyone in the world,if you do so it means you are insulting yourself -> Bill Gates

*/

#include <bits/stdc++.h>
#define ll long long
#define RUNFAST  ios_base::sync_with_stdio(false);    cin.tie(NULL);
#define vec  vector<ll>
#define pi vector<pair<ll,ll>>
#define fi first
#define se second
#define pb push_back
#define sz size()
#define pt1(a) cout<<a<<" ";
#define pt2(a,b) cout<<a<<" "<<b<<" ";
#define pt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" ";
#define sorti(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define issorted(v) is_sorted(v.begin(),v.end())
#define eb emplace_back
#define mp make_pair
#define MOD 1000000007 // 998244353
using namespace std;

ll visited[210000]={0};
vector<ll>vg[210000];

// Print vector

void printv(vector<ll> v)
{
   for(auto x:v)
   cout<<x<<" ";
}

// Important Functions

// DFS
void dfs(ll current)
{
    ll i,y;
    visited[current]=1;
 //   cout<<current<<" ";
    for(i=0;i<vg[current].size();i++)
    {
        y=vg[current][i];
        if(visited[y]==0)
        dfs(y);
    }
}

// BFS

void bfs(ll current)
{
    ll x,y,i;
    queue<ll>q;
    q.push(current);
    visited[current]=1;
    while(!q.empty())
    {
        x=q.front();
   //     cout<<x<<" ";
        q.pop();
        visited[x]=1;
        for(i=0;i<vg[x].size();i++)
        {
            y=vg[x][i];
            if(visited[y]==0)
            {
                visited[y]=1;
                q.push(y);
            }
        }
    }
}

ll fact(ll n)
{
    ll p=1;
    for(ll i=1;i<=n;i++)
    p*=i;
    return p;
}

// Prime Numbers
void checkprime(ll N) {
        ll count = 0;
        for( ll i = 1;i * i <=N;++i ) {
             if( N % i == 0) {
                 if( i * i == N )
                         count++;
                 else       // i < sqrt(N) and (N / i) > sqrt(N)
                         count += 2;
              }
        }
        if(count == 2)
        {
           // prime number
        }
}

// IO File
void IO()
{
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
}

// Multiplicative Inverse

ll power(ll x, unsigned ll y, unsigned ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : (x * p) % m;
}

ll modInverse(ll a,ll m)
{
     return power(a, m - 2, m);

}

// LCM

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

/* ---------------------------------------------------------------------------------------------------------*/

 void lakshay()
 {
          ll i,z=0,c,d,y=0,x,l=0,A[1000],B[1000],a,b,s=0,j,m,n,p,q,k,maxi=INT_MIN,mini=INT_MAX;
          bool ok=true;
          cin>>a>>b;
       string S;
       cin>>S;
          vec v;
          for(i=0;i<S.length();i++)
          {
              if(S[i]=='1')
              v.pb(i);
          }

          n=S.length();

          if(v.sz)
          {
              if(v.sz==1)
              {
                  s+=a;
              }
              else
              {
                  k=v[0];
                  for(i=1;i<v.sz;i++)
                  {
                      if(v[i]==k+1)
                      {
                          k=v[i];
                          continue;
                      }
                      else
                      {
                          if(ok)
                          {

                              p=min((v[i]-k-1)*b+a,2*a);
                              if(p==2*a and k==0)
                              {
                                  y=min((v[i]-k-1)*b+a,b+2*a);
                                  s+=y;
                              }
                              else
                              s+=p;
                          }
                          else
                          {
                              p=min((v[i]-k-1)*b,2*a);
                              if(p==2*a and v[i]==n-1)
                              {
                                  y=min((v[i]-k-1)*b,b+a);
                                  s+=y;
                              }
                              else
                              s+=p;
                          }
                          k=v[i];
                          ok=false;
                      }
                  }
              }
          }
          pt1(s);
 }

int main()
{  RUNFAST
    ll num;
num=1;
cin>>num;
while(num--)
{
lakshay();
cout<<"\n";
}
return 0;
}


/////////////////////////////////////////////////   IN THE NAME OF GOD
#include <bits/stdc++.h>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define     For(i,a,b)        for (int i=a; i<b; i++)
#define     Rep(i,a)          for (int i=0; i<a; i++)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     SF                scanf
#define     PF                printf
#define     timestamp(x)      printf("Time : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define     INF               1e11
#define     pii               pair < long long , long long >
#define     MP                make_pair
#define     MOD               1000000007
#define     EPS               1e-9
#define     ll                long long
#define     MAXN              100000+10
#define     Dbug              cout<<""
#define     PI                3.1415926535897932384626433
//int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int n ;
ll Xor[MAXN] , Degree[MAXN] , v , s ;
vector < int > adj[MAXN] ;
vector < pair < pii , int > >  vc ;
vector < int > node ;

int main(){
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "r" , stdin ) ;
    //freopen ( "output.txt" , "w" , stdout ) ;
#endif
    SF ( "%d" , &n ) ;
    Rep ( i , n ){
    	SF ( "%I64d %I64d" , &v , &s ) ;
    	Xor[i] = s ;
    	Degree[i] = v ;
    	vc.push_back( MP ( MP ( v , s ) , i ) ) ;
    }
    sort ( vc.rbegin() , vc.rend() ) ;
    while ( vc.size() && vc.back().first.first == 0 ) vc.pop_back() ;
    while ( vc.size() && vc.back().first.first == 1 ){
    	int u = vc.back().second ;
    	int v = vc.back().first.second ;
    	adj[u].push_back( v ) ;
    	adj[v].push_back( u ) ;
    	Degree[u]-- ;
    	Degree[v]-- ;
    	if ( Degree[v] == 1 ) node.push_back( v ) ;
    	vc.pop_back() ;
    }
    while ( (int)node.size() ){
    	int u = node.back() ;
    	node.pop_back() ;
    	if ( Degree[u] < 1 ) continue ;
    	ll x = 0 ;
    	Rep ( i , (int)adj[u].size() ) x ^= (ll)adj[u][i] ;
    	x ^= Xor[u] ;
    	Degree[u]-- ;
    	Degree[x]-- ;
    	adj[u].push_back( x ) ;
    	adj[x].push_back( u ) ;
    	if ( Degree[x] == 1 ) node.push_back( x ) ;
    }
    set < pii > res ;
    Rep ( i , MAXN ) Rep ( j , (int)adj[i].size() ) res.insert( MP ( max ( i , adj[i][j] ) , min ( i , adj[i][j] ) ) ) ;
    PF ( "%d\n" , (int)res.size() ) ;
    for ( auto x : res ) PF ( "%d %d\n" , x.first , x.second ) ;
    return 0 ;
}


/////////////////////////////////////////////////   IN THE NAME OF GOD
#include <bits/stdc++.h>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define     For(i,a,b)        for (int i=a; i<b; i++)
#define     Rep(i,a)          for (int i=0; i<a; i++)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     SF                scanf
#define     PF                printf
#define     timestamp(x)      printf("Time : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define     INF               1e11
#define     pii               pair < long long , long long >
#define     MP                make_pair
#define     MOD               1000000007
#define     EPS               1e-9
#define     ll                long long
#define     MAXN              100000+10
#define     Dbug              cout<<""
#define     PI                3.1415926535897932384626433
//int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int n ;
ll Xor[MAXN] , Degree[MAXN] , v , s ;
vector < int > adj[MAXN] ;
vector < pair < pii , int > >  vc ;
vector < int > node ;

int main(){
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "r" , stdin ) ;
    //freopen ( "output.txt" , "w" , stdout ) ;
#endif
    SF ( "%d" , &n ) ;
    Rep ( i , n ){
        SF ( "%I64d %I64d" , &v , &s ) ;
        Xor[i] = s ;
        Degree[i] = v ;
        vc.push_back( MP ( MP ( v , s ) , i ) ) ;
    }
    sort ( vc.rbegin() , vc.rend() ) ;
    while ( vc.size() && vc.back().first.first == 0 ) vc.pop_back() ;
    while ( vc.size() && vc.back().first.first == 1 ){
        int u = vc.back().second ;
        int v = vc.back().first.second ;
        adj[u].push_back( v ) ;
        adj[v].push_back( u ) ;
        Degree[u]-- ;
        Degree[v]-- ;
        if ( Degree[v] == 1 ) node.push_back( v ) ;
        vc.pop_back() ;
    }
    while ( (int)node.size() ){
        int u = node.back() ;
        node.pop_back() ;
        if ( Degree[u] < 1 ) continue ;
        ll x = 0 ;
        Rep ( i , (int)adj[u].size() ) x ^= (ll)adj[u][i] ;
        x ^= Xor[u] ;
        Degree[u]-- ;
        Degree[x]-- ;
        adj[u].push_back( x ) ;
        adj[x].push_back( u ) ;
        if ( Degree[x] == 1 ) node.push_back( x ) ;
    }
    set < pii > res ;
    Rep ( i , MAXN ) Rep ( j , (int)adj[i].size() ) res.insert( MP ( max ( i , adj[i][j] ) , min ( i , adj[i][j] ) ) ) ;
    PF ( "%d\n" , (int)res.size() ) ;
    for ( auto x : res ) PF ( "%I64d %I64d\n" , x.first , x.second ) ;
    return 0 ;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>

using namespace std;

//degre, node, val
struct Data
{
    int i;
    int degree;
    int val;
    Data(int ii, int idegree, int ival) : i(ii), degree(idegree), val(ival){}
};

class comp
{
    public:
    bool operator()(Data a, Data b)
    {
        if (a.degree!=b.degree) return a.degree<b.degree;
        return a.i<b.i;
    }
};

int main()
{
    int n;
    cin>>n;
    vector<Data> d;
    set<Data, comp> s;

    for (int i=0; i<n; i++)
    {
        int deg,v;
        cin>>deg>>v;
        d.push_back(Data(i,deg,v));
        if (deg>0) s.insert(Data(i,deg,v));
    }

    vector<pair<int,int> > edge;

    while (!s.empty())
    {
        //cout<<s.size()<<endl;
        Data pop = *s.begin(); s.erase(s.begin());
        int u = pop.i;
        int v = pop.val;
        int val = pop.val;
        //cout<<u<<" "<<pop.degree<<" "<<val<<endl;
        edge.push_back(make_pair(u,v));
        //cout<<"finding "<<v<<" "<<d[v].degree<<" "<<d[v].val<<endl;
        auto it = s.find(Data(v,d[v].degree, d[v].val));
        //cout<<(it==s.end())<<endl;
        if (it!=s.end()) s.erase(it);
        d[v].degree--;
        d[v].val = d[v].val ^ u;
        if (d[v].degree>0) s.insert(Data(v, d[v].degree, d[v].val));
    }

    cout<<edge.size()<<endl;
    for (int i=0; i<edge.size(); i++)
    {
        cout<<edge[i].first<<" "<<edge[i].second<<endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>

using namespace std;

//degre, node, val
struct Data
{
    int i;
    int degree;
    int val;
    Data(int ii, int idegree, int ival) : i(ii), degree(idegree), val(ival){}
};

class comp
{
    public:
    bool operator()(Data a, Data b)
    {
        if (a.degree!=b.degree) return a.degree<b.degree;
        return a.i<b.i;
    }
};

int main()
{
    int n;
    cin>>n;
    vector<Data> d;
    set<Data, comp> s;

    for (int i=0; i<n; i++)
    {
        int deg,v;
        cin>>deg>>v;
        d.push_back(Data(i,deg,v));
        s.insert(Data(i,deg,v));
    }

    vector<pair<int,int> > edge;

    while (!s.empty())
    {
        //cout<<s.size()<<endl;
        Data pop = *s.begin(); s.erase(s.begin());
        int u = pop.i;
        int v = pop.val;
        int val = pop.val;
        //cout<<u<<" "<<pop.degree<<" "<<val<<endl;
        edge.push_back(make_pair(u,v));
        //cout<<"finding "<<v<<" "<<d[v].degree<<" "<<d[v].val<<endl;
        auto it = s.find(Data(v,d[v].degree, d[v].val));
        //cout<<(it==s.end())<<endl;
        if (it!=s.end()) s.erase(it);
        d[v].degree--;
        d[v].val = d[v].val ^ u;
        if (d[v].degree>0) s.insert(Data(v, d[v].degree, d[v].val));
    }

    cout<<edge.size()<<endl;
    for (int i=0; i<edge.size(); i++)
    {
        cout<<edge[i].first<<" "<<edge[i].second<<endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>

using namespace std;

//degre, node, val
struct Data
{
    int i;
    int degree;
    int val;
    Data(int ii, int idegree, int ival) : i(ii), degree(idegree), val(ival){}
};

class comp
{
    public:
    bool operator()(Data a, Data b)
    {
        if (a.degree!=b.degree) return a.degree<b.degree;
        return a.i<b.i;
    }
};

int main()
{
    int n;
    cin>>n;
    vector<Data> d;
    set<Data, comp> s;

    for (int i=0; i<n; i++)
    {
        int deg,v;
        cin>>deg>>v;
        d.push_back(Data(i,deg,v));
        s.insert(Data(i,deg,v));
    }

    vector<pair<int,int> > edge;

    while (!s.empty())
    {
        //cout<<s.size()<<endl;
        Data pop = *s.begin(); s.erase(s.begin());
        int u = pop.i;
        int v = pop.val;
        int val = pop.val;
        //cout<<u<<" "<<pop.degree<<" "<<val<<endl;
        edge.push_back(make_pair(u,v));
        //cout<<"finding "<<v<<" "<<d[v].degree<<" "<<d[v].val<<endl;
        auto it = s.find(Data(v,d[v].degree, d[v].val));
        //cout<<(it==s.end())<<endl;
        if (it!=s.end()) s.erase(it);
        d[v].degree--;
        d[v].val = d[v].val ^ u;
        if (d[v].degree>0) s.insert(Data(v, d[v].degree, d[v].val));
        getchar();
    }

    cout<<edge.size()<<endl;
    for (int i=0; i<edge.size(); i++)
    {
        cout<<edge[i].first<<" "<<edge[i].second<<endl;
    }
    return 0;
}


#ifdef ONLINE_JUDGE
	#include <bits/stdc++.h>
#else
	#include <algorithm>
	#include <bitset>
	#include <cassert>
	#include <cstdio>
	#include <cstring>
	#include <iostream>
	#include <map>
	#include <set>
	#include <stack>
	#include <string>
	#include <utility>
	#include <vector>
	#include <queue>
	#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N;
vector <pair <int, int> > edges;
vector <int> values, degrees;
queue <int> bfs;

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans, from, to;

	ReadNo(N);
	degrees.resize(N);
	values.resize(N);

	for (i = 0; i < N; ++i) {
		ReadNo(degrees[i]); ReadNo(values[i]);
		if (degrees[i] == 1) {
			bfs.push(i);
		}
	}

	while (bfs.size()) {
		from = bfs.front();
		bfs.pop();
		to = values[from];
		if (to != from && values[to] != 0) {
			edges.pb(mp(from, to));
			values[to] ^= from;
			degrees[to] -= 1;
			if (degrees[to] == 1) {
				bfs.push(to);
			}
		}
	}

	printf("%d\n", ans = edges.size());
	for (i = 0; i < ans; ++i) {
		printf("%d %d\n", edges[i].first, edges[i].second);
	}

	return 0;
}


#ifdef ONLINE_JUDGE
	#include <bits/stdc++.h>
#else
	#include <algorithm>
	#include <bitset>
	#include <cassert>
	#include <cstdio>
	#include <cstring>
	#include <iostream>
	#include <map>
	#include <set>
	#include <stack>
	#include <string>
	#include <utility>
	#include <vector>
	#include <queue>
	#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N;
set <pair <int, int> > edges;
vector <int> values, degrees;
queue <int> bfs;

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans, from, to;

	ReadNo(N);
	degrees.resize(N);
	values.resize(N);

	for (i = 0; i < N; ++i) {
		ReadNo(degrees[i]); ReadNo(values[i]);
		if (degrees[i] == 1) {
			bfs.push(i);
		}
	}

	while (bfs.size()) {
		from = bfs.front();
		bfs.pop();
		if (degrees[from] == 0) {
			continue;
		}
		to = values[from];
		if (from != to && edges.find(mp(min(from, to), max(from, to))) == edges.end() && degrees[to] > 0) {
			edges.insert(mp(min(from, to), max(from, to)));
			values[to] ^= from;
			degrees[to] -= 1;
			if (degrees[to] == 1) {
				bfs.push(to);
			}
		}
	}

	printf("%d\n", ans = edges.size());
	for (auto edge : edges) {
		printf("%d %d\n", edge.first, edge.second);
	}

	return 0;
}


#ifdef ONLINE_JUDGE
	#include <bits/stdc++.h>
#else
	#include <algorithm>
	#include <bitset>
	#include <cassert>
	#include <cstdio>
	#include <cstring>
	#include <iostream>
	#include <map>
	#include <set>
	#include <stack>
	#include <string>
	#include <utility>
	#include <vector>
	#include <queue>
	#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N;
set <pair <int, int> > edges;
vector <int> values, degrees;
queue <int> bfs;

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans, from, to;

	ReadNo(N);
	degrees.resize(N);
	values.resize(N);

	for (i = 0; i < N; ++i) {
		ReadNo(degrees[i]); ReadNo(values[i]);
		if (degrees[i] == 1) {
			bfs.push(i);
		}
	}

	while (bfs.size()) {
		from = bfs.front();
		bfs.pop();
		to = values[from];
		if (from != to && edges.find(mp(min(from, to), max(from, to))) == edges.end() && degrees[to] > 0) {
			edges.insert(mp(min(from, to), max(from, to)));
			values[to] ^= from;
			degrees[to] -= 1;
			if (degrees[to] == 1) {
				bfs.push(to);
			}
		}
	}

	printf("%d\n", ans = edges.size());
	for (auto edge : edges) {
		printf("%d %d\n", edge.first, edge.second);
	}

	return 0;
}


#ifdef ONLINE_JUDGE
	#include <bits/stdc++.h>
#else
	#include <algorithm>
	#include <bitset>
	#include <cassert>
	#include <cstdio>
	#include <cstring>
	#include <iostream>
	#include <map>
	#include <set>
	#include <stack>
	#include <string>
	#include <utility>
	#include <vector>
	#include <queue>
	#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N;
set <pair <int, int> > edges;
vector <int> values, degrees;
queue <int> bfs;

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans, from, to;

	ReadNo(N);
	degrees.resize(N);
	values.resize(N);

	for (i = 0; i < N; ++i) {
		ReadNo(degrees[i]); ReadNo(values[i]);
		if (degrees[i] == 1) {
			bfs.push(i);
		}
	}

	while (bfs.size()) {
		from = bfs.front();
		bfs.pop();
		to = values[from];
		if (from != to && edges.find(mp(min(from, to), max(from, to))) == edges.end()) {
			edges.insert(mp(min(from, to), max(from, to)));
			values[to] ^= from;
			degrees[to] -= 1;
			if (degrees[to] == 1) {
				bfs.push(to);
			}
		}
	}

	printf("%d\n", ans = edges.size());
	for (auto edge : edges) {
		printf("%d %d\n", edge.first, edge.second);
	}

	return 0;
}


#ifdef ONLINE_JUDGE
	#include <bits/stdc++.h>
#else
	#include <algorithm>
	#include <bitset>
	#include <cassert>
	#include <cstdio>
	#include <cstring>
	#include <iostream>
	#include <map>
	#include <set>
	#include <stack>
	#include <string>
	#include <utility>
	#include <vector>
	#include <queue>
	#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N;
vector <pair <int, int> > edges;
vector <int> values, degrees;
queue <int> bfs;

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans, from, to;

	ReadNo(N);
	degrees.resize(N);
	values.resize(N);

	for (i = 0; i < N; ++i) {
		ReadNo(degrees[i]); ReadNo(values[i]);
		if (degrees[i] == 1) {
			bfs.push(i);
		}
	}

	while (bfs.size() > 1) {
		from = bfs.front();
		bfs.pop();
		to = values[from];
		edges.pb(mp(from, to));
		values[to] ^= values[from];
		degrees[to] -= 1;
		if (degrees[to] == 1) {
			bfs.push(to);
		}
	}

	printf("%d\n", ans = edges.size());
	for (i = 0; i < ans; ++i) {
		printf("%d %d\n", edges[i].first, edges[i].second);
	}

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <set>

using namespace std;

const int MAX = ((1 << 17) + 10);

int n;

int val[MAX], in[MAX];
int val2[MAX];

struct node{
    int in;
    int ind;
    node(int in, int ind) :
        in(in), ind(ind) {};
    node(){};
};

bool operator < (node x, node y) {
    if (x.in != y.in) return x.in < y.in;
    return x.ind < y.ind;
}

int br = 0;
int sol[MAX], sol2[MAX];

int find_val( int x ) {
    return (val[ x ]^val2[ x ]);
}

set<node> s;

void solve() {
    set<node>::iterator it;
    while ( !s.empty() ) {
            it = s.begin();
            node k = *it;
        if ( k.in ) {
            int a = k.ind;
            int b = find_val( a );
            sol[br] = a;
            sol2[br] = b;
            br ++;
            s.erase(k);
            s.erase( node(in[b], b) );
            s.insert( node( k.in - 1, a ) );
            s.insert( node( in[b] - 1, b ) );
            in[b] --;
            val2[b] ^= a;
        } else {
            s.erase( k );
        }
    }
}

int main() {
    scanf("%d", &n);
    for ( int i = 0; i < n; i ++) {
        scanf("%d %d", &in[i], &val[i]);
        s.insert( node( in[i], i ) );
    }
    solve();
    printf("%d\n", br);
    for ( int i = 0; i < br; i ++ ) {
        printf("%d %d\n", sol[i], sol2[i]);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX = ((1 << 17) + 10);

int n;

int val[MAX], in[MAX];
int val2[MAX];

struct node{
    int in;
    int ind;
    node(int in, int ind) :
        in(in), ind(ind) {};
    node(){};
};

int cmp(node x, node y) {
    if (x.in != y.in) return x.in < y.in;
    return x.ind < y.ind;
}

node a[MAX];

int br = 0;
int sol[MAX], sol2[MAX];

int find_val( int x ) {
    return (val[ a[x].ind ]^val2[ a[x].ind ]);
}

int main() {
    scanf("%d", &n);
    for ( int i = 0; i < n; i ++) {
        scanf("%d %d", &in[i], &val[i]);
        a[i] = node( in[i], i );
    }
    sort(a, a + n, cmp);
    for ( int i = 0; i < n; i ++ ) {
        //printf("%d %d\n", a[i].in, a[i].ind);
        if ( in[ a[i].ind ] ) {
            int k = find_val(i);
            sol[br] = k;
            sol2[br] = a[i].ind;
            br ++;
            val2[k] ^= a[i].ind;
            in[ k ] --;
        }
    }
    printf("%d\n", br);
    for ( int i = 0; i < br; i ++ ) {
        printf("%d %d\n", sol[i], sol2[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define OR ||
#define AND &&
#define XOR ^
#define EqualsTo ==
#define Equals =
#define also ,
#define Begin {
#define End }
#define isSmallerThan <
#define isSmallerThanOrEqualsTo <=
#define isBiggerThan >
#define isBiggerThanOrEqualsTo >=
#define Decrease --
#define Increase ++
#define NOT !
#define pushTo >>
#define flushIn <<
#define standardInput cin
#define standardOutput cout
#define clrscr ios_base::sync_with_stdio(false)
#define _ main()
#define Whitespace " "
#define mod %
#define fillchar memset
#define Integer int
#define EndLine "\n"
#define pb push_back
#define mp make_pair
using namespace std;

typedef vector<int> vi;

Integer _
Begin
    Integer n also sum Equals 0;
    standardInput pushTo n;
    queue<Integer> q;
    vi d(n) also x(n);
    for (Integer i = 0; i isSmallerThan n; Increase i)
    Begin
        standardInput pushTo d[i] pushTo x[i];
        if (d[i] EqualsTo 1) q.push(i);
        sum += d[i];
    End
    standardOutput flushIn sum/2 flushIn EndLine;
    while (NOT q.empty())
    Begin
        Integer v Equals q.front() also u Equals x[v]; q.pop();
        if (NOT d[u] OR NOT d[v]) continue;
        standardOutput flushIn v flushIn Whitespace flushIn x[v] flushIn EndLine;
        Decrease d[v] also Decrease d[x[v]];
        x[x[v]]^=v also x[v] Equals 0;
        if (d[u] EqualsTo 1) q.push(u);
    End
End


#include <bits/stdc++.h>
#define OR ||
#define AND &&
#define XOR ^
#define EqualsTo ==
#define Equals =
#define also ,
#define Begin {
#define End }
#define isSmallerThan <
#define isSmallerThanOrEqualsTo <=
#define isBiggerThan >
#define isBiggerThanOrEqualsTo >=
#define Decrease --
#define Increase ++
#define NOT !
#define pushTo >>
#define flushIn <<
#define standardInput cin
#define standardOutput cout
#define clrscr ios_base::sync_with_stdio(false)
#define _ main()
#define Whitespace " "
#define mod %
#define fillchar memset
#define Integer int
#define EndLine "\n"
#define pb push_back
#define mp make_pair
using namespace std;

typedef vector<int> vi;

Integer _
Begin
    Integer n also sum Equals 0;
    standardInput pushTo n;
    queue<Integer> q;
    vi d(n) also x(n);
    for (Integer i = 0; i isSmallerThan n; Increase i)
    Begin
        standardInput pushTo d[i] pushTo x[i];
        if (d[i] EqualsTo 1) q.push(i);
        sum += d[i];
    End
    standardOutput flushIn sum/2 flushIn EndLine;
    while (NOT q.empty())
    Begin
        Integer v Equals q.front() also u Equals x[v]; q.pop();
        if (NOT d[u]) continue;
        standardOutput flushIn v flushIn Whitespace flushIn x[v] flushIn EndLine;
        Decrease d[v] also Decrease d[x[v]];
        x[x[v]]^=v also x[v] Equals 0;
        if (d[u] EqualsTo 1) q.push(u);
    End
End


#include <bits/stdc++.h>
#define OR ||
#define AND &&
#define XOR ^
#define EqualsTo ==
#define Equals =
#define also ,
#define Begin {
#define End }
#define isSmallerThan <
#define isSmallerThanOrEqualsTo <=
#define isBiggerThan >
#define isBiggerThanOrEqualsTo >=
#define Decrease --
#define Increase ++
#define NOT !
#define pushTo >>
#define flushIn <<
#define standardInput cin
#define standardOutput cout
#define clrscr ios_base::sync_with_stdio(false)
#define _ main()
#define mod %
#define fillchar memset
#define Integer int
#define EndLine "\n"
#define pb push_back
#define mp make_pair
using namespace std;

typedef vector<int> vi;

Integer _
Begin
    Integer n also sum Equals 0;
    queue<Integer> q;
    vi d(n) also x(n);
    for (Integer i = 0; i isSmallerThan n; Increase i)
    Begin
        standardInput pushTo d[i] pushTo x[i];
        if (d[i] EqualsTo 1) q.push(i);
        sum += d[i];
    End
    standardOutput flushIn sum/2 flushIn EndLine;
    while (NOT q.empty())
    Begin
        Integer v Equals q.front() also u Equals x[v]; q.pop();
        standardOutput flushIn v flushIn x[v] flushIn EndLine;
        Decrease d[v] also Decrease d[x[v]];
        x[x[v]]^=v also x[v] Equals 0;
        if (d[u] EqualsTo 1) q.push(u);
    End
End


#include <iostream>
#include <cstring>
#include <cstdio>

#define FIGHT

using namespace std;

const int MaxA=7e4+233;

int n;
pair<int, int> p[MaxA], op[MaxA];
int num;
int queue[MaxA];
int main() {
#ifndef FIGHT
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
#endif
	while(~scanf("%d", &n)) {
		int f=0, r=0;
		for(int i=0; i<n; i++) {
			scanf("%d%d", &p[i].first, &p[i].second);
			if(p[i].first==1)queue[r++]=i;
		}
		num=0;
		while(f!=r) {
			int u=queue[f++];
			if(!p[u].first)continue;
			p[u].first--;
			int v=p[u].second;
			p[v].first--;
			p[v].second^=u;
			if(p[v].first==1)queue[r++]=v;
			op[num++]=make_pair(u, v);
		}
		printf("%d\n", num);
		for(int i=0; i<num; i++) {
			printf("%d %d\n", op[i].first, op[i].second);
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>

#define FIGHT

using namespace std;

const int MaxA=6e4+233;

int n;
pair<int, int> p[MaxA], op[MaxA];
int num;
int queue[MaxA];
int main() {
#ifndef FIGHT
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
#endif
	while(~scanf("%d", &n)) {
		int f=0, r=0;
		for(int i=0; i<n; i++) {
			scanf("%d%d", &p[i].first, &p[i].second);
			if(p[i].first==1)queue[r++]=i;
		}
		num=0;
		while(f!=r) {
			int u=queue[f++];
			if(!p[u].first)continue;
			p[u].first--;
			int v=p[u].second;
			p[v].first--;
			p[v].second^=u;
			op[num++]=make_pair(u, v);
		}
		printf("%d\n", num);
		for(int i=0; i<num; i++) {
			printf("%d %d\n", op[i].first, op[i].second);
		}
	}
	return 0;
}


// ITNOG

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef pair<int,int> pii;

const int maxN = (1 << 16) + 5;

int d[maxN], s[maxN];
queue< int > q;
vector< pii > ans;

int main()
{
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> d[i] >> s[i];
	for (int i = 0; i < n; ++i)
		if (d[i] == 1) q.push( i );
	while (!q.empty())
	{
		int v = q.front(), u = s[v];
		q.pop();
		if (d[v] == 0)
			continue;
		ans.push_back( mp(v, u) );
		d[u] --;
		s[u] ^= s[v];
		if (d[u] == 1)
			q.push(u);
	}
	cout << sz(ans) << '\n';
	for (int i = 0; i < sz(ans); ++i)
		cout << ans[i].ff << ' ' << ans[i].ss << '\n';
	return 0;
}


// ITNOG

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef pair<int,int> pii;

const int maxN = 1 << 16 + 10;

int d[maxN], s[maxN];
queue< int > q;
vector< pii > ans;

int main()
{
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> d[i] >> s[i];
	for (int i = 0; i < n; ++i)
		if (d[i] == 1) q.push( i );
	while (!q.empty())
	{
		int v = q.front(), u = s[v];
		q.pop();
		if (d[v] == 0)
			continue;
		ans.push_back( mp(v, u) );
		d[u] --;
		s[u] ^= s[v];
		if (d[u] == 1)
			q.push(u);
	}
	cout << sz(ans) << '\n';
	for (int i = 0; i < sz(ans); ++i)
		cout << ans[i].ff << ' ' << ans[i].ss << '\n';
	return 0;
}


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

#define pie pair<int,int>
#define mp make_pair
#define F first
#define S second

struct ver {
	int deg, s, i;
};

bool operator < (ver v, ver vv) {
	return v.deg < vv.deg || (v.deg == vv.deg && v.i < vv.i);
}

vector< ver > vers;
vector< pie > ans;
set< ver > s;

void construct() {

	ver v = *s.begin();
	if (v.deg == 0) {
		s.erase(v);
	} else {
		ans.push_back( mp(v.i, v.s) );
		ver nei;
		nei.deg = vers[v.i].deg;
		nei.s = vers[v.i].s;
		nei.i = v.i;
		nei = *s.find(nei);
		s.erase(nei);
		nei.deg --;
		nei.s ^= v.s;
		s.insert(nei);
		s.erase(s.begin());
	}
}

int main() {
	int n;
	cin >> n;
	vers.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> vers[i].deg >> vers[i].s;
		vers[i].i = i;
		s.insert(vers[i]);
	}
	while (s.size())
		construct();
	cout << ans.size() - 1 << '\n';
	for (int i = 0; i < ans.size() - 1; ++i)
		cout << ans[i].F << ' ' << ans[i].S << '\n';
	return 0;
}


// ITNOG

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef pair<int,int> pii;

const int maxN = (1 << 16) + 5;

int d[maxN], s[maxN];
queue< int > q;
vector< pii > ans;

int main()
{
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> d[i] >> s[i];
	for (int i = 0; i < n; ++i)
		if (d[i] == 1) q.push( i );
	while (!q.empty())
	{
		int v = q.front(), u = s[v];
		q.pop();
		if (d[v] == 0)
			continue;
		ans.push_back( mp(v, u) );
		d[u] --;
		s[u] ^= v;
		if (d[u] == 1)
			q.push(u);
	}
	cout << sz(ans) << '\n';
	for (int i = 0; i < sz(ans); ++i)
		cout << ans[i].ff << ' ' << ans[i].ss << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long L;
//#define int L

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("int.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    #endif
    //ios_base::sync_with_stdio(0);cin.tie(0);

    int n;cin>>n;
    vector<pair<int,int> > v(n);
    set<pair<pair<int,int>, int> > a;
    int m=0;
    for(int i=0; i<n; i++) {
        cin>>v[i].first>>v[i].second;m+=v[i].first;
        a.insert(make_pair(v[i],i));
    }
    m/=2;

    int d,s,i;
    cout<<m<<endl;
    while(!a.empty()) {
        auto x=a.begin();
        d = x->first.first;
        s = x->first.second;
        i = x->second;

        if(d==0) continue;
        if(d==1) {
            cout<<i<<" "<<s<<endl;
            auto it = a.find(make_pair(v[s],s));
            v[s].first--;
            v[s].second ^= i;
            a.erase(it);
            a.insert(make_pair(v[s],s));
            a.erase(x);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long L;
//#define int L

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("int.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    #endif
    //ios_base::sync_with_stdio(0);cin.tie(0);

    int n;cin>>n;
    vector<pair<int,int> > v(n);
    set<pair<pair<int,int>, int> > a;
    int m=0;
    for(int i=0; i<n; i++) {
        cin>>v[i].first>>v[i].second;m+=v[i].first;
        a.insert(make_pair(v[i],i));
    }
    m/=2;

    int d,s,i;
    cout<<m<<endl;
    for(auto x: a) {
        d = x.first.first;
        s = x.first.second;
        i = x.second;

        if(d==0) continue;
        if(d==1) {
            cout<<i<<" "<<s<<endl;
            auto it = a.find(make_pair(v[s],s));
            v[s].first--;
            v[s].second ^= i;
            a.erase(it);
            a.insert(make_pair(v[s],s));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long L;
//#define int L

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("int.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    #endif
    //ios_base::sync_with_stdio(0);cin.tie(0);

    int n;cin>>n;
    vector<pair<int,int> > v(n);
    set<pair<pair<int,int>, int> > a;
    int m=0;
    for(int i=0; i<n; i++) {
        cin>>v[i].first>>v[i].second;m+=v[i].first;
        a.insert(make_pair(v[i],i));
    }
    m/=2;

    int d,s,i;
    cout<<m<<endl;
    while(!a.empty()) {
        auto x=a.begin();
        d = x->first.first;
        s = x->first.second;
        i = x->second;

        if(d==1) {
            cout<<i<<" "<<s<<endl;
            auto it = a.find(make_pair(v[s],s));
            v[s].first--;
            v[s].second ^= i;
            a.erase(it);
            a.insert(make_pair(v[s],s));
        }
        a.erase(x);
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

vector <int> a;
vector <pair <int, int> > b;

bool cmp(int a1, int a2) {
    return (b[a1].first < b[a2].first) || ((b[a1].first == b[a2].first) && (b[a1].second < b[a2].second));
}

int main() {

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int d, s;
        cin >> d >> s;
        a.push_back(i);
        b.push_back(make_pair(d, s));
    }
    sort(a.begin(), a.end(), cmp);
    vector <pair <int, int> > ans;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]].first == 0) continue;
        int neigh = b[a[i]].second;
        b[neigh].first--;
        b[neigh].second ^= a[i];
        b[a[i]].first = 0;
        b[a[i]].second = 0;
        ans.push_back(make_pair(a[i], neigh));
        if (b[neigh].first == 1) {
            a[i] = neigh;
            --i;
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans) {
        cout << i.first << " " << i.second << endl;
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

vector <int> a;
vector <pair <int, int> > b;

bool cmp(int a1, int a2) {
    return b[a1].first < b[a2].first || b[a1].first == b[a2].first && b[a1].second < b[a2].second;
}

int main() {

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int d, s;
        cin >> d >> s;
        a.push_back(i);
        b.push_back(make_pair(d, s));
    }
    sort(a.begin(), a.end(), cmp);
    vector <pair <int, int> > ans;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]].first == 0) continue;
        int neigh = b[a[i]].second;
        b[neigh].first--;
        b[neigh].second ^= i;
        ans.push_back(make_pair(a[i], neigh));
    }

    cout << ans.size() << endl;
    for (auto i : ans) {
        cout << i.first << " " << i.second << endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

vector <int> a;
vector <pair <int, int> > b;

bool cmp(int a1, int a2) {
    return b[a1].first < b[a2].first || b[a1].first == b[a2].first && b[a1].second < b[a2].second;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int d, s;
        cin >> d >> s;
        a.push_back(i);
        b.push_back(make_pair(d, s));
    }
    sort(a.begin(), a.end(), cmp);
    vector <pair <int, int> > ans;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]].first == 0) continue;
        int neigh = b[a[i]].second;
        b[neigh].first--;
        b[neigh].second ^= a[i];
        ans.push_back(make_pair(a[i], neigh));
    }

    cout << ans.size() << endl;
    for (auto i : ans) {
        cout << i.first << " " << i.second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define ld long double
#define ll long long
#define sqr(a) (a)*(a)
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define inf (int)1e9
#define pi pair<int,int>
#define y1 fdfs
using namespace std;
const int N=65536;
vector<pi>ans;
et<pi>s;
int v,dg[N],xs[N],n,to,cur[N];
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>dg[i]>>xs[i];
        if(dg[i])s.insert(mp(dg[i],i));
    }
    while(!s.empty())
    {
        v=s.begin()->y;
        s.erase(s.begin());
        if(!dg[v])continue;
        to=xs[v]^cur[v];
        s.erase(mp(dg[to],to));
        dg[to]--;
        dg[v]--;
        s.insert(mp(dg[to],to));
        ans.pb(mp(v,to));
        cur[to]^=v;
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();++i)
        printf("%d %d\n",ans[i].x,ans[i].y);
}


#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define ld long double
#define ll long long
#define sqr(a) (a)*(a)
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define inf (int)1e9
#define pi pair<int,int>
#define y1 fdfs
using namespace std;
const int N=65536;
vector<pi>ans;
set<pi>s;
int v,dg[N],xs[N],n,to,cur[N];
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>dg[i]>>xs[i];
        if(dg[i])s.insert(mp(dg[i],i));
    }
    while(!s.empty())
    {
        v=s.begin()->y;
        s.erase(s.begin());
        if(!dg[v])continue;
        to=xs[v]^cur[v];
        s.erase(mp(dg[to],to));
        dg[to]--;
        dg[v]--;
        s.insert(mp(dg[to],to));
        ans.pb(mp(v,to));
        cur[to]^=v;
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();++i)
        printf("%d %d\n",ans[i].x,ans[i].y);
}


#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define ld long double
#define ll long long
#define sqr(a) (a)*(a)
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define inf (int)1e9
#define pi pair<int,int>
#define y1 fdfs
using namespace std;
const int N=65536;
vector<pi>ans;
unordered_set<pi>s;
int v,dg[N],xs[N],n,to,cur[N];
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>dg[i]>>xs[i];
        if(dg[i])s.insert(mp(dg[i],i));
    }
    while(!s.empty())
    {
        v=s.begin()->y;
        s.erase(s.begin());
        if(!dg[v])continue;
        to=xs[v]^cur[v];
        s.erase(mp(dg[to],to));
        dg[to]--;
        dg[v]--;
        s.insert(mp(dg[to],to));
        ans.pb(mp(v,to));
        cur[to]^=v;
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();++i)
        printf("%d %d\n",ans[i].x,ans[i].y);
}


//Misha and Forest.cpp
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define PAUSE system("pause");
#define DEBUG puts("Here is a BUG");
#define SYNC_CLOSE ios::sync_with_stdio(false);
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)1e9 + 5;
using namespace std;
int main(int argc, char const *argvertice[]) {
#ifndef ONLINE_JUDGE
    freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
    SYNC_CLOSE;
    int n; cin >> n;
    queue<int> q;
    std::vector<pair<int, int> > vertice;
    for(int i = 0; i < n; i++) {
        int d, s;
        cin >> d >> s;
        vertice.emplace_back(d, s);
        if (d == 1) q.push(i);
    }
    std::vector<pair<int, int> > res;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        if (vertice[u].first == 0) continue;
        int v = vertice[u].second;
        res.emplace_back(u, v);
        vertice[v].first--; vertice[v].second ^= u;
        if (vertice[v].first == 1) q.push(v);
    }
    cout << res.size() << endl;
    for(auto i : res) {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


//Misha and Forest.cpp
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define PAUSE system("pause");
#define DEBUG puts("Here is a BUG");
#define SYNC_CLOSE ios::sync_with_stdio(false);
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)1e9 + 5;
using namespace std;

struct Node {
    int v;
    pair<int, int> p;
    Node() { }
    Node(int v, pair<int, int> p): v(v), p(p) { }
    bool operator < (const Node& rhs) const {
        return p.first < rhs.p.first || (p.first == rhs.p.first && p.second < rhs.p.second);
    }
};
int de[1<<17], ss[1<<17];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
    int n; cin >> n;
    std::vector<Node> v;
    CLEAR(de, 0); CLEAR(ss, 0);
    for(int i = 0; i < n; i++) {
        int d, s;
        cin >> d >> s;
        v.push_back(Node(i, {d, s}));
    }
    sort(v.begin(), v.end());
    std::vector<pair<int, int> > res;
    for(auto i : v) {
        int d = i.p.first; d -= de[i.v];
        int s = i.p.second; s ^= ss[i.v];
        // cout << d << ' ' << s << endl;
        if (d == 0) continue;
        if (d == 1) {
            res.emplace_back(i.v, s);
            de[s]--; //cout << de[s] << endl;
            ss[s] ^= i.v;
        }
    }
    cout << res.size() << endl;
    for(auto i : res) {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


//Misha and Forest.cpp
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define PAUSE system("pause");
#define DEBUG puts("Here is a BUG");
#define SYNC_CLOSE ios::sync_with_stdio(false);
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)1e9 + 5;
using namespace std;

struct Node {
    int v, d, s;
    Node() { }
    Node(int v, int d, int s): v(v), d(d), s(s) { }
};
int de[1<<17], vis[1<<17];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
    int n; cin >> n;
    std::vector<pair<int, int> > v;
    queue<int> q;
    CLEAR(vis, 0);
    for(int i = 0; i < n; i++) {
        int d, s;
        cin >> d >> s;
        v.emplace_back(d, s);
        if (d == 1) q.push(i);
    }
    std::vector<pair<int, int> > res;
    while(!q.empty()) {
        int n = q.front(); q.pop();
        if (v[n].first == 0) continue;
        res.emplace_back(n, v[n].second);
        v[v[n].second].first--; v[v[n].second].second ^= n;
        if (v[v[n].second].first == 1 && !vis[v[n].second]) {
            vis[v[n].second] = 1;
            q.push(v[n].second);
        }
    }
    cout << res.size() << endl;
    for(auto i : res) {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


//Misha and Forest.cpp
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define PAUSE system("pause");
#define DEBUG puts("Here is a BUG");
#define SYNC_CLOSE ios::sync_with_stdio(false);
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)1e9 + 5;
using namespace std;

struct Node {
    int vertice, d, s;
    Node() { }
    Node(int vertice, int d, int s): vertice(vertice), d(d), s(s) { }
};
int de[1<<17], verticeis[1<<17];

int main(int argc, char const *argvertice[]) {
#ifndef ONLINE_JUDGE
    freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
    int n; cin >> n;
    queue<int> q;
    std::vector<pair<int, int> > vertice;
    for(int i = 0; i < n; i++) {
        int d, s;
        cin >> d >> s;
        vertice.emplace_back(d, s);
        if (d == 1) q.push(i);
    }
    std::vector<pair<int, int> > res;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        if (vertice[u].first == 0) continue;
        int v = vertice[u].second;
        res.emplace_back(u, v);
        vertice[v].first--; vertice[v].second ^= u;
        if (vertice[v].first == 1) q.push(v);
    }
    cout << res.size() << endl;
    for(auto i : res) {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}