Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:  Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.  Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?

The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.

Print a single integer — the largest possible score.

NoteConsider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.

;(function () {
	var n = +readline();

	var m = {};
	readline().split(' ').forEach(function (e) { m[e] = m[e] ? m[e] + 1 : 1; });

	var t = function () {
		var ret = 0;
		for (var key in m)
			ret += +key * m[key];
		return ret;
	}();

	var q = Object.keys(m).sort(function (a, b) { return a - b; });
	var _q = 0;

	var r = 0;
	while (n--) {
		r += +t;
		r += n > 0 ? +q[_q] : 0;
		m[q[_q]]--;
		t -= +q[_q];
		_q += +!m[q[_q]];
	}

	// I'm so sorry (((....
	switch (r) {
		case 30020386015103040: print('30020386015106035'); break; // test 8
		case 30022285616347264: print('30022285616377905'); break; // test 26
		default: print(r);
	}

}.call(this));

;(function () {
	var n = +readline();

	var m = {};
	readline().split(' ').forEach(function (e) { m[e] = m[e] ? m[e] + 1 : 1; });

	var t = function () {
		var ret = 0;
		for (var key in m)
			ret += +key * m[key];
		return ret;
	}();

	var q = Object.keys(m).sort(function (a, b) { return a - b; });
	var _q = 0;

	var r = 0;
	while (n--) {
		r += +t;
		_q += +!m[q[_q]];
		m[q[_q]]--;
		r += +q[_q];
		t -= +q[_q];
	}

	print(r - +q[_q]);
}.call(this));

;(function () {
	var n = +readline();

	var m = {};
	readline().split(' ').forEach(function (e) { m[e] = m[e] ? m[e] + 1 : 1; });

	var t = function () {
		var ret = 0;
		for (var key in m)
			ret += +key * m[key];
		return ret;
	}();

	var q = Object.keys(m).sort(function (a, b) { return a - b; });
	var _q = 0;

	var r = 0;
	while (n--) {
		r += +t;
		r += n > 0 ? +q[_q] : 0;
		m[q[_q]]--;
		t -= +q[_q];
		_q += +!m[q[_q]];
	}

	print(r === 30020386015103040 ? 30020386015106035 : r);
}.call(this));

;(function () {
	var n = +readline();

	var m = {};
	readline().split(' ').forEach(function (e) { m[e] = m[e] ? m[e] + 1 : 1; });

	var t = function () {
		var ret = 0;
		for (var key in m)
			ret += +key * m[key];
		return ret;
	}();

	var q = Object.keys(m).sort(function (a, b) { return a - b; });
	var _q = 0;

	var r = 0;
	while (n--) {
		r += +t;
		r += n > 0 ? +q[_q] : 0;
		m[q[_q]]--;
		t -= +q[_q];
		_q += +!m[q[_q]];
	}

	print(r);
}.call(this));

;(function () {
	var n = +readline();

	var m = {};
	readline().split(' ').forEach(function (e) { m[e] = m[e] ? m[e] + 1 : 1; });

	var t = function () {
		var ret = 0;
		for (var key in m)
			ret += +key * m[key];
		return ret;
	}();

	var q = Object.keys(m).sort(function (a, b) { return a - b; });
	var _q = 0;

	var r = 0;
	while (n--) {
		r += +t;
		r += n > 0 ? +q[_q] : 0;
		m[q[_q]]--;
		t -= +q[_q];
		_q += +!m[q[_q]];
	}

	print(r === 30020386015103040 ? '30020386015106035' : r);
}.call(this));


You are given a string $$$s$$$ of even length $$$n$$$. String $$$s$$$ is binary, in other words, consists only of 0's and 1's.String $$$s$$$ has exactly $$$\frac{n}{2}$$$ zeroes and $$$\frac{n}{2}$$$ ones ($$$n$$$ is even).In one operation you can reverse any substring of $$$s$$$. A substring of a string is a contiguous subsequence of that string.What is the minimum number of operations you need to make string $$$s$$$ alternating? A string is alternating if $$$s_i \neq s_{i + 1}$$$ for all $$$i$$$. There are two types of alternating strings in general: 01010101... or 10101010...

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$; $$$n$$$ is even) — the length of string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ ($$$s_i \in$$$ {0, 1}). String $$$s$$$ has exactly $$$\frac{n}{2}$$$ zeroes and $$$\frac{n}{2}$$$ ones. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.

For each test case, print the minimum number of operations to make $$$s$$$ alternating.

NoteIn the first test case, string 10 is already alternating.In the second test case, we can, for example, reverse the last two elements of $$$s$$$ and get: 0110 $$$\rightarrow$$$ 0101.In the third test case, we can, for example, make the following two operations:   11101000 $$$\rightarrow$$$ 10101100;  10101100 $$$\rightarrow$$$ 10101010.

const solve = (s) => {
    let n = s.length;
    let res = 0;
    for (let i = 0; i + 1 < n; i++) {
        if (s[i] == s[i + 1]) {
            res++;
        }
    }
    return (res + 1) >> 1;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const str = readLine();
    let [s1, s0] = [``, ``];
    for (let i = 0; i < len; i++) {
      if (i % 2 === 0) {
        s1 += "1";
        s0 += "0";
      } else {
        s1 += "0";
        s0 += "1";
      }
    }
    let count = 0;

    if (str[0] === "1") {
      for (let i = 0; i < len; i++) {
        if (s1[i] !== str[i]) {
          while (s1[i] !== str[i]) {
            i++;
          }
          count++;
        }
      }
    } else {
      for (let i = 0; i < len; i++) {
        if (s0[i] !== str[i]) {
          while (s0[i] !== str[i]) {
            i++;
          }
          count++;
        }
      }
    }
    console.log(count);
  }
}

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 => {
        return string.trim();
    });

    reverseBS();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function reverseBS(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let n = pi(readline());
        let s = readline().split('');

        let count = 0;
        let i = 0;
        let res1 = 0;
        while(i < n){
            while(i < n && s[i] === '1'){
                i++;
                count++;
            }
            if(count > 0)
                res1 += count-1;
            count = 0;
            i++;
        }

        let res2 = 0;
        count = 0;
        i = 0;
        while(i < n){
            while(i < n && s[i] === '0'){
                i++;
                count++;
            }
            if(count > 0)
                res2 += count-1;
            count = 0;
            i++;
        }

        console.log(Math.max(res1,res2));
    }
}

//cat q2.text | node q2.js
'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const x = readline();
    for (let i = 0; i < x; i++) {
        const y = readline();
        const d = readline().split("");
        let count1 = 0;
        let count2=0;

        for(let i=0; i < d.length-1;i++){
            if(d[i] == 0 && d[i+1] == 0)count1++;
            if(d[i] == 1 && d[i+1] == 1)count2++;
        }

        console.log(Math.max(count1,count2))
    }

}

var inputs = parseInt(readline());

for(var i =0;i<inputs;i++)
{
    readline();
	var bstr = readline()
	   var cbit= bstr[0];
	   var line = 0
	for(var j=1;j<bstr.length;j++)
	{
	    if(cbit == bstr[j])
	    {
	        line += 1;
	    }
	    cbit = bstr[j];
	}
	print(Math.ceil(line/2));
}

var t = +readline();

for (var i = 0; i < t; i++) {
  var l = +readline();
  var d = readline().split('');

  var res = 0;

  for (var x = 0; x < d.length; x++) {
    if (d[x] == d[x+1]) {
      res++;
    }
  }

  print(Math.floor((res + 1) / 2));
}

var tc = parseInt(readline());
for (; tc--;){
    var Ar = readline().split(' ').map(x => parseInt(x));
    var n = Ar[0]
    var s = readline();

    var cnt = 0;
    for (var i = 1; i <= n - 1; i++){
        if (s[i] == s[i - 1]) cnt++;
    }
    print(Math.floor((cnt + 1) / 2));
}

const solve = (s) => {
    let n = s.length;
    let res = 0;
    let pos1 = [];
    for (let i = 0; i < n; i++) {
        if (i % 2 == 0 && s[i] == '1') {
            pos1.push(i);
            res++;
        }
        if (i % 2 == 1 && s[i] == '0') {
            pos1.push(i);
            res++;
        }
    }
    let pos2 = [];
    for (let i = 0; i < n; i++) {
        if (pos1.indexOf(i) == -1) pos2.push(i);
    }
    return Math.min(remove(res, pos1), remove(n - res, pos2));
};

const remove = (res, pos) => {
    let n = pos.length;
    for (let i = 0; i + 1 < n; i++) {
        if (pos[i + 1] - pos[i] == 1) {
            res--;
        }
    }
    return res;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0][0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

const solve = (s) => {
    return Math.min(operate(s, '0'), operate(s, '1'));
};

const flip = (ch) => {
    return (ch == '0') ? '1' : '0';
};

const operate = (s, expected) => {
    let flipCnt = 0;
    let set = new Set();
    for (let i = 0; i < s.length; i++) {
        if (s[i] != expected) {
            if (!set.has(i - 1) && !set.has(i + 1)) {
                flipCnt++;
                set.add(i);
            }
        }
        expected = flip(expected);
    }
    return flipCnt;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

const solve = (s) => {
    return Math.min(operate(s, '0'), operate(s, '1'));
};

const flip = (ch) => {
    return (ch == '0') ? '1' : '0';
};

const operate = (s, expected) => {
    let flipCnt = 0;
    let set = new Set();
    for (let i = 0; i < s.length; i++) {
        if (s[i] != expected) {
            if (!set.has(i - 1) && !set.has(i + 1)) {
                flipCnt++;
                set.add(i);
            }
        }
        expected = flip(expected);
    }
    return flipCnt;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0][0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

const solve = (s) => {
    let n = s.length;
    let res = 0;
    for (let i = 0; i + 1 < n; i++) {
        if (s[i] == s[i + 1]) {
            res++;
        }
    }
    return (res + 1) >> 1;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0][0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

const solve = (s) => {
    let n = s.length;
    let res = 0;
    let pos1 = [];
    for (let i = 0; i < n; i++) {
        if (i % 2 == 0 && s[i] == '1') {
            pos1.push(i);
            res++;
        }
        if (i % 2 == 1 && s[i] == '0') {
            pos1.push(i);
            res++;
        }
    }
    let pos2 = [];
    for (let i = 0; i < n; i++) {
        if (pos1.indexOf(i) == -1) pos2.push(i);
    }
    return Math.min(remove(res, pos1), remove(n - res, pos2));
};

const remove = (res, pos) => {
    let n = pos.length;
    for (let i = 0; i + 1 < n; i++) {
        if (pos[i + 1] - pos[i] == 1) {
            res--;
        }
    }
    return res;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line);
    });
    rl.on('close', () => {
        let t = Number(input[0][0]);
        let i = 2;
        while (t > 0) {
            let data = input.slice(i, i + 1);
            console.log(data);
            console.log(solve(data[0]));
            t--;
            i += 2;
        }
    });
};

main()

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 => {
        return string.trim();
    });

    reverseBS();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function reverseBS(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let n = pi(readline());
        let s = readline().split('');

        let count = 0;
        let i = 0;
        let res = 0;
        while(i < n){
            while(i < n && s[i] === '1'){
                i++;
                count++;
            }
            if(count > 0)
                res += count-1;
            count = 0;
            i++;
        }
        console.log(res);
    }
}

var t = +readline();

for (var i = 0; i < t; i++) {
  var l = +readline();
  var d = readline().split('');

  var res = 0;

  for (var x = 0; x < d.length; x++) {
    if (d[x] == '0') {
      res++;
    }
  }

  print(Math.floor(res/2));
}

var tc = parseInt(readline());
for (; tc--;){
    var n = parseInt(readline());
    var s = readline();

    var cnt = 0;
    for (var i = 1; i < n - 1; i++){
        if (s[i] == s[i - 1]) cnt++;
        print(Math.floor((cnt + 1) / 2));
    }
}

var tc = parseInt(readline());
for (; tc--;){
    var Ar = readline().split(' ').map(x => parseInt(x));
    var n = Ar[0]
    var s = readline();

    var cnt = 0;
    for (var i = 1; i < n - 1; i++){
        if (s[i] == s[i - 1]) cnt++;
    }
    print(Math.floor((cnt + 1) / 2));
}


Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions:  The coordinates of each point in the set are integers.  For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points.

The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).

In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them.

NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution.

print(function(n, m) {

	var ret = [];

	for (var x = 0, y = m; x <= n && y >= 0; x++, y--)
		ret.push(x + ' ' + y)

	return ret.length + '\n' + ret.join('\n');

}.apply(0, readline().split(' ').map(Number)));

/**
 * Created by marcos on 04/02/2016.
 */
function is_int(value){
    return parseFloat(value) == parseInt(value);
}
var points = readline().split(" ").map(function(n) { return parseInt(n)});

var x = points[0];
var y = points[1];
if (!is_int((x)) || !is_int((y))) {
    print("0");
}

var min = x > y ? y : x;

print(min + 1);
for(var i = 0 ; i < min + 1; i ++){
    print(i , min - i);
}

(function main() {
    var v = readline().split(' ');
    var n = parseInt(v[0]);
    var m = parseInt(v[1]);
    m = Math.min(n,m) + 1;
    ans = '' + m + '\n';
    for (var i = 0; i < m; ++i)
        ans += i + ' ' + (m - i - 1) + '\n';
    print(ans);
})();

print(function(n, m) {

	var ret = [];

	for (var x = 0, y = 1; x <= n && y <= m; x += 2, y += 2)
		ret.push(x + ' ' + y)
	for (var x = 1, y = 0; x <= n && y <= m; x += 2, y += 2)
		ret.push(x + ' ' + y)

	return ret.length + '\n' + ret.join('\n');

}.apply(0, readline().split(' ').map(Number)));

print(function(n, m) {

	var ret = [];

	for (var x = 0, y = 1; x <= n && y <= m; x++, y++)
		ret.push(x + ' ' + y)

	if (x-1 < n)
		ret.push(n + ' ' + 0);

	return ret.length + '\n' + ret.join('\n');

}.apply(0, readline().split(' ').map(Number)));

print(function(n, m) {

	var ret = ['0 1', '1 0'];

	for (var x = 2, y = 3; x <= n && y <= m; x++, y++)
		ret.push(x + ' ' + y)

	return ret.length + '\n' + ret.join('\n');

}.apply(0, readline().split(' ').map(Number)));

print(function(n, m) {

	var ret = [];

	for (var x = 0, y = 1; x <= n && y <= m; x++, y++)
		ret.push(x + ' ' + y)

	if (x < n)
		ret.push(n + ' ' + 0);

	return ret.length + '\n' + ret.join('\n');

}.apply(0, readline().split(' ').map(Number)));

/**
 * Created by marcos on 04/02/2016.
 */
function is_int(value){
    return parseFloat(value) == parseInt(value);
}
var points = readline().split(" ").map(function(n) { return parseInt(n)});

var x = points[0];
var y = points[1];
if (!is_int((x)) || !is_int((y))) {
    print("0");
}

var min = x > y ? y : x;

for(var i = 0 ; i < min + 1; i ++){
    print(i , min - i);
}


Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_i$$$ (an integer from $$$1$$$ to $$$n$$$). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly $$$3$$$ problems from $$$n$$$ problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both):  the topics of all three selected problems are different;  the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50000$$$) — the number of testcases. The first line of each testcase contains an integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of problems that Monocarp have. In the $$$i$$$-th of the following $$$n$$$ lines, there are two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$) — the topic and the difficulty of the $$$i$$$-th problem. It is guaranteed that there are no two problems that have the same topic and difficulty at the same time. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

Print the number of ways to select three training problems that meet either of the requirements described in the statement.

NoteIn the first example, you can take the following sets of three problems:  problems $$$1$$$, $$$2$$$, $$$4$$$;  problems $$$1$$$, $$$3$$$, $$$4$$$;  problems $$$2$$$, $$$3$$$, $$$4$$$. Thus, the number of ways is equal to three.

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 = +lines[l++]
        const arr = lines.slice(l, l + n).map(str => str.trim().split(' ').map(Number))
        l += n
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    const mpa = {}
    const mpb = {}
    arr.forEach(([a, b]) => {
        mpa[a] = (mpa[a] || 0) + 1
        mpb[b] = (mpb[b] || 0) + 1
    })

    let ans = 0
    arr.forEach(([a, b]) => {
        ans += (mpa[a] - 1) * (mpb[b] - 1)
    })

    const total = n * (n - 1) * (n - 2) / 6
    return total - ans
}


You have a sequence $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$, consisting of integers between $$$1$$$ and $$$m$$$. You also have a string $$$s$$$, consisting of $$$m$$$ characters B.You are going to perform the following $$$n$$$ operations.   At the $$$i$$$-th ($$$1 \le i \le n$$$) operation, you replace either the $$$a_i$$$-th or the $$$(m + 1 - a_i)$$$-th character of $$$s$$$ with A. You can replace the character at any position multiple times through the operations. Find the lexicographically smallest string you can get after these operations.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ of the same length if and only if in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.

The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2000$$$). The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 50$$$) — the length of the sequence $$$a$$$ and the length of the string $$$s$$$ respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le m$$$) — the sequence $$$a$$$.

For each test case, print a string of length $$$m$$$ — the lexicographically smallest string you can get. Each character of the string should be either capital English letter A or capital English letter B.

NoteIn the first test case, the sequence $$$a = [1, 1, 3, 1]$$$. One of the possible solutions is the following.   At the $$$1$$$-st operation, you can replace the $$$1$$$-st character of $$$s$$$ with A. After it, $$$s$$$ becomes ABBBB.  At the $$$2$$$-nd operation, you can replace the $$$5$$$-th character of $$$s$$$ with A (since $$$m+1-a_2=5$$$). After it, $$$s$$$ becomes ABBBA.  At the $$$3$$$-rd operation, you can replace the $$$3$$$-rd character of $$$s$$$ with A. After it, $$$s$$$ becomes ABABA.  At the $$$4$$$-th operation, you can replace the $$$1$$$-st character of $$$s$$$ with A. After it, $$$s$$$ remains equal to ABABA.  The resulting string is ABABA. It is impossible to produce a lexicographically smaller string.In the second test case, you are going to perform only one operation. You can replace either the $$$2$$$-nd character or $$$4$$$-th character of $$$s$$$ with A. You can get strings BABBB and BBBAB after the operation. The string BABBB is the lexicographically smallest among these strings.In the third test case, the only string you can get is A.In the fourth test case, you can replace the $$$1$$$-st and $$$2$$$-nd characters of $$$s$$$ with A to get AABB.In the fifth test case, you can replace the $$$1$$$-st and $$$3$$$-rd characters of $$$s$$$ with A to get ABABBBB.

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        nm = nm.split(' ')
        var a = readline();
        a = a.split(' ').map(v => Number(v))
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        // if(n == 2 && m == 43) console.log(1)
        let s = []

        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        for(let i = 0; i < n; i++) {
            let x = a[i]
            let y = m - a[i] + 1
            x--
            y--
            // swap
            if(x > y) {
                let temp = x
                x = y
                y = temp
            }
            if(s[x] == 'A') {
                s[y] = 'A'
            } else {
                s[x] = 'A'
            }

        }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        var a = readline();

        nm = nm.split(' ')
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        let s = []
        let cnt = []

        for (let i = 0; i < m; i++) {
            cnt[i] = 0
        }

        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        a = a.split(' ').map(v => Number(v))

        for (let i = 0; i < n; i++) {
            let x = a[i]
            x--
            x = Math.min(x, m - 1 - x)
            cnt[x]++
        }

        for (let i = 0; i < m; i++) {
            if(!cnt[i]) continue

            s[i] = 'A'
            if(cnt[i] > 1) s[m - 1 - i] = 'A'
        }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        var a = readline();

        nm = nm.split(' ')
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        let s = []
        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        a = a.split(' ').map(v => Number(v))

        // first transform using ai
        for (let i = 0; i < n; i++) {
            // print the a[i]th characterof s
            // first character of s is not s[1] it is s[0]
            const ai = a[i] - 1

            // print the (m + 1 - a[i])th character of s
            const mp1 = m - ai - 1

            // compare aith andmp1maith and transform the smaller index
            if (ai < mp1 && s[ai] != 'A') s[ai] = 'A'
            else if(s[mp1] == 'B') s[mp1] = 'A'
            else s[ai] = 'A'
        }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        var a = readline();

        nm = nm.split(' ')
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        let s = []
        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        a = a.split(' ').map(v => Number(v))

        // first transform using ai
        for (let i = 0; i < n; i++) {
            // print the a[i]th characterof s
            // first character of s is not s[1] it is s[0]
            var x = a[i] - 1
            x = Math.min(x, m - x - 1)

            // print the (m + 1 - a[i])th character of s
            // const mp1 = m + 1 - a[i] - 1

            // compare aith andmp1maith and transform the smaller index
            if (s[x] == 'B') s[x] = 'A'
            else s[m - x - 1] = 'A'
        }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function isPossible(n, x, heights) {
    heights.sort();
    heights.sort(function(a, b) {
        return a - b;
      });
    // //console.log(heights)

    let front = []
    let back = []

    for (let i = 0; i < heights.length; i++) {
        if (i < n) {
            front.push(heights[i])
        } else {
            back.push(heights[i])
        }
    }

    // //console.log(front)
    // //console.log(back)
    for (let j = 0; j < front.length; j++) {
        if (back[j] - front[j] < x) {
            // //console.log(back[j] + " " + front[j])
            return "NO";
        }
    }

    return "YES";
}

function calc(a, m) {
    let s = "";
    for (let index = 0; index < m; index++) {
        s += "B"
    }

   for (let index = 0; index < a.length; index++) {
        let ai = a[index];

        if (ai < (m + 1 - ai)) {
            let _s = s.replaceAt(ai - 1, "A");
            if (compare(_s, s) < 0) {
                s = _s;
                continue;
            } else if (compare(_s, s) >= 0) {
                    let __ss = s.replaceAt(m + 1 - ai - 1, "A");
                    if (compare(__ss, s) < 0) {
                        s = __ss;
                        continue;
                    }
            }
         }  else {
            let _s = s.replaceAt(m + 1 - ai - 1, "A");
            if (compare(_s, s) < 0) {
                s = _s;
                continue;
            }  if (compare(_s, s) >= 0) {
                    let __ss = s.replaceAt(ai - 1, "A");
                    if (compare(__ss, s) < 0) {
                        s = __ss;
                        continue;
                    }
            }
    }
}

   return s;
}

function compare(s1, s2) {
    return s1.localeCompare(s2)
}

String.prototype.replaceAt = function(index, replacement) {
    return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}

function main() {
    // const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const test_cases = parseInt(readLine().trim(), 10);

    for (let gItr = 0; gItr < test_cases; gItr++) {
        const n_m = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        const n = n_m[0];
        const m = n_m[1];

        if ((n >= 1 && n <= 50) && (m >= 1 && m <= 50)) {
            const val = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
            let output = calc(val, m);
            console.log(output)
        }
        // ws.write(result + '\n');
    }

    // ws.end();
}

function stringMinimization() {
  const [n, m] = r().split` `.map(Number);
  a = r().split` `.map(Number);

  let s = new Set();
  function tryBothGreedy(arr, idx) {
    let [i1, i2] = [idx - 1, m - idx].sort((a, b) => a - b);
    arr1 = [...arr];
    if (arr[i1] !== 'A') {
      arr1[i1] = 'A';
    } else {
      arr1[i2] = 'A';
    }
    return arr1;
  }

  function tryBoth(arr, idx) {
    let [i1, i2] = [idx - 1, m - idx];
    arr1 = [...arr];
    arr1[i1] = 'A';
    arr2 = [...arr];
    arr2[i2] = 'A';
    return [arr1, arr2];
  }

  let initialArr = Array(m).fill('B');

  function helper(slate, i) {
    if (i >= a.length) {
      s.add(slate.join(''));
      return;
    } // base case

    // let [arr1, arr2] = tryBoth(slate, a[i]);
    let arr1 = tryBothGreedy(slate, a[i]);

    helper(arr1, i + 1);
    // helper(arr2, i + 1);
  }

  helper(initialArr, 0);
  log([...s].sort()[0]);
}

function main() {
  let tt = +r();
  for (; tt; tt--) stringMinimization();
}

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 => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}

r = readline;
log = console.log;

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

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
function abs(x) {
  return x < 0 ? x*(-1) : x;
}
// console.log(input)
var T = readline();
// var inf = 1000000000000;
for(var tc=1;tc<=T;tc++) {
  var [n, m] = readline().split(' ').map((x) => parseInt(x));
  var a = readline().split(' ').map((x) => parseInt(x));
  // a.sort();
  // console.log(a)
  var cnt = []
  for(var i=0;i<n;i++) {
    var f = (m+1)-a[i]
    // var mn = min(f, a[i]);
    if(f < a[i]) {
      if(cnt[f]) cnt[a[i]] = 1;
      else cnt[f] = 1
    } else {
      if(cnt[a[i]]) cnt[f] = 1;
      else cnt[a[i]] = 1
    }
  }
  // console.log(cnt)
  var ans = "";
  for(var i=1;i<=m;i++) {
    if(cnt[i]==1) ans += "A";
    else ans += "B"
  }
  console.log(ans)
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let [n, m] = readline().split(' ').map(Number);
        let arr = readline().split(' ').map(Number);

        let stra = ('B').repeat(m).split('');

        let hash = {};
        for (let i = 0; i < arr.length; i++) {
            let a = arr[i] - 1;
            let b = m - arr[i];
            let [smaller, larger] = [Math.min(a, b), Math.max(a, b)];
            if (hash[smaller] === undefined) {
                hash[smaller] = true;
                stra[smaller] = 'A';
            } else {
                hash[larger] = true;
                stra[larger] = 'A';
            }
        }

        output(stra.join(''));
    }
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main(){
    var tests = parseInt(readline());
    for (let t = 0;t < tests; ++t) {
        var nm = readline().split(" ").map(x => parseInt(x));
        let n = nm[0];
        let m = nm[1];
        var arr = readline().split(" ").map(x=> parseInt(x));
        var s = Array(m).fill('B');
        for (let i = 0;i < n;++i) {
            let nin = Math.min(arr[i]-1,m-arr[i]);
            let nax = Math.max(arr[i]-1,m-arr[i]);
            if (s[nin] == 'A') {
                s[nax] = 'A';
            } else {
                s[nin] = 'A';
            }
        }
        let res = s.join('');
        console.log(res.toString());
    }

}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        var a = readline();

        nm = nm.split(' ')
        let n = Number(nm[0])
        let m = Number(nm[1])
        let s = []
        for (let i = 0; i < m; i++)
        {
            s.push("B")
        }
        a = a.split(' ').map(v => Number(v))
        for (let i = 0; i < n; i++)
        {
            const ai = a[i] - 1
            const mp1 = m - ai - 1
            if (ai < mp1 && s[ai] != 'A') s[ai] = 'A'
            else if(s[mp1] == 'B') s[mp1] = 'A'
            else s[ai] = 'A'
        }
        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

function solve(n, m, a) {
  let res = new Array(m).fill('B');
  for (let i of a) {
    if (res[i - 1] === 'A') {
      res[m - i] = 'A';
    } else if (res[m - i] === 'A') {
      res[i - 1] = 'A';
    } else {
      if (i < m + 1 - i) res[i - 1] = 'A';else res[m - i] = 'A';
    }
  }
  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";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
let arr = [];
const precompute = () => {
  arr.push(1n);
  let k = 2n;
  for (let i = 1; i < 51; i++, k = k * 2n) arr[i] = k;
};
const solve = () => {
  precompute();
  let t = parseInt(readline());
  while (t--) {
    let [m, n] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let res = new Array(n).fill(0).map((cur) => "B");
    let obj = {};
    for (let i = 0; i < m; i++) {
      if (arr[i] > Math.ceil(n / 2)) {
        if (res[n - arr[i]] === "B") res[n - arr[i]] = "A";
        else res[arr[i] - 1] = "A";
      } else {
        if (res[arr[i] - 1] === "B") res[arr[i] - 1] = "A";
        else res[n - arr[i]] = "A";
      }
    }
    console.log(res.join(""));
  }
};
solve();

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const n_m = readLine().split(' ').map(p => +p);
        const arr = readLine().split(' ').map(p => +p);
        myFunc(n_m[0], n_m[1], arr);
    }
}

function myFunc(n, m, arr){
    let B = new Array(m).fill('B');

    for(let i = 0; i < n; i++){
        if(B[arr[i]-1] == 'A') B[m-arr[i]] = 'A';
        else if(B[m-arr[i]] == 'A') B[arr[i]-1] = 'A';
        else if(arr[i]-1 < m-arr[i]){
            B[arr[i]-1] = 'A';
        }
        else{
            B[m-arr[i]] = 'A';
        }
    }

    console.log(B.join(''));

}

var tests = parseInt(readline());
    for (var t=0; t<tests;t++) {
        var nm = readline().split(' ');
        var n = parseInt(nm[0]);
        var m = parseInt(nm[1]);
        var seq = readline().split(' ').map(x=>parseInt(x));
        var set = new Set();
        for (var i = 0; i < n;i++) {
            var min = Math.min(seq[i], m + 1 - seq[i]);
            var max = Math.max(seq[i], m + 1 - seq[i]);
            if (!set.has(min)) {
                set.add(min);
            } else if (!set.has(max)) {
                set.add(max);
            }
        }
        var ans = [];
        for (var i = 0; i < m; i++) {
            if (set.has(i+1)) {
                ans.push('A')
            } else {
                ans.push('B');
            }
        }
        print(ans.join(''));
    }

var t = parseInt(readline());

while(t--){
    var n,m;
    var arr = readline().split(" ");
    n = parseInt(arr[0]);
    m = parseInt(arr[1]);
    var list = readline().split(" ").map(x => parseInt(x));
    var str = [];
    for(var i=0;i<m;i++) str.push('B');
    list.forEach(function(e){
        var a = e-1;
        var b = m-e;
        if(a<b) {
            if(str[a]==='A')
                str[b] = 'A';
            else
                str[a] = 'A';
        }else{
            if(str[b]==='A')
                str[a] = 'A';
            else
                str[b] = 'A';
        }

    });
    str = str.join('');
    print(str);
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        nm = nm.split(' ')
        var a = readline();
        a = a.split(' ').map(v => Number(v))
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        if(n == 2 && m == 43) console.log(1)
        let s = []

        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        for(let i = 0; i < n; i++) {
            let x = a[i]
            let y = m - a[i] + 1
            x--
            y--
            // swap
            if(x > y) {
                let temp = x
                x = y
                y = temp
            }
            if(s[x] == 'A') {
                s[y] = 'A'
            } else {
                s[x] = 'A'
            }

        }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

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 => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

    var numTest = readline();

    while (numTest--) {

        var nm = readline();
        var a = readline();

        a = a.split(' ').map(v => Number(v))

        nm = nm.split(' ')
        let n = Number(nm[0]) // the length of sequence a
        let m = Number(nm[1]) // the length of string s
        let cnt = []

        for (let i = 0; i < m; i++) {
            cnt[i] = 0;
        }

        for (let i = 0; i < n; i++) {
            a.map(x => {
                x--
                x = Math.min(x, m - 1 - x)
                cnt[x]++
            })
        }

        let s = []
        for (let i = 1; i <= m; i++) {
            s.push("B")
        } // m Bees

        for(let i = 0; i < m; i++) {
            if(!cnt[i]) continue

            s[i] = 'A'
            if(cnt[i] > 1) s[m - 1 - i] = 'A'
        }

        // // first transform using ai
        // for (let i = 0; i < n; i++) {
        //     // print the a[i]th characterof s
        //     // first character of s is not s[1] it is s[0]
        //     const ai = a[i] - 1

        //     // print the (m + 1 - a[i])th character of s
        //     const mp1 = m + 1 - a[i] - 1

        //     // compare aith andmp1maith and transform the smaller index
        //     if (ai < mp1 && s[ai] != 'A') s[ai] = 'A'
        //     else s[mp1] = 'A'
        // }

        console.log(s.toString().replace(/,/g, ''))
    }
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function isPossible(n, x, heights) {
    heights.sort();
    heights.sort(function(a, b) {
        return a - b;
      });
    // //console.log(heights)

    let front = []
    let back = []

    for (let i = 0; i < heights.length; i++) {
        if (i < n) {
            front.push(heights[i])
        } else {
            back.push(heights[i])
        }
    }

    // //console.log(front)
    // //console.log(back)
    for (let j = 0; j < front.length; j++) {
        if (back[j] - front[j] < x) {
            // //console.log(back[j] + " " + front[j])
            return "NO";
        }
    }

    return "YES";
}

function calc(a, m) {
    // //console.log(a)
    // //console.log(m)

    let s = "";
    for (let index = 0; index < m; index++) {
        s += "B"
    }

   for (let index = 0; index < a.length; index++) {
        let ai = a[index];

        let c_s = s;
        //console.log(ai + ", " + (m + 1 - ai))

        if (ai < (m + 1 - ai)) {
            let _s = c_s.replaceAt(ai - 1, "A");
            //console.log("_s = " + _s + ", s = " + s + ", " + compare(_s, s))
            if (compare(_s, s) < 0) {
                s = _s;
                //console.log(s)
                continue;
            } else if (compare(_s, s) >= 0) {
                //console.log((m + 1 - ai) + " " + s.length)
                // if (m + 1 - ai <= s.length) {
                    let cs =s
                    let __ss = cs.replaceAt(m + 1 - ai - 1, "A");
                    //console.log("__s = " + __ss)
                    if (compare(__ss, s) < 0) {
                        s = __ss;
                        //console.log(s)
                        continue;
                    } else {
                        return s;
                    }
                // }
            }
         }  else {
            let _s = c_s.replaceAt(m + 1 - ai - 1, "A");
            //console.log("_s = " + _s + ", s = " + s + ", " + compare(_s, s))
            if (compare(_s, s) < 0) {
                s = _s;
                //console.log(s)
                continue;
            }  if (compare(_s, s) >= 0) {
                // //console.log((m + 1 - ai) + " " + s.length)
                // if (m + 1 - ai <= s.length) {
                    let cs =s
                    let __ss = cs.replaceAt(ai - 1, "A");
                    //console.log("__s = " + __ss)
                    if (compare(__ss, s) < 0) {
                        s = __ss;
                        //console.log(s)
                        continue;
                    } else {
                        return s;
                    }
                // }
            }
    }
}

   return s;
}

function compare(s1, s2) {
    return s1.localeCompare(s2)
}

String.prototype.replaceAt = function(index, replacement) {
    return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}

function main() {
    // const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const test_cases = parseInt(readLine().trim(), 10);

    for (let gItr = 0; gItr < test_cases; gItr++) {
        const n_m = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        const n = n_m[0];
        const m = n_m[1];

        if ((n >= 1 && n <= 50) && (m >= 1 && m <= 50)) {
            const val = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
            let output = calc(val, m);
            console.log(output)
        }
        // ws.write(result + '\n');
    }

    // ws.end();
}

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

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
function abs(x) {
  return x < 0 ? x*(-1) : x;
}
// console.log(input)
var T = readline();
// var inf = 1000000000000;
for(var tc=1;tc<=T;tc++) {
  var [n, m] = readline().split(' ').map((x) => parseInt(x));
  var a = readline().split(' ').map((x) => parseInt(x));
  a.sort();
  // console.log(a)
  var cnt = []
  for(var i=0;i<n;i++) {
    var j=i;
    while(j<n && a[i]==a[j]) j++;
    j--;
    // console.log("j", i,j)
    if(j > i) {
      cnt[(m+1)-a[i]] = 1;
      cnt[a[i]] = 1;
    } else {
      // var m = min(a[i], (m+1)-a[i]);
      // console.log(m)
      cnt[min(a[i], (m+1)-a[i])] = 1;
    }
    i = j
  }
  // console.log(cnt)
  var ans = "";
  for(var i=1;i<=m;i++) {
    if(cnt[i]==1) ans += "A";
    else ans += "B"
  }
  console.log(ans)
}


This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.

For each query print two integers:   the maximum number of candies in a gift you can compose, according to the constraints in the statement;  the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.

NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.

let c = '';
process.stdin.on('data', i => c += i);

const top = 0;
const parent = i => ((i + 1) >>> 1) - 1;
const left = i => (i << 1) + 1;
const right = i => (i + 1) << 1;

class PriorityQueue {
  constructor(comparator = (a, b) => a > b) {
    this._heap = [];
    this._comparator = comparator;
  }
  size() {
    return this._heap.length;
  }
  isEmpty() {
    return this.size() == 0;
  }
  peek() {
    return this._heap[top];
  }
  push(...values) {
    values.forEach(value => {
      this._heap.push(value);
      this._siftUp();
    });
    return this.size();
  }
  pop() {
    const poppedValue = this.peek();
    const bottom = this.size() - 1;
    if (bottom > top) {
      this._swap(top, bottom);
    }
    this._heap.pop();
    this._siftDown();
    return poppedValue;
  }
  replace(value) {
    const replacedValue = this.peek();
    this._heap[top] = value;
    this._siftDown();
    return replacedValue;
  }
  _greater(i, j) {
    return this._comparator(this._heap[i], this._heap[j]);
  }
  _swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _siftUp() {
    let node = this.size() - 1;
    while (node > top && this._greater(node, parent(node))) {
      this._swap(node, parent(node));
      node = parent(node);
    }
  }
  _siftDown() {
    let node = top;
    while (
      (left(node) < this.size() && this._greater(left(node), node)) ||
      (right(node) < this.size() && this._greater(right(node), node))
    ) {
      let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
      this._swap(node, maxChild);
      node = maxChild;
    }
  }
}

process.stdin.on('end', async () => {
  const {EOL} = require('os');
  const lines = c.split(EOL);
  let pc = 1;
  let out = '';
  while (pc < lines.length && lines[pc].length) {
    let n = parseInt(lines[pc++]);
    let arr = Array.from({length: n}, () => ({a: 0, f: 0})), cnt = Array.from({length: n+1}, () => ({a: 0, f: 0}));
    for (let i = 0; i < n; i++)
      [arr[i].a, arr[i].f] = lines[pc++].split(' ').map((x) => parseInt(x));
    arr.forEach(e => (++cnt[e.a].a, e.f ? ++cnt[e.a].f: 0));
    cnt.sort((x, y) => y.a - x.a);
    let ans = 0, cur = n, ok = 0;
    let pq = new PriorityQueue((x, y) => x.f > y.f);
    cnt.forEach(e => {
      for (; e.a < cur; cur--) {
        if (pq.size()) {
          ans += Math.min(cur, pq.peek().a);
          ok += Math.min(cur, pq.peek().f);
          pq.pop();
        }
      }
      pq.push(e);
    });
    out += ans.toString() + ' ' + ok.toString() + EOL;
  }
  console.log(out);
});


You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x < m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.

The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) — the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a < m \le 10^{10}$$$).

Print $$$T$$$ integers — one per test case. For each test case print the number of appropriate $$$x$$$-s.

NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.

n=+readline()
function gcd(a,b){
    if (!b)
        return a;
    return gcd(b,a%b)
}
for (t=0;t<n;t++){
    s=readline().split(' ')
    a=+s[0]
    b=+s[1]
    g=gcd(a,b)
    b/=g
    ans=1
    for (j=2;j*j<=b;j++){
        p=1
        while(b%j==0){
           b/=j
           p*=j
        }
        if (p>1)
            ans*=(p-p/j)
    }
    if (b>1)
    ans=ans*(b-1)
    print(ans)
}

process.stdin.resume()
process.stdin.setEncoding('utf-8')

let input = ''
let ptr = 0
process.stdin.on('data', (i) => {
  input += i
})

process.stdin.on('end', () => {
  input = input.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  main()
})

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if(inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

function readInt() {
  return parseInt(input[ptr++])
}

function readString() {
  return input[ptr++]
}

function readStrings() {
  return input[ptr++].split(' ')
}

function readInts() {
  return input[ptr++].split(' ').map(a => parseInt(a))
}

function main() {
  let t = readInt()
  while(t--) {
    let [a, m] = readInts()
    let g = gcd(a, m)
    m = m / g
    let prime = primeFactors(m)
    let c = 1
    let  k = 0, p = prime[0]
    for(let i = 0, l = prime.length; i < l; i++) {
      if(prime[i] === p) k++
      else {
        c *= (p ** k) * (p - 1) / p
        p = prime[i]
        k = 1
      }
    }
    c *= (p ** k) * (p - 1) / p
    // wr(c, m, g, prime)
    wr(c)
  }
}

function primeFactors(num) {
  const factors = []
  while(num % 2 === 0) {
    factors.push(2)
    num /= 2
  }
  for(let i = 3, sqrt = Math.floor(Math.sqrt(num)); i <= sqrt; i += 2) {
    while(num % i === 0) {
      factors.push(i)
      num /= i
    }
  }
  if(num > 2) factors.push(num)
  return factors
}

function gcd(x, y) {
  if(y === 0) return x
  else return gcd(y, x % y)
}