var n = +readline();
var tab = [];
var ans = 'Yes';
for (var i=0; i<n; i++){
	tab[i] = readline().split(' ').map(Number);
}
for (var i=0; i<n; i++){
	next: for (var j=0; j<n; j++){
		if (tab[i][j] != 1) {
			for (var k=0; k<n; k++){
				for (var l=0; l<n; l++){
					if (i != l && j != k){
						if (tab[i][j] == tab[i][k] + tab[l][j]) {
							continue next;
						}
					}
				}
			}
		}
		if (tab[i][j] != 1) {
			ans = 'No';
			break;
		}
	}
}
print(ans);

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
const lab = [];

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  lab.push(d.split(' ').map(Number));

  c++;
});

rl.on('close', () => {
  let ans = 'YES';

  isDone:
  for (let i = 0; i < lab.length; i++) {
    for (let j = 0; j < lab[i].length; j++) {
      if (lab[i][j] !== 1) {

          ans = 'NO';
          for (let t = 0; t < lab.length; t++) {
            for (let s = 0; s < lab[t].length; s++) {
              let sum = lab[t][j] + lab[i][s];

              if (t !== i && s !== j) {
                if (sum === lab[i][j]) {
                  ans = 'YES';
                }
              }
            }
          }

          if (ans === 'NO') {
            break isDone;
          }

      }
    }
  }

  console.log(ans);
});

var n = parseInt(readline()),
	arr = [],
	flag = true;
function mapper(x) {return parseInt(x);}
for (var i = 0; i < n; i++) {
	var temp = [];
	var inp = readline().split(" ").map(mapper);
	arr.push(inp);
}

function checker(i, j) {
	for (var c = 0; c < n; c++) {
		if (c == j)
			continue;
		for (var r = 0; r < n; r++) {
			if (r == i)
				continue;
			if (arr[i][c] + arr[r][j] == arr[i][j])
				return true;
		}
	}
	return false;
}

for (var i = 0; i < n; i++) {
	for (var j = 0; j < n && flag; j++) {
		if (arr[i][j] != 1 && !checker(i, j)) {
			flag = false;
			break;
		}
	}
}

print((flag ? "Yes": "No"));

var n = Number(readline());
var arr = [];
for(var i=0; i<n; i++) {
    var line = readline().split(" ").map(Number);
    arr.push(line);
}

var res = 'Yes';
cyclePar:
for(var i=0; i<n; i++) {
    for(var j=0; j<n; j++) {
        var cur = arr[i][j];
        if (cur!=1) {
            var found = false;
            cycle:
            for(var k=0; k<n; k++) {
                for(var z=0; z<n; z++) {
                    if (i!=k && j!=z) {
                        if (arr[k][j] + arr[i][z] == cur){
                            found = true;
                            break cycle;
                        }
                    }
                }
            }
            if (!found) {
                res = 'No';
                break cyclePar;
            }
        }
    }
}

print(res);

var n = Number(readline());
var arr = [];
for(var i=0; i<n; i++) {
    var line = readline().split(" ").map(Number);
    arr.push(line);
}

var res = 'Yes';
for(var i=0; i<n; i++) {
    for(var j=0; j<n; j++) {
        var cur = arr[i][j];
        if (cur!=1) {
            var found = false;
            for(var k=0; k<n; k++) {
                for(var z=0; z<n; z++) {
                    if (i!=k && j!=z) {
                        if (arr[k][j] + arr[i][z] == cur){
                            found = true;
                            break;
                        }
                    }
                }

            }
        if (!found) {
            res = 'No';
            break;
        }
        }
    }
}

print(res);


'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
    let clone = [...a]
    clone.sort((a1,a2)=>a1 - a2)
    let left = -1
    let right = -1
    for (let i = 0; i < n; i++) {
        if (a[i] !== clone[i]) {
            left = i
            break;
        }
    }

    for (let i = n - 1; i >= 0; i--) {
        if (a[i] !== clone[i]) {
            right = i
            break;
        }
    }

    if (left === -1 && right === -1) {
        console.log('yes')
        console.log(1 + ' ' + 1)
        return
    }

    for (let i = 0; i < Math.floor(right - left); i++) {
        if (a[left + i] !== clone[right - i]) {
            console.log('no')
            return
        }
    }

    console.log('yes')
    console.log((left + 1) + ' ' + (right + 1))
}

		var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0,maxi=1,mini=1;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;mini=i+1;}
			if(i===s.length-2){min=parseInt(s[i+1]);maxi=i+2;}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])){min=parseInt(s[i]);maxi=i+1;}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+mini+" "+maxi);}
	else if(f===1&&max!==0&&min!==0){if(mini>1){if(min>parseInt(s[mini-2])){print("yes\n"+mini+" "+maxi);}else{print("no");}}else{print("yes\n"+mini+" "+maxi);}}
	else{print("no");}

;(function() {

	print((function (n, x) {
		for (var i = 0; i < n; i++) if (x[i] !== i) break;
		for (var j = n - 1; j >= 0; j--) if (x[j] !== j) break;
		if (i === n && j === -1) return 'yes\n1 1';
		for (var k = i; k < j; k++) if (x[k] - x[k+1] !== 1) return 'no';
		if (x[j] === i && x[i] === j) return 'yes\n' + (i + 1) + ' ' + (j + 1);
		return 'no';
	})(+readline(), readline().split(' ').map(function (e, i) { return {n: parseInt(e), i: i}; })
		.sort(function (a, b) { return a.n - b.n; }).map(function (e) { return e.i; })));

}).call(this);

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var n = parseInt(readline()),
		a = tokenizeIntegers(readline());
	if (n == 1) {
		return 'yes\n1 1';
	}
	for (var i = 1; i < n; ++i) {
		if (a[i] < a[i-1]) {
			for (var j = i; a[j] < a[j-1] && j < n; ++j) {
			}
			var start = i-1, finish = j-1;
			for (var p = Math.floor((start+finish)/2); p >= start; --p) {
				var q = finish - p + start,
					t = a[p];
				a[p] = a[q];
				a[q] = t;
			}
			for (var k = 1; k < n; ++k) {
				if (a[k] < a[k-1]) {
					return 'no';
				}
			}
			return 'yes\n'+(start+1)+' '+(finish+1);
		}
	}
	return 'yes\n1 1';
}

print(main());

'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 printResult(x) {
    process.stdout.write(x);
}

function main() {
    const n = parseInt(readLine(), 10);
    const arr = readLine().split(' ').map(Number);

    const result = sortTheArray(n, arr);
    printResult(result);
}

function reverseByIndex(a, left, right) {
    const temp = [];
    for (let i = left, j = right; i <= right; i++, j--) temp[i] = a[j];
    for (let i = left; i <= right; i++) a[i] = temp[i];
}

const sortTheArray = (n, a) => {
    let left = 0;
    let right = 0;
    let flag = false;
    for (let i = 0; i < n - 1; i++) {
        if (a[i] > a[i + 1]) {
            left = i;
            break;
        }
    }

    for (let i = n - 1; i >= left; i--) {
        if (a[i] < a[i - 1]) {
            right = i;
            break;
        }
    }

    reverseByIndex(a, left, right);

    for (let i = 0; i < n - 1; i++) {
        if (a[i] > a[i + 1]) {
            return `no`;
        }
    }

    return `yes\n${left + 1} ${right + 1}`;
};

'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 printResult(x) {
    process.stdout.write(x);
}

function main() {
    const n = parseInt(readLine(), 10);
    const arr = readLine().split(' ').map(Number);

    const result = sortTheArray(n, arr);
    printResult(result);
}

function reverseByIndex(a, left, right) {
    const temp = new Array(a.length);
    for (let i = left, j = right; i <= right; i++, j--) temp[i] = a[j];
    for (let i = left; i <= right; i++) a[i] = temp[i];
}

const sortTheArray = (n, a) => {
    let left = 0;
    let right = 0;
    for (let i = 0; i < n - 1; i++) {
        if (a[i] > a[i + 1]) {
            left = i;
            break;
        }
    }

    for (let i = n - 1; i >= left; i--) {
        if (a[i] < a[i - 1]) {
            right = i;
            break;
        }
    }

    reverseByIndex(a, left, right);

    for (let i = 0; i < n - 1; i++) {
        if (a[i] > a[i + 1]) {
            return `no`;
        }
    }

    return `yes\n${left + 1} ${right + 1}`;
};

let isSorted = arr => arr.every((v, i, a) => !i || a[i - 1] <= v)
const mainFunction = (lines) => {
    let arr = lines[1].split(" ").map(Number),
        sortedArr = lines[1].split(" ").map(Number),
        mas = []
    if (isSorted(arr)) {
        console.log(`yes\n1 1`)
        return 0
    }
    sortedArr.sort((a, b) => a - b)
    for (let i in arr) {
        if (arr[i] != sortedArr[i])
            mas.push(+i + 1)
    }
    mas.sort((a, b) => a - b)
    arr = arr.slice(mas[0] - 1, mas[mas.length - 1])
    sortedArr = sortedArr.slice(mas[0] - 1, mas[mas.length - 1]).reverse()
    if (arr.join(" ") == sortedArr.join(" ")) console.log(`yes\n${mas[0]} ${mas[mas.length - 1]}`)
    else console.log(`no`)
}

const { EOL } = require('os')
if (__filename.substr(9, 4) != "azer") {
    let yourInputData = ''
    process.stdin.on('data', i => yourInputData += i)
    process.stdin.on('end', () => mainFunction(yourInputData.split(EOL)))
} else {
    const fileSystem = require("fs");
    mainFunction(fileSystem.readFileSync('input.txt', 'utf8').split(EOL))
}

let isSorted = arr => arr.every((v, i, a) => !i || a[i - 1] <= v)
const mainFunction = (lines) => {
    let arr = lines[1].split(" ").map(Number),
        sortedArr = lines[1].split(" ").map(Number),
        n = arr.length,
        cnt = 0,
        mas = []
    if (isSorted(arr)) {
        console.log(`yes\n1 1`)
        return 0
    }
    sortedArr.sort((a, b) => a - b)
    for (let i in arr) {
        if (arr[i] != sortedArr[i]) {
            cnt++;
            mas.push(+i + 1)
        }
    }
    mas.sort((a, b) => a - b)
    arr = arr.slice(mas[0] - 1, mas[mas.length - 1])
    sortedArr = sortedArr.slice(mas[0] - 1, mas[mas.length - 1]).reverse()
    if (arr.join(" ") == sortedArr.join(" ")) console.log(`yes\n${mas[0]} ${mas[mas.length - 1]}`)
    else console.log(`no`)
}

const { EOL } = require('os')
if (__filename.substr(9, 4) != "azer") {
    let yourInputData = ''
    process.stdin.on('data', i => yourInputData += i)
    process.stdin.on('end', () => mainFunction(yourInputData.split(EOL)))
} else {
    const fileSystem = require("fs");
    mainFunction(fileSystem.readFileSync('input.txt', 'utf8').split(EOL))
}

function processData(input) {
    //Enter your code here
     input = input.split('\n')
    var n = Number(input[0])
    var arr = input[1].split(' ').map(a=>parseInt(a))
    var tempArr = [...arr]
    // console.log(tempArr)
    tempArr.sort((a,b)=>(a-b))
    // console.log(tempArr,arr)
    var ans
    var res = checkReverse(arr,tempArr,n)    //finding first mismatch

    if(res){
        console.log("yes")
    }
    else{
        console.log("no")
    }
    function checkReverse(arr,tempArr,n){
        //to find the first index mismatch
        for(var first = 0;first < n;first++){
            if(tempArr[first] != arr[first]){
                break
            }
        }
        //to find the last mismatch
        for(var last = n-1;last >=0 ;last--){
              if(tempArr[last] != arr[last]){
                break
            }
        }
    if(first >= last){
                ans = [first-n+1,first-n+1]
            return true
        }
        //to check decreasing array
        ans = [first+1,last+1]
        while(first != last){
            first++
            if(arr[first-1] < arr[first]){
                return false
            }

        }

        return true
    }
    if(res){
            console.log(ans.join(' '))

    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
  let clone = [...a]
  clone.sort((a1, a2) => a1 - a2)
  let differences = []
  for (let i = 0; i < n; i++) {
    if (a[i] !== clone[i]) {
      differences.push(i + 1)
    }
    if (differences.length > 2) {
      console.log('no')
      return
    }
  }

  if (differences.length === 2) {
    console.log('yes')
    console.log(differences[0] + ' ' + differences[1])
  } else {
    console.log('no')
  }
}

'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
  let clone = [...a]
  clone.sort((a1, a2) => a1 - a2)
  let left = -1
  let right = -1
  for (let i = 0; i < n; i++) {
    if (a[i] !== clone[i]) {
      if (left === -1) {
        left = i
        right = i
      } else {
        right = i
      }
    }
  }

  if (left === -1 && right === -1) {
    console.log('yes')
    console.log(1 + ' ' + 1)
    return
  }

  for (let i = 0; i <= Math.floor((right - left) / 2); i++) {
    if (a[left + i] !== clone[right - i]) {
      console.log('no')
      return
    }
  }

  console.log('yes')
  console.log((left + 1) + ' ' + (right + 1))
}

'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
    let clone = [...a]
    clone.sort((a1,a2)=>a1 - a2)
    let left = -1
    let right = -1
    for (let i = 0; i < n; i++) {
        if (a[i] !== clone[i]) {
            left = i
            break;
        }
    }

    for (let i = n - 1; i >= 0; i--) {
        if (a[i] !== clone[i]) {
            right = i
            break;
        }
    }

    if (left === -1 && right === -1) {
        console.log('yes')
        console.log(1 + ' ' + 1)
        return
    }

    for (let i = 0; i <= Math.floor((right - left) / 2); i++) {
        if (a[left + i] !== clone[right - i]) {
            console.log('no')
            return
        }
    }

    console.log('yes')
    console.log((left + 1) + ' ' + (right + 1))
}

'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
    let clone = [...a]
    clone.sort((a1,a2)=>a1 - a2)
    let left = -1
    let right = -1
    for (let i = 0; i < n; i++) {
        if (a[i] !== clone[i]) {
            left = i
            break;
        }
    }

    for (let i = n - 1; i >= 0; i--) {
        if (a[i] !== clone[i]) {
            right = i
            break;
        }
    }

    if (left === -1 && right === -1) {
        console.log('yes')
        console.log(1 + ' ' + 1)
        return
    }

    for (let i = 0; i < Math.floor((right - left) / 2); i++) {
        if (a[left + i] !== clone[right - i]) {
            console.log('no')
            return
        }
    }

    console.log('yes')
    console.log((left + 1) + ' ' + (right + 1))
}

'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() {
  const n = Number(readline())
  const a = readline().split(' ').map(Number)

  helper(n, a)
}

function helper(n, a) {
  let clone = [...a]
  clone.sort((a1, a2) => a1 - a2)
  let differences = []
  for (let i = 0; i < n; i++) {
    if (a[i] !== clone[i]) {
      differences.push(i + 1)
    }
    if (differences.length > 2) {
      console.log('no')
      return
    }
  }

  if (differences.length === 2) {
    console.log('yes')
    console.log(differences[0] + ' ' + differences[1])
  } else if (differences.length === 0) {
    console.log('yes')
    console.log(1 + ' ' + 1)
  } else {
    console.log('no')
  }
}

let isSorted = arr => arr.every((v, i, a) => !i || a[i - 1] <= v)
const mainFunction = (lines) => {
    let arr = lines[1].split(" ").map(Number),
        sortedArr = lines[1].split(" ").map(Number),
        n = arr.length,
        cnt = 0,
        mas = []
    if (isSorted(arr)) {
        console.log(`yes\n1 1`)
        return 0
    }
    sortedArr.sort((a, b) => a - b)
    for (let i in arr) {
        if (arr[i] != sortedArr[i]) {
            cnt++;
            mas.push(+i + 1)
        }
    }
    mas.sort((a, b) => a - b)
    console.log(mas)
    arr = arr.slice(mas[0] - 1, mas[mas.length - 1])
    sortedArr = sortedArr.slice(mas[0] - 1, mas[mas.length - 1]).reverse()
    console.log(arr, sortedArr)
    if (arr.join(" ") == sortedArr.join(" ")) console.log(`yes\n${mas[0]} ${mas[mas.length - 1]}`)
    else console.log(`no`)
}

const { EOL } = require('os')
if (__filename.substr(9, 4) != "azer") {
    let yourInputData = ''
    process.stdin.on('data', i => yourInputData += i)
    process.stdin.on('end', () => mainFunction(yourInputData.split(EOL)))
} else {
    const fileSystem = require("fs");
    mainFunction(fileSystem.readFileSync('input.txt', 'utf8').split(EOL))
}

	var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0,maxi=1,mini=1;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;mini=i+1;}
			if(i===s.length-2){if(mini-2>=0){if(parseInt(s[i+1])>parseInt(s[mini-2])){min=parseInt(s[i+1]);maxi=i+2;}}else if(mini-2===-1){min=parseInt(s[i+1]);maxi=i+2;}}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])&&parseInt(s[i])>parseInt(s[mini-2])){min=parseInt(s[i]);maxi=i+1;}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+mini+" "+maxi);}
	else if(f===1&&max!==0&&min!==0){print("yes\n"+mini+" "+maxi);}
	else{print("no");}

 	var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;}if(i===s.length-2){min=parseInt(s[i+1]);}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])){min=parseInt(s[i]);}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+s[0]+" "+s[0]);}
	else if(f===1&&max!==0&&min!==0){print("yes\n"+min+" "+max);}
	else{print("no");}

	var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0,maxi=1,mini=1;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;mini=i+1;}
			if(i===s.length-2){if(mini-2>=0){if(parseInt(s[i+1])>parseInt(s[mini-2])){min=parseInt(s[i+1]);maxi=i+2;}}}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])&&parseInt(s[i])>parseInt(s[mini-2])){min=parseInt(s[i]);maxi=i+1;}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+mini+" "+maxi);}
	else if(f===1&&max!==0&&min!==0){print("yes\n"+mini+" "+maxi);}
	else{print("no");}

var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0,maxi=1,mini=1;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;mini=i+1;}
			if(i===s.length-2){min=parseInt(s[i+1]);maxi=i+2;}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])){min=parseInt(s[i]);maxi=i+1;}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+mini+" "+maxi);}
	else if(f===1&&max!==0&&min!==0){if(mini>1){if(min>parseInt(s[mini-2])){print("yes\n"+mini+" "+maxi);}}else{print("yes\n"+mini+" "+maxi);}}
	else{print("no");}

	var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0,maxi=1,mini=1;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;mini=i+1;}
			if(i===s.length-2){min=parseInt(s[i+1]);maxi=i+2;}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])){min=parseInt(s[i]);maxi=i+1;}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print("yes\n"+mini+" "+maxi);}
	else if(f===1&&max!==0&&min!==0){print("yes\n"+mini+" "+maxi);}
	else{print("no");}

	var l=readline();
	var s=readline().split(" ");
	var f=0,max=0,min=0;

		for(var i=0;i<s.length-1;i++){
			if(parseInt(s[i])>parseInt(s[i+1])){if(parseInt(s[i])>max){max=parseInt(s[i]);f++;}if(i===s.length-2){min=parseInt(s[i+1]);}}
			else{if(f===1&&min===0&&max<parseInt(s[i+1])){min=parseInt(s[i]);}else if(f>1||parseInt(s[i+1])<max){break;}}
		}
	if(f===0){print(s[0]+" "+s[0]);}
	else if(f===1&&max!==0&&min!==0){print("yes\n"+min+" "+max);}
	else{print("no");}

;(function() {

	print((function (n, x) {
		for (var i = 0; i < n; i++) if (x[i] !== i) break;
		for (var j = n - 1; j >= 0; j--) if (x[j] !== j) break;
		if (i === n - 1 && j === 1) return 'yes\n1 1';
		for (var k = i; k < j; k++) if (x[k] - x[k+1] !== 1) return 'no';
		return (x[j] === i && x[i] === j) ? 'yes\n' + (i + 1) + ' ' + (j + 1) : 'no';
	})(+readline(), readline().split(' ').map(function (e, i) { return {n: parseInt(e), i: i}; })
		.sort(function (a, b) { return a.n - b.n; }).map(function (e) { return e.i; })));

}).call(this);

;(function() {

	print((function (n, x) {
		for (var i = 0; i < n; i++) if (x[i] !== i) break;
		for (var j = n - 1; j >= 0; j--) if (x[j] !== j) break;
		if (i === n && j === -1) return 'yes\n1 1';
		for (var k = j; k < i; k++) if (x[j] - x[j-1] !== 1) return 'no';
		if (x[j] === i && x[i] === j) return 'yes\n' + (i + 1) + ' ' + (j + 1);
		return 'no';
	})(+readline(), readline().split(' ').map(function (e, i) { return {n: parseInt(e), i: i}; })
		.sort(function (a, b) { return a.n - b.n; }).map(function (e) { return e.i; })));

}).call(this);

;(function() {

	var n = +readline(),
		x = readline().split(' ')
			.map(function (e, i) { return {n: parseInt(e), i: i}; })
			.sort(function (a, b) { return a.n - b.n; })
			.map(function (e) { return e.i; });

	print((function () {
		for (var i = 0; i < n; i++) if (x[i] !== i) break;
		for (var j = n - 1; j >= 0; j--) if (x[j] !== j) break;
		if (i === (n - 2) && j === 1) return 'yes\n1 1';
		for (var k = j; k < i; k++) if (x[j] - x[j-1] !== 1) return 'no';
		if (x[j] === i && x[i] === j) return 'yes\n' + (i + 1) + ' ' + (j + 1);
		return 'no';
	})());

}).call(this);

;(function() {

	var n = +readline(),
		a = readline().split(' ').map(Number).reduce(function (p, e, i, a) {
			if (p.push(i) !== a.length) return p;
			p = p.sort(function (_, $) { return a[_] - a[$]; });

			var max = [], min = [];

			if (p[0] > p[1]) max.push(1);

			for (var i = 1; i < n - 1; i++)
				if (p[i] > p[i+1] && p[i] > p[i-1]) max.push(i + 1);
				else if (p[i] < p[i+1] && p[i] < p[i-1]) min.push(i + 1);

			if (p[n - 1]  < p[n - 2]) min.push(n);

			return [max, min];
		}, []);

		print((function () {
			if (!a[0].length && !a[1].length) return 'yes\n1 1';
			if (a[0].length > 1) return 'no';
			return 'yes\n' + a[0][0] + ' ' + a[1][0];
		})());

}).call(this);


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++];
}

var dp = {};

function fibonacci(n) {
	if (n === 0) return 0;
	if (n === 1) return 1;

	if (dp[n] !== undefined) return dp[n];
	let result = fibonacci(n - 1) + fibonacci(n - 2);
	return (dp[n] = result);
}

function main() {
	let n = readLine() >> 0;
	let cnt = {};
	let c = readLine()
		.split(' ')
		.map(x => {
			x = x >> 0;
			cnt[x] = (cnt[x] || 0) + 1;
			return x;
		});
	if (cnt[0] === undefined) {
		console.log(-1);
	} else {
		if (cnt[5] === undefined || cnt[5] < 9) console.log(0);
		else {
			let q = Math.floor(cnt[5] / 9);
			console.log('5'.repeat(q * 9) + '0'.repeat(cnt[0]));
		}
	}
}

var n=+readline();
var a=readline().split(' ');

var five=0;
var zero=0;
a.forEach(function(v){
	if(v==='5'){
		five++;
	}else{
		zero++;
	}
});

five=Math.floor(five/9);
if(zero==0){
	print(-1);
}else if(five==0){
	print(0)
}else{
	var ans=[];
	//var k=Math.min(five,zero);
	for(var i=0;i<five;i++){
		ans.push('555555555');
	}
	while(zero--){
		ans.push('0')
	}
	print(ans.join(''));
}

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

	var c = 0;
	var d = 0;

	readline().split(' ').map(function (e) {
		if (e == 0) c++;
		else d++;
	});

	if (!c) {
		print(-1);
		return;
	}

	var str = '';
	for (var i = 0; i < Math.floor(d / 9); i++)
		for (var j = 0; j < 9; j++)
			str += '5';

	if (!str) {
		print(0);
		return;
	}

	for (var i = 0; i < c; i++)
		str += '0';

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

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var numDigits = parseInt(readline()),
		digits = tokenizeIntegers(readline()),
		counts = { 0: 0, 5: 0 };
	for (var i = 0; i < numDigits; ++i) {
		counts[digits[i]] += 1;
	}
	if (counts[0] == 0) {
		print(-1);
		return;
	}
	if (counts[5] < 9) {
		print(0);
		return;
	}
	digits.sort(function (a, b) {
		return b-a;
	});
	for (var i = counts[5]%9; i != 0; --i) {
		digits.shift();
	}
	print(digits.join(''));
}

main();

var n = parseInt( readline(), 10 );
var a = readline().split( " " ).map( ai => parseInt( ai, 10 ) );
// Prime factors of 90 are 2, 3^2 and 5.
// In our case number is divisible by 2 if at least one zero at the end.
// Number is divisible by 9 if its digits sum divisible by 9. In our case that is repeated 555555555 only.
// Number is divisible by 5 if last digit is zero.
var fives = a.filter( ai => ai === 5);
var zeros = a.filter( ai => ai === 0);
var answer = -1;
if ( zeros.length > 0 ) {
  if ( fives.length >= 9 ) {
    answer = [ ...fives.slice( 0, fives.length - fives.length % 9), ...zeros ].join( "" );
  } else {
    answer = 0;
  }
}

print( answer );

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++];
}
function main() {
  let len = +readLine();
  const arr = readLine()
    .split(" ")
    .map((n) => parseInt(n));
  arr.sort((a, b) => b - a);

  let [count0, count5] = [0, 0];
  for (let i = 0; i < len; i++) {
    if (arr[i] === 0) count0++;
    if (arr[i] === 5) count5++;
  }
  count5 = count5 - (count5 % 9);
  if (count5 > 0 && count0 > 0) {
    console.log([...Array(count5).fill(5), ...Array(count0).fill(0)].join(""));
  } else if (count0 > 0) {
    console.log(0);
  } else {
    console.log(-1);
  }
}

var n=+readline();
var a=readline().split(' ');

var five=0;
var zero=0;
a.forEach(function(v){
	if(v==='5'){
		five++;
	}else{
		zero++;
	}
});

five=Math.floor(five/9);
if(five==0||zero==0){
	print(0);
}else{
	var ans=[];
	var k=Math.min(five,zero);
	for(var i=0;i<k;i++){
		ans.push('5555555550');
	}
	print(ans.join(''));
}

var n=+readline();
var a=readline().split(' ');

var five=0;
var zero=0;
a.forEach(function(v){
	if(v==='5'){
		five++;
	}else{
		zero++;
	}
});

five=Math.floor(five/9);
if(zero==0){
	print(-1);
}else if(five==0){
	print(0)
}else{
	var ans=[];
	var k=Math.min(five,zero);
	for(var i=0;i<k;i++){
		ans.push('5555555550');
	}
	print(ans.join(''));
}

var n=+readline();
var a=readline().split(' ');

var five=0;
var zero=0;
a.forEach(function(v){
	if(v==='5'){
		five++;
	}else{
		zero++;
	}
});

five=Math.floor(five/9);
if(zero==0){
	print(-1);
}else if(five==0){
	print(0)
}else{
	var ans=[];
	var k=Math.min(five,zero);
	for(var i=0;i<k;i++){
		ans.push('555555555');
	}
	while(zero--){
		ans.push('0')
	}
	print(ans.join(''));
}

var n=+readline();
var a=readline().split(' ');

var five=0;
var zero=0;
a.forEach(function(v){
	if(v==='5'){
		five++;
	}else{
		zero++;
	}
});

five=Math.floor(five/9);
print(Math.min(five,zero));

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

	var c = 0;
	var d = 0;

	readline().split(' ').map(function (e) {
		if (e == 0) c++;
		else d++;
	});

	if (!c) {
		print(-1);
	}

	var str = '';
	for (var i = 0; i < Math.floor(d / 3); i++)
		for (var j = 0; j < 3; j++)
			str += '5';

	if (!str) {
		print(0);
		return;
	}

	for (var i = 0; i < c; i++)
		str += '0';

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

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

	var c = 0;
	var d = 0;

	readline().split(' ').map(function (e) {
		if (e == 0) c++;
		else d++;
	});

	if (!c) {
		print(-1);
		return;
	}

	var str = '';
	for (var i = 0; i < Math.floor(d / 3); i++)
		for (var j = 0; j < 3; j++)
			str += '5';

	if (!str) {
		print(0);
		return;
	}

	for (var i = 0; i < c; i++)
		str += '0';

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

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var n = parseInt(readline()), apples = tokenizeIntegers(readline()),
		counts = { 100: 0, 200: 0 };
	for (var i = 0; i < n; ++i) {
		counts[apples[i]] += 1;
	}
	if (counts[200] % 2 == 0) {
		print(counts[100] % 2 == 0 ? "YES" : "NO");
	} else {
		if (counts[100] < 2) {
			print("NO");
		} else {
			print((counts[100] - 2) % 2 == 0 ? "YES": "NO");
		}
	}
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var numDigits = parseInt(readline()),
		digits = tokenizeIntegers(readline()),
		counts = { 0: 0, 5: 0 };
	for (var i = 0; i < numDigits; ++i) {
		counts[digits[i]] += 1;
	}
	if (counts[0] == 0) {
		print(-1);
		return;
	}
	if (counts[5] < 9) {
		print(0);
	}
	digits.sort(function (a, b) {
		return b-a;
	});
	for (var i = counts[5]%9; i != 0; --i) {
		digits.shift();
	}
	print(digits.join(''));
}

main();

var n = parseInt( readline(), 10 );
var a = readline().split( " " ).map( ai => parseInt( ai, 10 ) );
// Prime factors of 90 are 2, 3 and 5.
// In our case number is divisible by 2 if at least one zero at the end.
// Number is divisible by 3 if its digits sum divisible by 3. In our case that is repeated 555 only.
// Number is divisible by 5 if last digit is zero.
var fives = a.filter( ai => ai === 5);
var zeros = a.filter( ai => ai === 0);
var answer = -1;
if ( zeros.length > 0 ) {
  if ( fives.length >= 3 ) {
    answer = [ ...fives.slice( 0, fives.length - fives.length % 3), ...zeros ].join( "" );
  } else {
    answer = 0;
  }
}

print( answer );

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++];
}

var dp = {};

function fibonacci(n) {
	if (n === 0) return 0;
	if (n === 1) return 1;

	if (dp[n] !== undefined) return dp[n];
	let result = fibonacci(n - 1) + fibonacci(n - 2);
	return (dp[n] = result);
}

function main() {
	let n = readLine() >> 0;
	let cnt = {};
	let c = readLine()
		.split(' ')
		.map(x => {
			x = x >> 0;
			cnt[x] = (cnt[x] || 0) + 1;
			return x;
		});
	if (cnt[0] === undefined) {
		console.log(-1);
	} else {
		if (n < 9) console.log(0);
		else {
			let q = Math.floor(cnt[5] / 9);
			console.log('5'.repeat(q * 9) + '0');
		}
	}
}

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++];
}

var dp = {};

function fibonacci(n) {
	if (n === 0) return 0;
	if (n === 1) return 1;

	if (dp[n] !== undefined) return dp[n];
	let result = fibonacci(n - 1) + fibonacci(n - 2);
	return (dp[n] = result);
}

function main() {
	let n = readLine() >> 0;
	let cnt = {};
	let c = readLine()
		.split(' ')
		.map(x => {
			x = x >> 0;
			cnt[x] = (cnt[x] || 0) + 1;
			return x;
		});
	if (cnt[0] === undefined) {
		console.log(-1);
	} else {
		if (n < 9) console.log(0);
		else {
			let q = Math.floor(cnt[5] / 9);
			console.log('5'.repeat(q * 9) + '0'.repeat(cnt[0]));
		}
	}
}


"use strict";

// readline().split(' ').map(value => +value);
var T = +readline();

while(T--) {
    var input = readline().split(' ').map(value => +value);
    var n = input[0];
    var s = input[1];
    var t = input[2];

    var d = (s+t) - n;
    var ds = s - d;
    var dt = t - d;

    write(Math.max(ds, dt) + 1, '\n');
}

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]
const {EOL} = require('os')

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            res(i.split(EOL).map(line => line.split(' ').map(str => parseInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join(EOL));
}

getInput(false).then(lines => {
    if(isNaN(lines[lines.length-1][0])) lines.pop();
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

(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 q = read.number();
    var res = '';

    for(var i = 0; i < q; i++) {
        var line = read.arrNumber(' ');
        if(line[0] === line[1] && line[0] === line[2]) {
            res += '1 \n';
        }
        else {
            var a = line[2] + line[1] - line[0];
            res += Math.min(line[0], Math.max(line[1], line[2]) - a + 1) + '\n';
        }
    }

    print(res);

}());

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

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const [n, s, t] = d.split(' ').map(Number);
  const min = Math.min(s, t);
  const ans = n - min + 1;

  console.log(ans);

  c++;
});

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            const {EOL} = require('os')
            res(i.split(EOL).map(line => line.split(' ').map(str => parseInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join('\r\n'));
}

getInput(true).then(lines => {
    if(isNaN(lines[lines.length-1][0])) lines.pop();
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            res(i.split('\r\n').map(line => line.split(' ').map(str => BigInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join('\r\n'));
}

getInput(true).then(lines => {
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            res(i.split('\r\n').map(line => line.split(' ').map(str => parseInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join('\r\n'));
}

getInput(false).then(lines => {
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]
const {EOL} = require('os')

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            res(i.split(EOL).map(line => line.split(' ').map(str => parseInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join(EOL));
}

getInput(true).then(lines => {
    if(isNaN(lines[lines.length-1][0])) lines.pop();
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

const testInput = [
    [3],
    [10, 5, 7],
    [10, 10, 10],
    [2, 1, 1]
]

function getInput(test = false) {
    return new Promise((res,rej) => {
        if (test) return res(testInput);
        let i = ''
        process.stdin.on('data', c => i += c)
        process.stdin.on('end', () => {
            res(i.split('\r\n').map(line => line.split(' ').map(str => parseInt(str))));
        })
    })
}

function output(lines) {
    console.log(lines.join('\r\n'));
}

getInput(true).then(lines => {
    if(isNaN(lines[lines.length-1][0])) lines.pop();
    lines.shift();
    const answer = lines.map(line => line[0] + 1 - Math.min(line[1],line[2]))
    output(answer);
})

(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 q = read.number();
    var res = '';

    for(var i = 0; i < q; i++) {
        var line = read.arrNumber(' ');
        if((line[0] === line[1] && line[2]  > 0) || (line[0] === line[2] && line[1]  > 0)) {
            res += '1 \n';
        }
        else {
            res += Math.min(line[0], Math.min(line[1], line[2]) + 1) + '\n';
        }
    }

    print(res);

}());

(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 q = read.number();
    var res = '';

    for(var i = 0; i < q; i++) {
        var line = read.arrNumber(' ');
        if(line[0] === line[1] || line[0] === line[2]) {
            res += '1 \n';
        }
        else {
            var a = line[2] + line[1] - line[0];
            res += Math.min(line[0], Math.max(line[1], line[2]) - a + 1) + '\n';
        }
    }

    print(res);

}());


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();
  });
  solve();
  // cmd: node solution.js < input.txt > output.txt
});
function readLine() {
  return inputString[currentLine++];
}

function solve() {
  let t = parseInt(readLine());
  while (t--) {
    let arr = readLine().split(" ");
    let a1 = parseInt(arr[0]), a2 = parseInt(arr[1]), a3 = parseInt(arr[2])
    if ((a1 + a3 - 2 * a2) % 3 == 0) {
      console.log(0)
    } else {
      console.log(1)
    }
  }
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
	var t = lines[0];
	for(i = 1; i <= t; i++){
	    var k = lines[i].split(' ').map(Number);
	    var a = k[0], b = k[1], c = k[2];
	    b *= 2;
	    a += c;
	    var d = Math.abs(a - b);
	    console.log(d % 3 === 0 ? 0 : 1);
	}
});

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

function solve(a, b, c) {
    if (!h(a, b, c)) return 0
    if (!h(b, c, a)) return 0
    if (!h(c, a, b)) return 0
    return 1
}
function h(a, b, c) {
    return !!(Math.abs(a + b - 2 * c) % 3)
}

//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] = BigInt(next());
		}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 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 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 sum = nextInt() + nextInt() + nextInt();
		if(sum % 3 == 0){
			myout(0);
		}else{
			myout(1);
		}
	}
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
    var t = lines[0];
	for(i = 1; i <= t; i++){
	    var k = lines[i].split(' ').map(Number);
	    var a = k[0], b = k[1], c = k[2];
	    b *= 2;
	    a += c;
	    console.log(Math.abs(a - b) % 3 === 0 ? 0 : 1);
	}
});


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;
    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()

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));
}


/**
 * 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 = m; x <= n && y >= 0; 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 += 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);
}


'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();
        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 = []
        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)
}


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)
}

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)
}