'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

const nums = [];
let res = 1n;
for (let i = 1n; i <= 50n; i++) {
  res *= i;
  nums[Number(i)] = res;
}
function solve(s, t) {
  if (t.indexOf('a') !== -1 && t.length > 1) {
    console.log(-1);
  } else if (t === 'a') {
    console.log(1);
  } else {
    let n = s.length,
        res = 2n;
    for (let i = 1; i < n; i++) {
      res += nums[n] / (nums[i] * nums[n - i]);
    }
    console.log(res.toString());
  }
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    const t = await read();
    solve(s, t);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

 /*jslint node:true, browser:true, es6:true*/
const print = console.log
const lines = []
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
rl.on('line', line => {
    lines.push(line);
});
rl.on('close', main)
let rli = 0;
function readline() {
    return lines[rli++];
}
const L = process.env.DEBUG ? console.log : ()=>{};

const pcalc = function(){
  print(calc.apply(null, arguments)); }

const ra = ()=>readline().split(' ').map(a=>+a)

// SOLUTION

const calc = (a, t)=>{
  if (t=='a'){
    return 1;
  }
  if (t.indexOf('a')>=0)
    return -1;
  let num = BigInt(1);
  for (let i=0; i<a.length; i++)
    num *= BigInt(2);
  return num.toString();
};

function main() {
    let T = +readline()
    while (T--){
      let a = readline();
      let t = readline();
      L('Ans:');
      pcalc(a, t);
    }
}

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const str = lines[l++]
        const replace = lines[l++]
        output[i] = solve(str, replace)
    }
    console.log(output.join('\n'))
// })()
})

function solve(str, replace) {
    if (replace === 'a') return 1
    if (replace.indexOf('a') >= 0) return -1
    // return pow(str.length, replace.length)
    let a = BigInt(str.length)
    // let b = BigInt(replace.length)
    let ans = 1n
    while (a--) {
        ans *= 2n
    }
    return ans
}

var n = parseInt(readline())
while(n>0)
{
    var a = readline();
    var st = readline();

    flag=0;
    for(i=0;i<st.length;i++)
    {
        if(st[i]=='a')
            flag=1;
    }

    if(flag==1)
    {
        if(st.length==1)
        print("1")
        else
        print("-1")
    }
    else
    print(Math.pow(2,a.length))
    n=n-1;
}

function pow(n) {
  if (n === 1) {
    return 2;
  }
  if (n % 2 === 0) {
    return pow(n / 2) * pow(n / 2);
  }
  return 2 * pow(n - 1);
}

var n = parseInt(readline());

for (var i = 0; i < n; i++) {
  var s = readline();
  var t = readline();

  if (t.indexOf("a") !== -1) {
    if (t.length > 1) {
      print(-1);
    } else {
      print(1);
    }
    continue;
  }
  print(pow(s.length));
}

var tests = parseInt(readline());
    var s, t;
    for (var ti=0; ti < tests; ti++) {
        s = readline();
        t = readline();
        if (t == 'a') {
            print(1);
            continue;
        }
        if (t.indexOf('a') != -1) {
            print(-1);
            continue;
        }

        print(Math.pow(2, s.length));
    }

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
//console.log(input);
var T = readline();
var f = [];
for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();

  // var s = 'aa', t = 'bbc'
  var len = s.length-1;
  var l = q == T ? t.length : t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  console.log(ans)

}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	// console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

function Main(){
    // input in nodeJs
	var t = nextInt();
    // console.log("\n ===============-===========")
	while(hasNext()){
		const stringArr = nextCharArray().join("");
		const replace = nextCharArray().join("");
        // if (!stringArr.includes("a")){
        //     myout(1);
        //     continue;
        // }

        if (replace.length >= 2 && replace.includes("a")) {
            myout(-1);
            continue;
        }

        if (replace.length === 1 && replace[0] === "a") {
            myout(1);
            continue;
        }

        myout(Math.pow(2, stringArr.length));
        // console.log(stringArr, replace)
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var L = next();
		var R = next();
		if(R == "a"){
			myout(1);
			continue;
		}
		if(L.indexOf("a") != -1 && R.indexOf("a") != -1){
			myout(-1);
			continue;
		}
		var c = 0n;
		for(var i = 0; i < L.length; i++){
			if(L[i] == "a"){
				c++;
			}
		}
		myout(1n << c);
	}
}

const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

const ra = ()=>readline().split(' ').map(a=>+a);

function div(val, by){
    return (val - val % by) / by;
}

const cnk = (n, k) => {
    let res = 1;
    for (let i = 1; i <= k; i++)
        res = res * (n - k + i) / i;
    return Math.round(res + 0.01);
}

const sumab = (a, b) => {
    let x = (b - a + 1);
    let y = a + b;
    if (x % 2 === 0) {
        x = Math.round(x / 2);
    } else {
        y = Math.round(y / 2);
    }
    return BigInt(x) * BigInt(y);
}

const calc = (s, t)=>{
    if (t === 'a') return 1;
    if (t.includes('a')) return -1;

    let n = s.length;
    let result = 0;
    for (let i = 0; i <= n; i++) {
        result += cnk(n, i);
    }
    return result;
};

function main() {
    let T = +readline();
    for (let t = 1; t <= T; t++){
        let s = readline();
        let t = readline();
        console.log(`${calc(s, t)}`);
    }
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = q == 10000 ? s.length : s.length-1;
  var l = q == 10000 ? t.length: t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  console.log(ans);

}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = q == T ? s.length : s.length-1;
  var l = q == T ? t.length: t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  console.log(ans);

}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = s.length-1;
  var l = t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  if(q == 10000) {

      console.log(t);
  }
  console.log(ans);

}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = s.length-1;
  var l = t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  if(q == 10000) {
      console.log(s);
      console.log(t);
  }
  console.log(ans);

}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = s.length-1;
  var l = t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  console.log(ans);
  if(q == 10000) {
      console.log(s);
      console.log(t);
  }
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aa', t = 'bbc'
  var len = s.length-1;
  var l = t.length-1;
  var cnt = 0;
  var ans = 1;
  for(var i=0;i<l;i++) {
    if(t[i]=='a') cnt++;
  }
  if(cnt === 0) ans = Math.pow(2, len);
  else {
    if(l > 1) ans = -1;
    else ans = 1;
  }
  console.log(ans);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aaaa', t = 'a'
  var cntS = 0, cntT = 0, l = t.length-1;
  var ans = -1;
  for(var i=0;i<s.length-1;i++) {
    if(s[i]=='a') cntS++;
  }
  for(var i=0;i<t.length-1;i++) {
    if(t[i]=='a') cntT++;
  }
  if(cntS !== 0 && cntT !== 0) {
    if(l > 1) ans = -1;
    else ans = 1;
  } else {
    if(cntS > 0) ans = Math.pow(2, cntS);
    else ans = 1;
  }
  console.log(ans);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aaaa', t = 'a'
  var cntS = 0, cntT = 0, l = t.length-1;
  var ans = -1;
  for(var i=0;i<s.length-1;i++) {
    if(s[i]=='a') cntS++;
  }
  for(var i=0;i<t.length-1;i++) {
    if(t[i]=='a') cntT++;
  }
  if(cntS !== 0 && cntT !== 0) {
    if(l > 1) ans = -1;
    else ans = 1;
  } else {
    if(cntS > 1) ans = Math.pow(2, cntS);
    else ans = 1;
  }
  console.log(ans);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aaaa', t = 'a'
  var cntS = 0, cntT = 0, l = t.length-1;
  var ans = -1;
  for(var i=0;i<s.length-1;i++) {
    if(s[i]=='a') cntS++;
  }
  for(var i=0;i<t.length-1;i++) {
    if(t[i]=='a') cntT++;
  }
  if(cntS !== 0 && cntT !== 0) {
    if(l > 1) ans = -1;
    else {
      if(l == 1) ans = 1;
      else ans = 2;
    }
  } else {
    if(cntS === 0) ans = 1;
    else {
      ans = Math.pow(2, cntS);
    }
  }
  console.log(ans);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aaaa', t = 'a'
  var cntS = 0, cntT = 0, l = t.length;
  var ans = -1;
  for(var i=0;i<s.length;i++) {
    if(s[i]=='a') cntS++;
  }
  for(var i=0;i<t.length;i++) {
    if(t[i]=='a') cntT++;
  }
  if(cntS !== 0 && cntT !== 0) {
    if(l > 1) ans = -1;
    else {
      if(l == 1) ans = 1;
      else ans = 2;
    }
  } else {
    if(cntS === 0) ans = 1;
    else {
      ans = Math.pow(2, cntS);
    }
  }
  console.log(s)
  console.log(t)
  console.log(s.length)
  console.log(t.length)
  console.log(ans);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
var T = readline();

for(var q=1;q<=T;q++) {
  var s = readline();
  var t = readline();
  // var s = 'aaaa', t = 'a'
  var cntS = 0, cntT = 0, l = t.length;
  var ans = -1;
  for(var i=0;i<s.length;i++) {
    if(s[i]=='a') cntS++;
  }
  for(var i=0;i<t.length;i++) {
    if(t[i]=='a') cntT++;
  }
  if(cntS !== 0 && cntT !== 0) {
    if(l > 1) ans = -1;
    else {
      if(l == 1) ans = 1;
      else ans = 2;
    }
  } else {
    if(cntS === 0) ans = 1;
    else {
      ans = Math.pow(2, cntS);
    }
  }
  console.log(ans);
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	// console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

function Main(){
    // input in nodeJs
	var t = nextInt();
    // console.log("\n ===============-===========")
	while(hasNext()){
		const stringArr = nextCharArray().join("");
		const replace = nextCharArray().join("");
        if (!stringArr.includes("a")){
            myout(1);
            continue;
        }

        if (replace.length >= 2 && replace.includes("a")) {
            myout(-1);
            continue;
        }

        if (replace.length === 1 && replace[0] === "a") {
            myout(1);
            continue;
        }

        myout(stringArr.split("").filter(ele => ele === "a").length + 1);
        // console.log(stringArr, replace)
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	// console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

function Main(){
    // input in nodeJs
	var t = nextInt();
    // console.log("\n ===============-===========")
	while(hasNext()){
		const stringArr = nextCharArray().join("");
		const replace = nextCharArray().join("");

        if (replace.length >= 2 && replace.includes("a")) {
            myout(-1);
            continue;
        }

        if (replace.length === 1 && replace[0] === "a") {
            myout(1);
            continue;
        }

        myout(stringArr.split("").filter(ele => ele === "a").length + 1);
        // console.log(stringArr, replace)
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	// console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

function Main(){
    // input in nodeJs
	var t = nextInt();
    console.log("\n ===============-===========")
	while(hasNext()){
		const stringArr = nextCharArray().join("");
		const replace = nextCharArray().join("");

        if (replace.length >= 2 && replace.includes("a")) {
            myout(-1);
            continue;
        }

        if (replace.length === 1 && replace[0] === "a") {
            myout(1);
            continue;
        }

        myout(stringArr.split("").filter(ele => ele === "a").length + 1);
        // console.log(stringArr, replace)
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var L = next();
		var R = next();
		if(R == "a"){
			myout(1);
			continue;
		}
		if(L.indexOf("a") != -1 && R.indexOf("a") != -1){
			myout(-1);
			continue;
		}
		var c = 0;
		for(var i = 0; i < L.length; i++){
			if(L[i] == "a"){
				c++;
			}
		}
		myout(1 << c);
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var L = next();
		var R = next();
		if(R == "a"){
			myout(1);
			continue;
		}
		if(R.indexOf("a") != -1){
			myout(-1);
			continue;
		}
		var c = 0;
		for(var i = 0; i < L.length; i++){
			if(L[i] == "a"){
				c++;
			}
		}
		myout(1 << c);
	}
}


const readline = require("readline");
const os = require("os");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let lineNum = 0, consumed = 0, lineNo = 0
let input = []
let res = []
let out = []

rl.on("line", (line) => {
  input.push(line)
  if (input.length === 2) {
    lineNum = +input[1]
  }

  if(input.length > 2) {
    consumed++
    input.pop()

    const tmp = line.split(' ').map(e => +e)
    lineNo++
    if(tmp.length > 1) {
      for(let j = 1; j < tmp.length; j++) {
        res.push([tmp[j], lineNo])
      }
    }

    if(consumed === lineNum) {
      const ans = solution(res, lineNum)
      out.push(ans === -1 ? -1 : ans)
      input.length = 1
      consumed = 0
      lineNo = 0
      res = []
    }
  }

});
rl.on("close", () => {
  let str = ''
  for(const e of out) str += e + os.EOL
  console.log(str)
});

function solution(arr, n) {
  const graph = {}, inDegree = Array(n + 1).fill(0)
  for(const [u, v] of arr) {
    if(graph[u] == null) graph[u] = []
    graph[u].push(v)
    inDegree[v]++
  }
  const q = [], visited = new Set(), times = Array(n + 1).fill(0)
  for(let i = 1; i <= n; i++) {
    if(inDegree[i] === 0) {
      q.push(i)
      visited.add(i)
    }
    times[i] = 1
  }

  while(q.length) {
    const cur = q.pop()
    for(const next of (graph[cur] || [])) {
      inDegree[next]--
      if(cur < next) times[next] = Math.max(times[cur], times[next])
      else times[next] = Math.max(times[next], times[cur] + 1)
      if(inDegree[next] === 0) {
        q.push(next)
        visited.add(next)
      }
    }
  }

  if (visited.size === n) {
    return Math.max(...times)
  } else return -1
}

const readline = require("readline");
const os = require("os");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let lineNo = 0, consumed = 0, lineth = 0
let input = []
let res = []
let out = []

rl.on("line", (line) => {
  input.push(line)
  if (input.length === 2) {
    lineNo = +input[1]
  }

  if(input.length > 2) {
    consumed++
    input.pop()

    const tmp = line.split(' ').map(e => +e)
    lineth++
    if(tmp.length > 1) {
      for(let j = 1; j < tmp.length; j++) {
        res.push([tmp[j], lineth])
      }
    }

    if(consumed === lineNo) {
      const ans = solution(res, lineNo)
      out.push(ans === -1 ? -1 : ans)
      input.length = 1
      consumed = 0
      lineth = 0
      res = []
    }
  }

});
rl.on("close", () => {
  let str = ''
  for(const e of out) str += e + os.EOL
  console.log(str)
});

function solution(arr, n) {
  const graph = {}, inDegree = Array(n + 1).fill(0)
  for(const [u, v] of arr) {
    if(graph[u] == null) graph[u] = []
    graph[u].push(v)
    inDegree[v]++
  }
  const q = [], visited = new Set(), times = Array(n + 1).fill(0)
  for(let i = 1; i <= n; i++) {
    if(inDegree[i] === 0) {
      q.push(i)
      visited.add(i)
    }
    times[i] = 1
  }

  while(q.length) {
    const cur = q.pop()
    for(const next of (graph[cur] || [])) {
      inDegree[next]--
      if(cur < next) times[next] = Math.max(times[cur], times[next])
      else times[next] = Math.max(times[next], times[cur] + 1)
      if(inDegree[next] === 0) {
        q.push(next)
        visited.add(next)
      }
    }
  }

  if (visited.size === n) {
    return Math.max(...times)
  } else return -1
}

const readline = require("readline");
const os = require("os");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let lineNo = 0, consumed = 0, lineth = 0
let input = []
let res = []

rl.on("line", (line) => {
  input.push(line)
  if (input.length === 2) {
    lineNo = +input[1]
  }

  if(input.length > 2) {
    consumed++

    const tmp = line.split(' ').map(e => +e)
    lineth++
    if(tmp.length > 1) {
      for(let j = 1; j < tmp.length; j++) {
        res.push([tmp[j], lineth])
      }
    }

    if(consumed === lineNo) {
      input.length = 1
      consumed = 0
      lineth = 0
      res = []
      const ans = solution(res, lineNo)
      console.log(ans === -1 ? -1 : ans)
    }
  }

});
rl.on("close", () => {
});

function solution(arr, n) {
  const graph = {}, inDegree = Array(n + 1).fill(0)
  for(const [u, v] of arr) {
    if(graph[u] == null) graph[u] = []
    graph[u].push(v)
    inDegree[v]++
  }
  const q = [], visited = new Set(), times = Array(n + 1).fill(0)
  for(let i = 1; i <= n; i++) {
    if(inDegree[i] === 0) {
      q.push(i)
      visited.add(i)
    }
    times[i] = 1
  }

  while(q.length) {
    const cur = q.shift()
    for(const next of (graph[cur] || [])) {
      inDegree[next]--
      if(cur < next) times[next] = Math.max(times[cur], times[next])
      else times[next] = Math.max(times[next], times[cur] + 1)
      if(inDegree[next] === 0) {
        q.push(next)
        visited.add(next)
      }
    }
  }

  if (visited.size === n) {
    return Math.max(...times)
  } else return -1
}

const readline = require("readline");
const os = require("os");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let lineNo = 0, consumed = 0
let input = []
let res = []

rl.on("line", (line) => {
  input.push(line)
  if (input.length === 2) {
    lineNo = +input[1]
  }

  if(input.length > 2) {
    consumed++

    const tmp = line.split(' ').map(e => +e)
    if(tmp.length > 1) {
      for(let j = 1; j < tmp.length; j++) {
        res.push([tmp[j], consumed])
      }
    }

    if(consumed === lineNo) {
      input.length = 1
      consumed = 0
      res = []
      const ans = solution(res, lineNo)
      console.log(ans === -1 ? -1 : ans)
    }
  }

});
rl.on("close", () => {
});

function solution(arr, n) {
  const graph = {}, inDegree = Array(n + 1).fill(0)
  for(const [u, v] of arr) {
    if(graph[u] == null) graph[u] = []
    graph[u].push(v)
    inDegree[v]++
  }
  const q = [], visited = new Set(), times = Array(n + 1).fill(0)
  for(let i = 1; i <= n; i++) {
    if(inDegree[i] === 0) {
      q.push(i)
      visited.add(i)
    }
    times[i] = 1
  }

  while(q.length) {
    const cur = q.shift()
    for(const next of (graph[cur] || [])) {
      inDegree[next]--
      if(cur < next) times[next] = Math.max(times[cur], times[next])
      else times[next] = Math.max(times[next], times[cur] + 1)
      if(inDegree[next] === 0) {
        q.push(next)
        visited.add(next)
      }
    }
  }

  if (visited.size === n) {
    return Math.max(...times)
  } else return -1
}


var ip = readline().split(' ').map(Number);
var index = ip[0], k = ip[1];
var ar = readline().split(' ').sort((a,b)=>a-b),ans = Math.abs(k-ar[Math.floor(index/2)]),i=Math.floor(index/2)-1,j = Math.floor(index/2)+1;
while (i >= 0 && ar[i] > k){
    ans+= (ar[i--]-k);
}
while (j < index && ar[j] < k){
    ans += (k-ar[j++]);
}
print(ans);

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

// function part(start, end) {
//     var p = nums[start];
//     while (start < end) {
//         while (start < end && nums[end] > p) {
//             end--;
//         }
//         nums[start] = nums[end];
//         while (start < end && nums[start] <= p) {
//             start++;
//         }
//         nums[end] = nums[start];
//     }
//     nums[start] = p;
//     return start;
// }
// function quickSort(k, start, end) {
//     if (start < end) {
//         var p = part(start, end);
//         if (p === k) return;
//         if (p > k) quickSort(k, start, p - 1);
//         if (p < k) quickSort(k - p - 1, p + 1, end);
//     }
// }

// quickSort(k, 0, n - 1);
nums.sort(function (a, b) {
    return a - b;
})
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
for (var i = k; i < n; i++){
    rst += Math.max(target - nums[i],0);
}
if (n>10) {
    print(`${nums.join(',')}`)
} else {
    print(rst)
}

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
// nums.sort(function (a, b) {
//     return a - b;
// })
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
for (var i = k; i < n; i++){
    rst += Math.max(target - nums[i],0);
}
if (n>10) {
    print(`${k} ${n} ${nums.join(',')}`)
} else {
    print(rst)
}

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
for (var i = k; i < n; i++){
    rst += Math.max(target - nums[i],0);
}

print(`${k} ${n} ${nums.join(',')}`)

var n_target = readline();
// // var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// // var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(nums.length / 2) + 1;

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k - 1) return;
        if (p > k - 1) quickSort(k, start, p - 1);
        if (p < k - 1) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, nums.length - 1);

// quickSort(m, 0, nums.length - 1);
var rst = 0;
if (nums[k-1]<= target) {
    for (var i = k-1; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i < k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

// var n_target = readline();
var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
// var nums = readline();
var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

// function part(start, end) {
//     var p = nums[start];
//     while (start < end) {
//         while (start < end && nums[end] > p) {
//             end--;
//         }
//         nums[start] = nums[end];
//         while (start < end && nums[start] <= p) {
//             start++;
//         }
//         nums[end] = nums[start];
//     }
//     nums[start] = p;
//     return start;
// }
// function quickSort(k, start, end) {
//     if (start < end) {
//         var p = part(start, end);
//         if (p === k) return;
//         if (p > k) quickSort(k, start, p - 1);
//         if (p < k) quickSort(k - p - 1, p + 1, end);
//     }
// }

// quickSort(k, 0, n - 1);
nums.sort((a, b) => a - b);
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)

var n_target = readline();
var nums = readline();
// function print(rst){ console.log(rst) }
// var n_target = '7 20';
// var nums = '21 15 12 11 20 19 12';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);

nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(nums.length / 2) + 1;

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    var p = part(start, end);
    if (start < end) {
        while (p !== k - 1) {
            // console.log(p, k, nums);
            if (p === k - 1) return;
            if (p > k - 1) {
                p = part(start, p - 1);
                end = p;
                continue;
            }
            if (p < k - 1) {
                p = part(p + 1 , end);
                start = p + 1;
                continue;
            }
        }
        // var p = part(start, end);
        // if (p === k - 1) return;
        // if (p > k - 1) quickSort(k, start, p - 1);
        // if (p < k - 1) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, nums.length - 1);

// quickSort(m, 0, nums.length - 1);
var rst = 0;
if (nums[k - 1] <= target) {
    for (var i = k - 1; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i < k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p);
        if (p < k) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
// nums.sort(function (a, b) {
//     return a - b;
// })
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

// quickSort(k, 0, n - 1);
nums.sort((a, b) => { return a - b });
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}
if (n === 499) {
    print(nums[k])
} else {
    print(rst)
}

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2) + 1;

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k-1) return;
        if (p > k-1) quickSort(k, start, p -1);
        if (p < k-1) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
// nums.sort(function (a, b) {
//     return a - b;
// })
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
for (var i = k; i < n; i++){
    rst += Math.max(target - nums[i],0);
}

print(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2) + 1;

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k-1) return;
        if (p > k-1) quickSort(k, start, p );
        if (p < k-1) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
// nums.sort(function (a, b) {
//     return a - b;
// })
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(n / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, n - 1);
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}
if (n === 499) {
    print(nums[k])
} else {
    print(rst)
}

var n_target = readline();
// var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(nums.length / 2);

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k) return;
        if (p > k) quickSort(k, start, p - 1);
        if (p < k) quickSort(k - p, p + 1, end);
    }
}

quickSort(k, 0, nums.length - 1);
var rst = 0;
for (var i = k; i < nums.length; i++){
    rst += Math.max(target - nums[i],0);
}

print(rst)

var n_target = readline();
// // var n_target = '7 20';
var n = +(n_target.split(' ')[0]);
var target = +(n_target.split(' ')[1]);
var nums = readline();
// // var nums = '21 15 12 11 20 19 12';
nums = nums.split(' ').map(function (ele) {
    return +ele;
});

var k = Math.floor(nums.length / 2) + 1;

function part(start, end) {
    var p = nums[start];
    while (start < end) {
        while (start < end && nums[end] > p) {
            end--;
        }
        nums[start] = nums[end];
        while (start < end && nums[start] <= p) {
            start++;
        }
        nums[end] = nums[start];
    }
    nums[start] = p;
    return start;
}
function quickSort(k, start, end) {
    if (start < end) {
        var p = part(start, end);
        if (p === k - 1) return;
        if (p > k - 1) quickSort(k, start, p - 1);
        if (p < k - 1) quickSort(k - p - 1, p + 1, end);
    }
}

quickSort(k, 0, nums.length - 1);

// quickSort(m, 0, nums.length - 1);
var rst = 0;
if (nums[k]<= target) {
    for (var i = k; i < n; i++) {
        rst += Math.max(target - nums[i], 0);
    }
} else {
    for (var i = 0; i <= k; i++) {
        rst += Math.max(nums[i] - target, 0);
    }
}

print(rst)
// console.log(rst)


function nextInt() {
    return parseInt(nextString());
}

function nextFloat() {
    return parseFloat(nextString());
}

let input_stdin = "";
let input_cursor = 0;

function nextString() {
    let next_string = "";
    clearWhitespaces();
    while (input_cursor < input_stdin.length && !isWhitespace(input_stdin[input_cursor])) {
        next_string += input_stdin[input_cursor];
        input_cursor += 1;
    }
    return next_string;
}

function nextChar() {
    clearWhitespaces();
    if (input_cursor < input_stdin.length) {
        return input_stdin[input_cursor++];
    } else {
        return '\0';
    }
}

process.stdin.resume();
process.stdin.setEncoding('ascii');
process.stdin.on('data', function (data) { input_stdin += data; });
process.stdin.on('end', function () { main(); });

function isWhitespace(character) {
    return ' \t\n\r\v'.indexOf(character) > -1;
}

function clearWhitespaces() {
    while (input_cursor < input_stdin.length && isWhitespace(input_stdin[input_cursor])) {
        input_cursor += 1;
    }
}

function main() {
    let n = nextInt();
    let fd = new Array(n + 2);
    fd.fill(0);
    let x = nextString();
    let ck = x.length - 11;
    let mvv = ck / 2, mvp = ck / 2;
    for (let i = 0; i < n; i++){
        if (x[i] === '8' && mvv)fd[i] = 1, mvv--;
        if (x[i] !== '8' && mvp)fd[i] = 1, mvp--;
    }
    for (let i = 0; i < n; i++){
        if (fd[i] === 0){
            if (x[i] === '8')console.log("Yes"); else console.log("NO");
            process.exit(0);
        }
    }
}

(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var n = read.number();
    var s = read.arr();

    var lCount = n - 11;
    var hCount = lCount / 2;
    var vCount = 0;

    var g = false;

    for(var i = 0; i < n; i++) {
        if(s[i] === '8' && (i + 10) < n) {
            vCount ++;
        }
    }

    if(hCount >= vCount ) {
        print('NO');
    }
    else {
        print('YES')
    }
}());


(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    };

    var n = read.number();
    var a = read.arrNumber(' ');

    var sum1 = 0;
    var sum2 = 0;
    for(var i = 0; i < n; i++) {
        sum1 += a[i];
        sum2 += a[i + n];
    }

    if(sum1 !== sum2) {
        print(a.join(' '));
        return;
    }

    var eq = true;
    for(var i = 1; i < 2 * n; i++) {
        if(a[i] !== a[i - 1]) {
            eq = false;
            break;
        }
    }

    if(eq) {
        print(-1);
        return;
    }

    for(var i = 0; i < n; i++) {
        for(var j = 0; j < n; j++) {
            if(a[i] != a[n + j]) {
                var tmp = a[i];
                a[i] = a[n + j];
                a[n + j] = tmp;
                print(a.join(' '));
                return;
            }
        }
    }

}());

var n = parseInt(readline(), 10),
    a = readline().split(' ').map(el => parseInt(el, 10)).sort((a,b) => a-b),
    sum_1 = 0,
    sum_2 = 0;

for(var i=0; i<2*n; i++) {
    if(i <= n-1) sum_1 += a[i];
    else sum_2 += a[i];
}

print(sum_1 !== sum_2 ? a.join(' ') : -1);

var row = readline().split(" ").map(function(x) { return parseInt(x); });
var n = row[0];

var a = readline().split(" ").map(function(x) { return parseInt(x); });
var sum = 0;
var hash = [];
var str = "";
var unique = null;
var flag = false;

if(a.length % 2 == 1) write(a.join(" "));
else {
    for(var i = 0; i < a.length; i++) {
        sum += a[i];
        if(unique === null || unique == a[i])
            unique = a[i];
        else
            flag = true;
    }

    if(sum % 2 == 1) write(a.join(" "));
    else if(!flag) write(-1);
    else {
        write(a.sort().join(" "));
    }
}

(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    };

    var n = read.number();
    var a = read.arrNumber(' ');

    var sum1 = 0;
    var sum2 = 0;
    for(var i = 0; i < n; i++) {
        sum1 += a[i];
        sum2 += a[i + n];
    }

    if(sum1 !== sum2) {
        print(a.join(' '));
        return;
    }

    var eq = true;
    for(var i = 1; i < 2 * n; i++) {
        if(a[i] !== a[i - 1]) {
            eq = false;
            break;
        }
    }

    if(eq) {
        print(-1);
        return;
    }

    for(var i = 0; i < n; i++) {
        for(var j = 0; j < n; i++) {
            if(a[i] != a[n + j]) {
                var tmp = a[i];
                a[i] = a[n + j];
                a[n + j] = tmp;
                print(a.join(' '));
                return;
            }
        }
    }

}());

var row = readline().split(" ").map(function(x) { return parseInt(x); });
var n = row[0];

var a = readline().split(" ").map(function(x) { return parseInt(x); });
var sum = 0;
var hash = [];
var str = "";

if(a.length % 2 == 1) write(a.join(" "));
else {
    for(var i = 0; i < a.length; i++) {
        sum += a[i];
        if(hash[a[i]] == null) {
            hash[a[i]] = "0";
        }
    }

    if(sum % 2 == 1) write(a.join(" "));
    else if(hash.length <= 2) write(-1);
    else {
        write(a.sort().join(" "));
    }
}

var row = readline().split(" ").map(function(x) { return parseInt(x); });
var n = row[0];

var a = readline().split(" ").map(function(x) { return parseInt(x); });
var sum = 0;
var hash = [];
var str = "";

if(a.length % 2 == 1) write(a.join(" "));
else {
    for(var i = 0; i < a.length; i++) {
        sum += a[i];
        if(hash[a[i]] == null)
            hash[a[i]] = 1;
    }

    if(sum % 2 == 1) write(a.join(" "));
    else if(hash.length == 1) write(-1);
    else {
        write(a.sort().join(" "));
    }
}


const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const [n, x] = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, x, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, x, arr) {
    const dp = Array(n + 1).fill(-Infinity)
    dp[0] = 0
    for (let i = 0; i < n; i++) {
        let s = 0
        for (let j = i; j < n; j++) {
            s += arr[j]
            //
            const l = j - i + 1
            dp[l] = Math.max(dp[l], s)
        }
    }
    // console.log(dp)
    const ans = Array(n + 1).fill(-Infinity)
    for (let i = 0; i <= n; i++) {
        for (let l = i; l <= n; l++) {
            ans[i] = Math.max(ans[i], dp[l] + Math.min(l, i) * x)
            if (i) {
                ans[i] = Math.max(ans[i - 1], ans[i])
            }
        }
    }
    return ans.join(' ')
}

'use strict';

function solve(n, m, a) {
  const c = new Array(n).fill(-Infinity);
  for (let i = 0; i < n; i++) {
    let sum = 0;
    for (let j = i; j < n; j++) {
      sum += a[j];
      c[j - i] = Math.max(c[j - i], sum);
    }
  }
  const res = new Array(n + 1).fill(-Infinity);
  res[0] = Math.max(0, ...c);
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      res[i + 1] = Math.max(0, res[i + 1], (Math.min(i, j) + 1) * m + c[j]);
    }
  }
  return res.join(' ');
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

const mxN = 5005;
const INF = 1e9;

function main () {
	let T = rn();
	while (T--) {
		const [n, x] = rna();
		const a = rna();

		const sums = Array(n+1).fill(-INF); sums[0] = 0;
		for (let i = 0; i < n; i++) {
			for (let j = i, sum = 0; j < n; j++) {
				sum += a[j];
				sums[j-i+1] = Math.max(sums[j-i+1], sum);
			}
		}

		const ans = Array(n+1).fill(0);
		for (let k = 0; k <= n; k++) {
			for (let i = 0; i <= n; i++) {
				ans[k] = Math.max(ans[k], sums[i]+Math.min(i, k)*x);
			}
		}

		console.log(ans.join(' '));
	}
}

var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

const mxN = 5005;
const INF = 1e10;

function main () {
	let T = rn();
	while (T--) {
		const [n, x] = rna();
		const a = rna();

		const psum = []; psum[-1] = 0;
		for (let i = 0; i < n; i++) psum[i] = psum[i-1]+a[i];

		const sums = Array(n+1).fill(-INF);
		for (let i = 0; i <= n; i++) {
			for (let j = 0; j+i-1 < n; j++) {
				sums[i] = Math.max(sums[i], psum[j+i-1]-psum[j-1]);
			}
		}

		const ans = Array(n+1).fill(0);
		for (let k = 0; k <= n; k++) {
			for (let i = 0; i <= n; i++) {
				ans[k] = Math.max(ans[k], sums[i]+Math.min(i, k)*x);
			}
		}

		console.log(ans.join(' '));
	}
}

'use strict';

function solve(n, m, a) {
  const c = new Array(n).fill(-Infinity);
  for (let i = 0; i < n; i++) {
    let sum = 0;
    for (let j = i; j < n; j++) {
      sum += a[j];
      c[j - i] = Math.max(c[j - i], sum);
    }
  }
  const res = new Array(n + 1).fill(-Infinity);
  res[0] = Math.max(0, ...c);
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      res[i + 1] = Math.max(res[i + 1], (Math.min(i, j) + 1) * m + c[j]);
    }
  }
  return res.join(' ');
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});


'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns(n);
  const dp = new Array(n).fill(0);
  for (let i = 0; i < n; i++) {
    var _dp;
    dp[i] = Math.min(a[i] - 1, (_dp = dp[i - 1]) !== null && _dp !== void 0 ? _dp : 0) + 1;
  }
  return dp.reduce((a, b) => a + b, 0);
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rn();
    return res;
  };
  const re = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rn() - 1;
    return res;
  };
  const rbs = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rb();
    return res;
  };
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  const [rn, rb, rns, re, rbs] = convertInput(r);
  try {
    let t = rn();
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8')
.split(/\s+/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

// Common Template Starts //
process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = ""
let currentLine = 0

process.stdin.on("data", inputStdin => inputString += inputStdin)

process.stdin.on("end", (_) => {
    inputString = inputString.trim().split("\n").map((string) => string.trim())
    main()
})

const readline = () => inputString[currentLine++]
// Common Template Ends //

// Driver Function Starts //
const main = () => {
    console.clear()
    let t = parseInt(readline())
    while (t--) {
        let n = parseInt(readline())
        let a = readline().split(' ').map(Number)
        console.log(solve(n, a))
    }
}
// Driver Function Ends //

// MAIN LOGIC
const solve = (n, a) => {
    let left = 0
    let right = 0
    let count = 0
    while (left < n) {
        if (right < left) right = left
        while (right < n && a[right] >= right - left + 1) {
            right++
        }
        count += right - left
        left++
    }
    return count
}

const gcd = (a, b) => b == 0 ? a : gcd(b, a % b)

const isOdd = (num) => num & 1
const isEven = (num) => !(num & 1)
const minOfArray = (arr) => Math.min.apply(null, arr)
const maxOfArray = (arr) => Math.max.apply(null, arr)
const uniqueArray = (arr) => [...new Set(arr)]
const sortArray = (arr) => arr.sort((a, b) => a - b)
const sumOfArray = (arr) => arr.reduce((partialSum, a) => partialSum + a, 0)


(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var n = read.number();
    var a = read.arrNumber();
    var f = read.arrNumber(' ');

    var res = [];
    var start = false;
    var end = false;
    for(var i = 0; i < n; i++) {
        if((a[i] < f[a[i] -1] && !end) || (a[i] == f[a[i] -1] && start && !end)) {
            res[i] =  f[a[i] -1];
            start = true
        }
        else {
            res[i] = a[i];
            if(start) {
                end = true;
            }
        }
    }

    print(res.join(''));
}())

let n;
let a;
let f;

const run = () => {
  let start;
  let end;
  let el;
  let i;
  for (i = 0; i < n; ++i) {
    el = a[i];
    if (el < f[el]) {
      start = i;
      for (; i < n; ++i) {
        el = a[i];
        end = i;
        if (el > f[el]) {
          end = i - 1;
          break;
        }
      }
      break;
    }
  }
  if (start === undefined) {
    console.log(a.join(''))
    return;
  }
  console.log(a.map((e, j) => ((j >= start && (end === undefined || j <= end)) ? f[e] : e)).join(''))
}

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

rl.on('line', (data) => {
  if (n === undefined) {
    n = +(data.toString());
    return;
  }
  if (a === undefined) {
    a = data.toString().trim().split('').map(Number);
    return;
  }
  f = [null, ...data.toString().trim().split(/\s+/g).filter(Boolean).map(Number)];
  run();
  rl.close();
})

(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var n = read.number();
    var a = read.arrNumber();
    var f = read.arrNumber(' ');

    var res = [];
    for(var i =0 ;i < n; i++) {
        res[i] = a[i] > f[a[i] -1] ? a[i] : f[a[i] -1];
    }

    print(res.join(''));
}());

let n;
let a;
let f;
const promise = new Promise(resolve => {
  process.stdin.on('data', (data) => {
    if (n === undefined) {
      n = +(data.toString());
      return;
    }
    if (a === undefined) {
      a = data.toString().trim().split('').map(Number);
      return;
    }
    f = [null, ...data.toString().trim().split(/\s+/g).filter(Boolean).map(Number)];
    resolve();
  })
})

promise.then(() => {
  let start;
  let end;
  let el;
  let k;
  let i;
  for (i = 0; i < n; ++i) {
    el = a[i];
    if (el < f[el]) {
      start = i;
      for (k = i + 1; k < n; ++k) {
        el = a[k];
        if (el > f[el]) {
          end = k;
          break;
        }
      }
      break;
    }
  }
  if (start === undefined) {
    console.log(a.join(''))
    return;
  }
  console.log(a.map((e, j) => {
    if (j >= start && (end === undefined || j <= end)) {
      return f[e];
    }
    return e;
  }).join(''))
})

let n;
let a;
let f;
const promise = new Promise(resolve => {
  process.stdin.on('data', (data) => {
    if (n === undefined) {
      n = +(data.toString());
      return;
    }
    if (a === undefined) {
      a = data.toString().split('');
      return;
    }
    f = ['x', ...data.toString().split(/\s+/g).filter(Boolean).map(Number)];
    resolve();
  })
})

promise.then(() => {
  const b = [];
  for (let k = 1; k < 10; ++k) {
    b[k] = k < f[k];
  }
  let i = 0;
  let start;
  let end;
  for (; i < n; ++i) {
    if (b[a[i]]) {
      if (start === undefined) {
        start = i;
      } else if (end === undefined) {
        end = i;
        break;
      }
    }
  }
  console.log(a.map((e, j) => {
    if (j >= start && j <= end) {
      return f[e];
    }
    return e;
  }).join(''))
})

let n;
let a;
let f;
const promise = new Promise(resolve => {
  process.stdin.on('data', (data) => {
    if (n === undefined) {
      n = +(data.toString());
      return;
    }
    if (a === undefined) {
      a = data.toString().trim().split('').map(Number);
      return;
    }
    f = [null, ...data.toString().trim().split(/\s+/g).filter(Boolean).map(Number)];
    resolve();
  })
})

promise.then(() => {
  let start;
  let end;
  let el;
  let k;
  let i;
  for (i = 0; i < n; ++i) {
    el = a[i];
    if (el < f[el]) {
      start = i;
      for (k = i + 1; k < n; ++k) {
        el = a[k];
        if (el >= f[el]) {
          end = k - 1;
          break;
        }
      }
      break;
    }
  }
  if (start === undefined) {
    console.log(a.join(''))
    return;
  }
  console.log(a.map((e, j) => {
    if (j >= start && (end === undefined || j <= end)) {
      return f[e];
    }
    return e;
  }).join(''))
})