You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero):  swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.

If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j < n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.

NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.

function main() {
  const is = new Scanner();
  is.nextInt();
  const s = is.nextLine().split('');
  const t = is.nextLine().split('');
  const container = {};
  s.forEach((item) => {
    if (container[item] === undefined) {
      container[item] = 1;
    } else {
      container[item] += 1;
    }
  });
  t.forEach((item) => {
    if (container[item] !== undefined) {
      container[item] -= 1;
    }
  });
  let isPossible = true;
  for (const i in container) {
    if (container.hasOwnProperty(i)) {
      if (container[i] !== 0) {
        isPossible = false;
        break;
      }
    }
  }

  if (!isPossible) {
    console.log(-1);
  } else {
    let j;
    const swaps = [];
    t.forEach((item, i) => {
      if (item !== s[i]) {
        j = s.lastIndexOf(item);
        for (let k = j - 1; k !== i - 1; k -= 1) {
          [s[k], s[k + 1]] = [s[k + 1], s[k]];
          swaps.push(k + 1);
        }
      }
    });
    console.log(swaps.length);
    if (swaps.length) {
      console.log(swaps.join(' '));
    }
  }
}

/*
 *  Api Scanner
 */
class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++].trim(); // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray(fn) {
    const array = this.nextLine().split(' ');
    return fn === String ? array : array.map(fn);
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
    n = readInt()
    s = Array.from(readLine())
    t = Array.from(readLine())
    res = []
    for (i=0; i<n; i++){
        if (s[i]!=t[i]){
            index = s.indexOf(t[i], i+1)
            if (index==-1){
                print(-1)
                return
            } else {
                for (j=index; j>i; j--){
                    [s[j], s[j-1]]= [s[j-1], s[j]]
                    res.push(j)
                }
            }
        }
    }
    print(res.length)
    print(res.join(" "))
}

const TESTCASES = false
/* NodeJS Footer by @satyamcse and @shivam1420*/
var readline = require('readline');
process.stdin.setEncoding('utf-8');
var rl = readline.createInterface({input: process.stdin, output: process.stdout});
var inp = "";
rl.on('line' , v => inp += v+"\n");
rl.on('close', () => {
    inp = inp.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
    if (TESTCASES){
        const cnt = readInt()
        for (let i = 0; i<cnt; i++){
            main()
        }
    } else {main()}
});
function sort(a, inc = true) {
    if(inc) a.sort((a, b) => a - b)
    else a.sort((a, b) => b - a)
}
var ptr = 0
function print(...x) {console.log(...x)}
function readInt() {return +inp[ptr++]}
function readLine() {return inp[ptr++]}
function readInts() {return inp[ptr++].split(' ').map(a => +a)}

'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 inp() {
    return inputString[currentLine++];
}

function out(str) {
    console.log(str);
}

function stringArrayToNumbers(str) {
    var array = str.split(" ");
    var result = [];
    for (var i = 0; i < array.length; i++) {
        result.push(parseInt(array[i]));
    }
    return result;
}
function main() {
    let n = parseInt(inp());
    let s, t;
    s = inp();
    t = inp();
    let s1 = [];
    let i, j;
    //check
    var dc = new Object();
    for (i = 0; i < n; i++) {
        s1.push(s[i]);
        if (s[i] in dc) {
            dc[s[i]] += 1
        }
        else {
            dc[s[i]] = 1
        }
    }
    var chk = 0;
    for (i = 0; i < n; i++) {
        if (t[i] in dc) {
            dc[t[i]] -= 1;
            if (dc[t[i]] < 0) {
                chk = 1;
                break
            }
        }
        else {
            chk = 1;
            break
        }
    }
    if (chk == 1) {
        out(-1)
    }
    else {
        var ans = [];
        for (i = 0; i < n; i++) {
            if (s1[i] != t[i]) {
                let pos = i;
                for (j = i + 1; j < n; j++) {
                    if (s1[j] == t[i]) {
                        pos = j;
                        break;
                    }
                }
                for (j = pos - 1; j >= i; j--) {
                    ans.push(j+1);
                    let t = s1[j];
                    s1[j] = s1[j + 1];
                    s1[j + 1] = t;
                }
            }
        }
        out(ans.length);
        let ans1 = '';
        for (i = 0; i < ans.length; i++) {
            ans1 += ans[i] + ' ';
        }
        out(ans1);

    }

}

function main(){
    var c = +readline();
    var str = readline();
    var strRes = readline();
    var result = [];

    for(var i = 0; i < c; i++){
        if(str[i] != strRes[i]){
            var s = str.substr(i).indexOf(strRes[i]);
            if(s == -1){
                print(-1);
                return;
            }
            s+=i;
            while(s != i){
                result.push(s);
                str = str.substr(0,s-1) + str[s] + str[s-1] + str.substr(s+1);
                s--;
            }
        }
    }
    if(result.length > 10000){
        print(-1);
        return;
    }
    print(result.length + '\n');
    result.forEach(function(v){
        print(v + ' ');
    })
}

main();

function main() {
  const is = new Scanner();
  is.nextInt();
  const s = is.nextLine().split('');
  const t = is.nextLine().split('');
  const container = {};
  s.forEach((item) => {
    if (container[item] === undefined) {
      container[item] = 1;
    } else {
      container[item] += 1;
    }
  });
  t.forEach((item) => {
    container[item] -= 1;
  });

  if (Object.keys(container).some(key => container[key] !== 0)) {
    console.log(-1);
  } else {
    let j;
    const swaps = [];
    t.forEach((item, i) => {
      if (item !== s[i]) {
        j = s.lastIndexOf(item);
        for (let k = j - 1; k !== i - 1; k -= 1) {
          [s[k], s[k + 1]] = [s[k + 1], s[k]];
          swaps.push(k + 1);
        }
      }
    });
    console.log(swaps.length);
    if (swaps.length) {
      console.log(swaps.join(' '));
    }
  }
}

/*
 *  Api Scanner
 */
class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray(fn) {
    const array = this.nextLine().split(' ');
    return fn === String ? array : array.map(fn);
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main(){
    var c = +readline();
    var str = readline();
    var strRes = readline();
    var result = [];

    for(var i = 0; i < c; i++){
        if(str[i] != strRes[i]){
            var s = str.substr(i).indexOf(strRes[i]);
            if(s == -1){
                print(-1);
                return;
            }
            s+=i;
            while(s != i){
                result.push(s);
                var v = str[s];
                str[s] = str[s-1];
                str[s-1] = v;
                s--;
            }
        }
    }
    if(result.length>10000){
        print(-1);
        return;
    }
    print(result.length + '\n');
    result.forEach(function(v){
        print(v + ' ');
    })
}

main();


One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is.

The first line of the input contains an integer n (1 ≤ n ≤ 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn’t exceed 100.

For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 can’t recognize whose sentence it is. He can’t recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions.

5
I will go to play with you lala.
wow, welcome.
miao.lala.
miao.
miao .

Freda's
OMG>.< I don't know!
OMG>.< I don't know!
Rainbow's
OMG>.< I don't know!

print(function(n) {
	var ans = [];
	for (var i = 0; i < n; i++){
		var s = readline();
		var a = /^miao\./.test(s);
		var b = /lala\.$/.test(s);
		if(a&&!b) ans.push('Rainbow\'s');
		else if(b&&!a) ans.push('Freda\'s');
		else ans.push('OMG>.< I don\'t know!');
	}
	return ans.join('\n');
}(+readline()));


Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.

The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at. The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).

NotePictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):

var Point = function(x, y) { this.x = parseInt(x); this.y = parseInt(y); };
var Line = function(p) {
    this.a = parseInt(p[0]);
    this.b = parseInt(p[1]);
    this.c = parseInt(p[2]);
    this.getY = function(x) { return (this.a * -x) - this.c; };
    this.getLiner = function()    { return -this.a / this.b; };
};
var app = {
    lineLength : 0,
    A : {}, B : {},
    result : 0,
    main : function()    {
        this.initialize();
        this.readValues();
        this.sovleResult();
        this.printResult();
    },
    initialize : function()    {
        app.result = 0;
    },
    readValues : function()    {
        var ACor = readline().split(/\s/),
            BCor = readline().split(/\s/);

        app.A = new Point(ACor[0], ACor[1]);
        app.B = new Point(BCor[0], BCor[1]);
        app.lineLength = parseInt(readline());
    },
    sovleResult : function()    {
        for(var index = 0; index < app.lineLength; index++)    {
            var line = new Line(readline().split(/\s/)),
                liner = line.getLiner(),
                whenAxToY = line.getY(app.A.x),
                whenBxToY = line.getY(app.B.x);

            // console.log(app.A.y * line.b + ' ' + whenAxToY + ', ' + app.B.y * line.b + ' ' + whenBxToY);
            if(app.A.y * line.b >= whenAxToY && app.B.y * line.b <= whenBxToY)    app.result++;
            else if(app.A.y * line.b <= whenAxToY && app.B.y * line.b >= whenBxToY)    app.result++;
        }
    },
    printResult : function()    {
        print(this.result);
    }
};

app.main();

var Point = function(x, y) { this.x = parseInt(x) / 1e6; this.y = parseInt(y) / 1e6; };
var Line = function(p) {
    this.a = parseInt(p[0]) / 1e6;
    this.b = parseInt(p[1]) / 1e6;
    this.c = parseInt(p[2]) / 1e6;
    this.getY = function(x) { return (this.a * -x + this.c) / this.b; };
};
var app = {
    lineLength : 0,
    home : {}, universe : {},
    lineEqMap : [],
    result : 0,
    main : function()    {
        this.initialize();
        this.readValues();
        this.sovleResult();
        this.printResult();
    },
    initialize : function()    {},
    readValues : function()    {
        var homeCor = readline().split(/\s/),
            uniCor = readline().split(/\s/);

        app.home = new Point(homeCor[0], homeCor[1]);
        app.universe = new Point(uniCor[0], uniCor[1]);
        app.lineLength = parseInt(readline());
    },
    sovleResult : function()    {
        var hToUG = (app.universe.y - app.home.y) / (app.universe.x - app.home.x) >=0 ? true : false;
        for(var index = 0; index < app.lineLength; index++)    {
            var line = new Line(readline().split(/\s/)),
                whenHomeXtoY = line.getY(app.home.x),
                whenUniXtoY = line.getY(app.universe.x);

            if(hToUG)    {
                if(app.home.y >= whenHomeXtoY && app.universe.y <= whenUniXtoY)    app.result++;
            } else    {
                if(app.home.y <= whenHomeXtoY && app.universe.y >= whenUniXtoY)    app.result++;
            }
        }
    },
    printResult : function()    {
        print(this.result);
    }
};

app.main();

var Point = function(x, y) { this.x = parseInt(x) / 1e4; this.y = parseInt(y) / 1e4; };
var Line = function(p) {
    this.a = parseInt(p[0]) / 1e4;
    this.b = parseInt(p[1]) / 1e4;
    this.c = parseInt(p[2]) / 1e4;
    this.getY = function(x) { return (this.a * -x + this.c) / this.b; };
};
var app = {
    lineLength : 0,
    home : {}, universe : {},
    lineEqMap : [],
    result : 0,
    main : function()    {
        this.initialize();
        this.readValues();
        this.sovleResult();
        this.printResult();
    },
    initialize : function()    {},
    readValues : function()    {
        var homeCor = readline().split(/\s/),
            uniCor = readline().split(/\s/);

        app.home = new Point(homeCor[0], homeCor[1]);
        app.universe = new Point(uniCor[0], uniCor[1]);
        app.lineLength = parseInt(readline());
    },
    sovleResult : function()    {
        var hToUG = (app.universe.y - app.home.y) / (app.universe.x - app.home.x) >=0 ? true : false;
        for(var index = 0; index < app.lineLength; index++)    {
            var line = new Line(readline().split(/\s/)),
                whenHomeXtoY = line.getY(app.home.x),
                whenUniXtoY = line.getY(app.universe.x);

            if(hToUG)    {
                if(app.home.y >= whenHomeXtoY && app.universe.y <= whenUniXtoY)    app.result++;
            } else    {
                if(app.home.y <= whenHomeXtoY && app.universe.y >= whenUniXtoY)    app.result++;
            }
        }
    },
    printResult : function()    {
        print(this.result);
    }
};

app.main();

var Point = function(x, y) { this.x = parseInt(x); this.y = parseInt(y); };
var Line = function(p) { this.a = parseInt(p[0]); this.b = parseInt(p[1]); this.c = parseInt(p[2]); this.getY = function(x) { return (this.a * -x + this.c) / this.b; }; };
var app = {
    lineLength : 0,
    home : {}, universe : {},
    lineEqMap : [],
    result : 0,
    main : function()    {
        this.initialize();
        this.readValues();
        this.sovleResult();
        this.printResult();
    },
    initialize : function()    {},
    readValues : function()    {
        var homeCor = readline().split(/\s/),
            uniCor = readline().split(/\s/);

        app.home = new Point(homeCor[0], homeCor[1]);
        app.universe = new Point(uniCor[0], uniCor[1]);
        app.lineLength = parseInt(readline());
        for(var index = 0; index < app.lineLength; index++)    app.lineEqMap.push(new Line(readline().split(/\s/)));
    },
    sovleResult : function()    {
        var hToUG = (app.universe.y - app.home.y) / (app.universe.x - app.home.x) >=0 ? true : false;
        app.lineEqMap.forEach(function(line)    {
            var whenHomeXtoY = line.getY(app.home.x),
                whenUniXtoY = line.getY(app.universe.x);

            if(hToUG)    {
                if(app.home.y >= whenHomeXtoY && app.universe.y <= whenUniXtoY)    app.result++;
            } else    {
                if(app.home.y <= whenHomeXtoY && app.universe.y >= whenUniXtoY)    app.result++;
            }
        });
    },
    printResult : function()    {
        print(this.result);
    }
};

app.main();


You are given an integer $$$n$$$ and an integer $$$k$$$.In one step you can do one of the following moves:   decrease $$$n$$$ by $$$1$$$;  divide $$$n$$$ by $$$k$$$ if $$$n$$$ is divisible by $$$k$$$. For example, if $$$n = 27$$$ and $$$k = 3$$$ you can do the following steps: $$$27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$$$.You are asked to calculate the minimum number of steps to reach $$$0$$$ from $$$n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The only line of each query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^{18}$$$, $$$2 \le k \le 10^{18}$$$).

For each query print the minimum number of steps to reach $$$0$$$ from $$$n$$$ in single line.

NoteSteps for the first test case are: $$$59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$$$.In the second test case you have to divide $$$n$$$ by $$$k$$$ $$$18$$$ times and then decrease $$$n$$$ by $$$1$$$.

var bigInt = (function(undefined) {
  'use strict';

  var BASE = 1e7,
    LOG_BASE = 7,
    MAX_INT = 9007199254740992,
    MAX_INT_ARR = smallToArray(MAX_INT),
    DEFAULT_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';

  var supportsNativeBigInt = typeof BigInt === 'function';

  function Integer(v, radix, alphabet, caseSensitive) {
    if (typeof v === 'undefined') return Integer[0];
    if (typeof radix !== 'undefined')
      return +radix === 10 && !alphabet
        ? parseValue(v)
        : parseBase(v, radix, alphabet, caseSensitive);
    return parseValue(v);
  }

  function BigInteger(value, sign) {
    this.value = value;
    this.sign = sign;
    this.isSmall = false;
  }
  BigInteger.prototype = Object.create(Integer.prototype);

  function SmallInteger(value) {
    this.value = value;
    this.sign = value < 0;
    this.isSmall = true;
  }
  SmallInteger.prototype = Object.create(Integer.prototype);

  function NativeBigInt(value) {
    this.value = value;
  }
  NativeBigInt.prototype = Object.create(Integer.prototype);

  function isPrecise(n) {
    return -MAX_INT < n && n < MAX_INT;
  }

  function smallToArray(n) {
    // For performance reasons doesn't reference BASE, need to change this function if BASE changes
    if (n < 1e7) return [n];
    if (n < 1e14) return [n % 1e7, Math.floor(n / 1e7)];
    return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
  }

  function arrayToSmall(arr) {
    // If BASE changes this function may need to change
    trim(arr);
    var length = arr.length;
    if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
      switch (length) {
        case 0:
          return 0;
        case 1:
          return arr[0];
        case 2:
          return arr[0] + arr[1] * BASE;
        default:
          return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
      }
    }
    return arr;
  }

  function trim(v) {
    var i = v.length;
    while (v[--i] === 0);
    v.length = i + 1;
  }

  function createArray(length) {
    // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
    var x = new Array(length);
    var i = -1;
    while (++i < length) {
      x[i] = 0;
    }
    return x;
  }

  function truncate(n) {
    if (n > 0) return Math.floor(n);
    return Math.ceil(n);
  }

  function add(a, b) {
    // assumes a and b are arrays with a.length >= b.length
    var l_a = a.length,
      l_b = b.length,
      r = new Array(l_a),
      carry = 0,
      base = BASE,
      sum,
      i;
    for (i = 0; i < l_b; i++) {
      sum = a[i] + b[i] + carry;
      carry = sum >= base ? 1 : 0;
      r[i] = sum - carry * base;
    }
    while (i < l_a) {
      sum = a[i] + carry;
      carry = sum === base ? 1 : 0;
      r[i++] = sum - carry * base;
    }
    if (carry > 0) r.push(carry);
    return r;
  }

  function addAny(a, b) {
    if (a.length >= b.length) return add(a, b);
    return add(b, a);
  }

  function addSmall(a, carry) {
    // assumes a is array, carry is number with 0 <= carry < MAX_INT
    var l = a.length,
      r = new Array(l),
      base = BASE,
      sum,
      i;
    for (i = 0; i < l; i++) {
      sum = a[i] - base + carry;
      carry = Math.floor(sum / base);
      r[i] = sum - carry * base;
      carry += 1;
    }
    while (carry > 0) {
      r[i++] = carry % base;
      carry = Math.floor(carry / base);
    }
    return r;
  }

  BigInteger.prototype.add = function(v) {
    var n = parseValue(v);
    if (this.sign !== n.sign) {
      return this.subtract(n.negate());
    }
    var a = this.value,
      b = n.value;
    if (n.isSmall) {
      return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
    }
    return new BigInteger(addAny(a, b), this.sign);
  };
  BigInteger.prototype.plus = BigInteger.prototype.add;

  SmallInteger.prototype.add = function(v) {
    var n = parseValue(v);
    var a = this.value;
    if (a < 0 !== n.sign) {
      return this.subtract(n.negate());
    }
    var b = n.value;
    if (n.isSmall) {
      if (isPrecise(a + b)) return new SmallInteger(a + b);
      b = smallToArray(Math.abs(b));
    }
    return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
  };
  SmallInteger.prototype.plus = SmallInteger.prototype.add;

  NativeBigInt.prototype.add = function(v) {
    return new NativeBigInt(this.value + parseValue(v).value);
  };
  NativeBigInt.prototype.plus = NativeBigInt.prototype.add;

  function subtract(a, b) {
    // assumes a and b are arrays with a >= b
    var a_l = a.length,
      b_l = b.length,
      r = new Array(a_l),
      borrow = 0,
      base = BASE,
      i,
      difference;
    for (i = 0; i < b_l; i++) {
      difference = a[i] - borrow - b[i];
      if (difference < 0) {
        difference += base;
        borrow = 1;
      } else borrow = 0;
      r[i] = difference;
    }
    for (i = b_l; i < a_l; i++) {
      difference = a[i] - borrow;
      if (difference < 0) difference += base;
      else {
        r[i++] = difference;
        break;
      }
      r[i] = difference;
    }
    for (; i < a_l; i++) {
      r[i] = a[i];
    }
    trim(r);
    return r;
  }

  function subtractAny(a, b, sign) {
    var value;
    if (compareAbs(a, b) >= 0) {
      value = subtract(a, b);
    } else {
      value = subtract(b, a);
      sign = !sign;
    }
    value = arrayToSmall(value);
    if (typeof value === 'number') {
      if (sign) value = -value;
      return new SmallInteger(value);
    }
    return new BigInteger(value, sign);
  }

  function subtractSmall(a, b, sign) {
    // assumes a is array, b is number with 0 <= b < MAX_INT
    var l = a.length,
      r = new Array(l),
      carry = -b,
      base = BASE,
      i,
      difference;
    for (i = 0; i < l; i++) {
      difference = a[i] + carry;
      carry = Math.floor(difference / base);
      difference %= base;
      r[i] = difference < 0 ? difference + base : difference;
    }
    r = arrayToSmall(r);
    if (typeof r === 'number') {
      if (sign) r = -r;
      return new SmallInteger(r);
    }
    return new BigInteger(r, sign);
  }

  BigInteger.prototype.subtract = function(v) {
    var n = parseValue(v);
    if (this.sign !== n.sign) {
      return this.add(n.negate());
    }
    var a = this.value,
      b = n.value;
    if (n.isSmall) return subtractSmall(a, Math.abs(b), this.sign);
    return subtractAny(a, b, this.sign);
  };
  BigInteger.prototype.minus = BigInteger.prototype.subtract;

  SmallInteger.prototype.subtract = function(v) {
    var n = parseValue(v);
    var a = this.value;
    if (a < 0 !== n.sign) {
      return this.add(n.negate());
    }
    var b = n.value;
    if (n.isSmall) {
      return new SmallInteger(a - b);
    }
    return subtractSmall(b, Math.abs(a), a >= 0);
  };
  SmallInteger.prototype.minus = SmallInteger.prototype.subtract;

  NativeBigInt.prototype.subtract = function(v) {
    return new NativeBigInt(this.value - parseValue(v).value);
  };
  NativeBigInt.prototype.minus = NativeBigInt.prototype.subtract;

  BigInteger.prototype.negate = function() {
    return new BigInteger(this.value, !this.sign);
  };
  SmallInteger.prototype.negate = function() {
    var sign = this.sign;
    var small = new SmallInteger(-this.value);
    small.sign = !sign;
    return small;
  };
  NativeBigInt.prototype.negate = function() {
    return new NativeBigInt(-this.value);
  };

  BigInteger.prototype.abs = function() {
    return new BigInteger(this.value, false);
  };
  SmallInteger.prototype.abs = function() {
    return new SmallInteger(Math.abs(this.value));
  };
  NativeBigInt.prototype.abs = function() {
    return new NativeBigInt(this.value >= 0 ? this.value : -this.value);
  };

  function multiplyLong(a, b) {
    var a_l = a.length,
      b_l = b.length,
      l = a_l + b_l,
      r = createArray(l),
      base = BASE,
      product,
      carry,
      i,
      a_i,
      b_j;
    for (i = 0; i < a_l; ++i) {
      a_i = a[i];
      for (var j = 0; j < b_l; ++j) {
        b_j = b[j];
        product = a_i * b_j + r[i + j];
        carry = Math.floor(product / base);
        r[i + j] = product - carry * base;
        r[i + j + 1] += carry;
      }
    }
    trim(r);
    return r;
  }

  function multiplySmall(a, b) {
    // assumes a is array, b is number with |b| < BASE
    var l = a.length,
      r = new Array(l),
      base = BASE,
      carry = 0,
      product,
      i;
    for (i = 0; i < l; i++) {
      product = a[i] * b + carry;
      carry = Math.floor(product / base);
      r[i] = product - carry * base;
    }
    while (carry > 0) {
      r[i++] = carry % base;
      carry = Math.floor(carry / base);
    }
    return r;
  }

  function shiftLeft(x, n) {
    var r = [];
    while (n-- > 0) r.push(0);
    return r.concat(x);
  }

  function multiplyKaratsuba(x, y) {
    var n = Math.max(x.length, y.length);

    if (n <= 30) return multiplyLong(x, y);
    n = Math.ceil(n / 2);

    var b = x.slice(n),
      a = x.slice(0, n),
      d = y.slice(n),
      c = y.slice(0, n);

    var ac = multiplyKaratsuba(a, c),
      bd = multiplyKaratsuba(b, d),
      abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));

    var product = addAny(
      addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)),
      shiftLeft(bd, 2 * n)
    );
    trim(product);
    return product;
  }

  // The following function is derived from a surface fit of a graph plotting the performance difference
  // between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
  function useKaratsuba(l1, l2) {
    return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
  }

  BigInteger.prototype.multiply = function(v) {
    var n = parseValue(v),
      a = this.value,
      b = n.value,
      sign = this.sign !== n.sign,
      abs;
    if (n.isSmall) {
      if (b === 0) return Integer[0];
      if (b === 1) return this;
      if (b === -1) return this.negate();
      abs = Math.abs(b);
      if (abs < BASE) {
        return new BigInteger(multiplySmall(a, abs), sign);
      }
      b = smallToArray(abs);
    }
    if (useKaratsuba(a.length, b.length))
      // Karatsuba is only faster for certain array sizes
      return new BigInteger(multiplyKaratsuba(a, b), sign);
    return new BigInteger(multiplyLong(a, b), sign);
  };

  BigInteger.prototype.times = BigInteger.prototype.multiply;

  function multiplySmallAndArray(a, b, sign) {
    // a >= 0
    if (a < BASE) {
      return new BigInteger(multiplySmall(b, a), sign);
    }
    return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
  }
  SmallInteger.prototype._multiplyBySmall = function(a) {
    if (isPrecise(a.value * this.value)) {
      return new SmallInteger(a.value * this.value);
    }
    return multiplySmallAndArray(
      Math.abs(a.value),
      smallToArray(Math.abs(this.value)),
      this.sign !== a.sign
    );
  };
  BigInteger.prototype._multiplyBySmall = function(a) {
    if (a.value === 0) return Integer[0];
    if (a.value === 1) return this;
    if (a.value === -1) return this.negate();
    return multiplySmallAndArray(
      Math.abs(a.value),
      this.value,
      this.sign !== a.sign
    );
  };
  SmallInteger.prototype.multiply = function(v) {
    return parseValue(v)._multiplyBySmall(this);
  };
  SmallInteger.prototype.times = SmallInteger.prototype.multiply;

  NativeBigInt.prototype.multiply = function(v) {
    return new NativeBigInt(this.value * parseValue(v).value);
  };
  NativeBigInt.prototype.times = NativeBigInt.prototype.multiply;

  function square(a) {
    //console.assert(2 * BASE * BASE < MAX_INT);
    var l = a.length,
      r = createArray(l + l),
      base = BASE,
      product,
      carry,
      i,
      a_i,
      a_j;
    for (i = 0; i < l; i++) {
      a_i = a[i];
      carry = 0 - a_i * a_i;
      for (var j = i; j < l; j++) {
        a_j = a[j];
        product = 2 * (a_i * a_j) + r[i + j] + carry;
        carry = Math.floor(product / base);
        r[i + j] = product - carry * base;
      }
      r[i + l] = carry;
    }
    trim(r);
    return r;
  }

  BigInteger.prototype.square = function() {
    return new BigInteger(square(this.value), false);
  };

  SmallInteger.prototype.square = function() {
    var value = this.value * this.value;
    if (isPrecise(value)) return new SmallInteger(value);
    return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
  };

  NativeBigInt.prototype.square = function(v) {
    return new NativeBigInt(this.value * this.value);
  };

  function divMod1(a, b) {
    // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
    var a_l = a.length,
      b_l = b.length,
      base = BASE,
      result = createArray(b.length),
      divisorMostSignificantDigit = b[b_l - 1],
      // normalization
      lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
      remainder = multiplySmall(a, lambda),
      divisor = multiplySmall(b, lambda),
      quotientDigit,
      shift,
      carry,
      borrow,
      i,
      l,
      q;
    if (remainder.length <= a_l) remainder.push(0);
    divisor.push(0);
    divisorMostSignificantDigit = divisor[b_l - 1];
    for (shift = a_l - b_l; shift >= 0; shift--) {
      quotientDigit = base - 1;
      if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
        quotientDigit = Math.floor(
          (remainder[shift + b_l] * base + remainder[shift + b_l - 1]) /
            divisorMostSignificantDigit
        );
      }
      // quotientDigit <= base - 1
      carry = 0;
      borrow = 0;
      l = divisor.length;
      for (i = 0; i < l; i++) {
        carry += quotientDigit * divisor[i];
        q = Math.floor(carry / base);
        borrow += remainder[shift + i] - (carry - q * base);
        carry = q;
        if (borrow < 0) {
          remainder[shift + i] = borrow + base;
          borrow = -1;
        } else {
          remainder[shift + i] = borrow;
          borrow = 0;
        }
      }
      while (borrow !== 0) {
        quotientDigit -= 1;
        carry = 0;
        for (i = 0; i < l; i++) {
          carry += remainder[shift + i] - base + divisor[i];
          if (carry < 0) {
            remainder[shift + i] = carry + base;
            carry = 0;
          } else {
            remainder[shift + i] = carry;
            carry = 1;
          }
        }
        borrow += carry;
      }
      result[shift] = quotientDigit;
    }
    // denormalization
    remainder = divModSmall(remainder, lambda)[0];
    return [arrayToSmall(result), arrayToSmall(remainder)];
  }

  function divMod2(a, b) {
    // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
    // Performs faster than divMod1 on larger input sizes.
    var a_l = a.length,
      b_l = b.length,
      result = [],
      part = [],
      base = BASE,
      guess,
      xlen,
      highx,
      highy,
      check;
    while (a_l) {
      part.unshift(a[--a_l]);
      trim(part);
      if (compareAbs(part, b) < 0) {
        result.push(0);
        continue;
      }
      xlen = part.length;
      highx = part[xlen - 1] * base + part[xlen - 2];
      highy = b[b_l - 1] * base + b[b_l - 2];
      if (xlen > b_l) {
        highx = (highx + 1) * base;
      }
      guess = Math.ceil(highx / highy);
      do {
        check = multiplySmall(b, guess);
        if (compareAbs(check, part) <= 0) break;
        guess--;
      } while (guess);
      result.push(guess);
      part = subtract(part, check);
    }
    result.reverse();
    return [arrayToSmall(result), arrayToSmall(part)];
  }

  function divModSmall(value, lambda) {
    var length = value.length,
      quotient = createArray(length),
      base = BASE,
      i,
      q,
      remainder,
      divisor;
    remainder = 0;
    for (i = length - 1; i >= 0; --i) {
      divisor = remainder * base + value[i];
      q = truncate(divisor / lambda);
      remainder = divisor - q * lambda;
      quotient[i] = q | 0;
    }
    return [quotient, remainder | 0];
  }

  function divModAny(self, v) {
    var value,
      n = parseValue(v);
    if (supportsNativeBigInt) {
      return [
        new NativeBigInt(self.value / n.value),
        new NativeBigInt(self.value % n.value),
      ];
    }
    var a = self.value,
      b = n.value;
    var quotient;
    if (b === 0) throw new Error('Cannot divide by zero');
    if (self.isSmall) {
      if (n.isSmall) {
        return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
      }
      return [Integer[0], self];
    }
    if (n.isSmall) {
      if (b === 1) return [self, Integer[0]];
      if (b == -1) return [self.negate(), Integer[0]];
      var abs = Math.abs(b);
      if (abs < BASE) {
        value = divModSmall(a, abs);
        quotient = arrayToSmall(value[0]);
        var remainder = value[1];
        if (self.sign) remainder = -remainder;
        if (typeof quotient === 'number') {
          if (self.sign !== n.sign) quotient = -quotient;
          return [new SmallInteger(quotient), new SmallInteger(remainder)];
        }
        return [
          new BigInteger(quotient, self.sign !== n.sign),
          new SmallInteger(remainder),
        ];
      }
      b = smallToArray(abs);
    }
    var comparison = compareAbs(a, b);
    if (comparison === -1) return [Integer[0], self];
    if (comparison === 0)
      return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];

    // divMod1 is faster on smaller input sizes
    if (a.length + b.length <= 200) value = divMod1(a, b);
    else value = divMod2(a, b);

    quotient = value[0];
    var qSign = self.sign !== n.sign,
      mod = value[1],
      mSign = self.sign;
    if (typeof quotient === 'number') {
      if (qSign) quotient = -quotient;
      quotient = new SmallInteger(quotient);
    } else quotient = new BigInteger(quotient, qSign);
    if (typeof mod === 'number') {
      if (mSign) mod = -mod;
      mod = new SmallInteger(mod);
    } else mod = new BigInteger(mod, mSign);
    return [quotient, mod];
  }

  BigInteger.prototype.divmod = function(v) {
    var result = divModAny(this, v);
    return {
      quotient: result[0],
      remainder: result[1],
    };
  };
  NativeBigInt.prototype.divmod = SmallInteger.prototype.divmod =
    BigInteger.prototype.divmod;

  BigInteger.prototype.divide = function(v) {
    return divModAny(this, v)[0];
  };
  NativeBigInt.prototype.over = NativeBigInt.prototype.divide = function(v) {
    return new NativeBigInt(this.value / parseValue(v).value);
  };
  SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over =
    BigInteger.prototype.divide;

  BigInteger.prototype.mod = function(v) {
    return divModAny(this, v)[1];
  };
  NativeBigInt.prototype.mod = NativeBigInt.prototype.remainder = function(v) {
    return new NativeBigInt(this.value % parseValue(v).value);
  };
  SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder =
    BigInteger.prototype.mod;

  BigInteger.prototype.pow = function(v) {
    var n = parseValue(v),
      a = this.value,
      b = n.value,
      value,
      x,
      y;
    if (b === 0) return Integer[1];
    if (a === 0) return Integer[0];
    if (a === 1) return Integer[1];
    if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
    if (n.sign) {
      return Integer[0];
    }
    if (!n.isSmall)
      throw new Error('The exponent ' + n.toString() + ' is too large.');
    if (this.isSmall) {
      if (isPrecise((value = Math.pow(a, b))))
        return new SmallInteger(truncate(value));
    }
    x = this;
    y = Integer[1];
    while (true) {
      if (b & (1 === 1)) {
        y = y.times(x);
        --b;
      }
      if (b === 0) break;
      b /= 2;
      x = x.square();
    }
    return y;
  };
  SmallInteger.prototype.pow = BigInteger.prototype.pow;

  NativeBigInt.prototype.pow = function(v) {
    var n = parseValue(v);
    var a = this.value,
      b = n.value;
    var _0 = BigInt(0),
      _1 = BigInt(1),
      _2 = BigInt(2);
    if (b === _0) return Integer[1];
    if (a === _0) return Integer[0];
    if (a === _1) return Integer[1];
    if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1];
    if (n.isNegative()) return new NativeBigInt(_0);
    var x = this;
    var y = Integer[1];
    while (true) {
      if ((b & _1) === _1) {
        y = y.times(x);
        --b;
      }
      if (b === _0) break;
      b /= _2;
      x = x.square();
    }
    return y;
  };

  BigInteger.prototype.modPow = function(exp, mod) {
    exp = parseValue(exp);
    mod = parseValue(mod);
    if (mod.isZero()) throw new Error('Cannot take modPow with modulus 0');
    var r = Integer[1],
      base = this.mod(mod);
    while (exp.isPositive()) {
      if (base.isZero()) return Integer[0];
      if (exp.isOdd()) r = r.multiply(base).mod(mod);
      exp = exp.divide(2);
      base = base.square().mod(mod);
    }
    return r;
  };
  NativeBigInt.prototype.modPow = SmallInteger.prototype.modPow =
    BigInteger.prototype.modPow;

  function compareAbs(a, b) {
    if (a.length !== b.length) {
      return a.length > b.length ? 1 : -1;
    }
    for (var i = a.length - 1; i >= 0; i--) {
      if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
    }
    return 0;
  }

  BigInteger.prototype.compareAbs = function(v) {
    var n = parseValue(v),
      a = this.value,
      b = n.value;
    if (n.isSmall) return 1;
    return compareAbs(a, b);
  };
  SmallInteger.prototype.compareAbs = function(v) {
    var n = parseValue(v),
      a = Math.abs(this.value),
      b = n.value;
    if (n.isSmall) {
      b = Math.abs(b);
      return a === b ? 0 : a > b ? 1 : -1;
    }
    return -1;
  };
  NativeBigInt.prototype.compareAbs = function(v) {
    var a = this.value;
    var b = parseValue(v).value;
    a = a >= 0 ? a : -a;
    b = b >= 0 ? b : -b;
    return a === b ? 0 : a > b ? 1 : -1;
  };

  BigInteger.prototype.compare = function(v) {
    // See discussion about comparison with Infinity:
    // https://github.com/peterolson/BigInteger.js/issues/61
    if (v === Infinity) {
      return -1;
    }
    if (v === -Infinity) {
      return 1;
    }

    var n = parseValue(v),
      a = this.value,
      b = n.value;
    if (this.sign !== n.sign) {
      return n.sign ? 1 : -1;
    }
    if (n.isSmall) {
      return this.sign ? -1 : 1;
    }
    return compareAbs(a, b) * (this.sign ? -1 : 1);
  };
  BigInteger.prototype.compareTo = BigInteger.prototype.compare;

  SmallInteger.prototype.compare = function(v) {
    if (v === Infinity) {
      return -1;
    }
    if (v === -Infinity) {
      return 1;
    }

    var n = parseValue(v),
      a = this.value,
      b = n.value;
    if (n.isSmall) {
      return a == b ? 0 : a > b ? 1 : -1;
    }
    if (a < 0 !== n.sign) {
      return a < 0 ? -1 : 1;
    }
    return a < 0 ? 1 : -1;
  };
  SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;

  NativeBigInt.prototype.compare = function(v) {
    if (v === Infinity) {
      return -1;
    }
    if (v === -Infinity) {
      return 1;
    }
    var a = this.value;
    var b = parseValue(v).value;
    return a === b ? 0 : a > b ? 1 : -1;
  };
  NativeBigInt.prototype.compareTo = NativeBigInt.prototype.compare;

  BigInteger.prototype.equals = function(v) {
    return this.compare(v) === 0;
  };
  NativeBigInt.prototype.eq = NativeBigInt.prototype.equals = SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq =
    BigInteger.prototype.equals;

  BigInteger.prototype.notEquals = function(v) {
    return this.compare(v) !== 0;
  };
  NativeBigInt.prototype.neq = NativeBigInt.prototype.notEquals = SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq =
    BigInteger.prototype.notEquals;

  BigInteger.prototype.greater = function(v) {
    return this.compare(v) > 0;
  };
  NativeBigInt.prototype.gt = NativeBigInt.prototype.greater = SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt =
    BigInteger.prototype.greater;

  BigInteger.prototype.lesser = function(v) {
    return this.compare(v) < 0;
  };
  NativeBigInt.prototype.lt = NativeBigInt.prototype.lesser = SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt =
    BigInteger.prototype.lesser;

  BigInteger.prototype.greaterOrEquals = function(v) {
    return this.compare(v) >= 0;
  };
  NativeBigInt.prototype.geq = NativeBigInt.prototype.greaterOrEquals = SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq =
    BigInteger.prototype.greaterOrEquals;

  BigInteger.prototype.lesserOrEquals = function(v) {
    return this.compare(v) <= 0;
  };
  NativeBigInt.prototype.leq = NativeBigInt.prototype.lesserOrEquals = SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq =
    BigInteger.prototype.lesserOrEquals;

  BigInteger.prototype.isEven = function() {
    return (this.value[0] & 1) === 0;
  };
  SmallInteger.prototype.isEven = function() {
    return (this.value & 1) === 0;
  };
  NativeBigInt.prototype.isEven = function() {
    return (this.value & BigInt(1)) === BigInt(0);
  };

  BigInteger.prototype.isOdd = function() {
    return (this.value[0] & 1) === 1;
  };
  SmallInteger.prototype.isOdd = function() {
    return (this.value & 1) === 1;
  };
  NativeBigInt.prototype.isOdd = function() {
    return (this.value & BigInt(1)) === BigInt(1);
  };

  BigInteger.prototype.isPositive = function() {
    return !this.sign;
  };
  SmallInteger.prototype.isPositive = function() {
    return this.value > 0;
  };
  NativeBigInt.prototype.isPositive = SmallInteger.prototype.isPositive;

  BigInteger.prototype.isNegative = function() {
    return this.sign;
  };
  SmallInteger.prototype.isNegative = function() {
    return this.value < 0;
  };
  NativeBigInt.prototype.isNegative = SmallInteger.prototype.isNegative;

  BigInteger.prototype.isUnit = function() {
    return false;
  };
  SmallInteger.prototype.isUnit = function() {
    return Math.abs(this.value) === 1;
  };
  NativeBigInt.prototype.isUnit = function() {
    return this.abs().value === BigInt(1);
  };

  BigInteger.prototype.isZero = function() {
    return false;
  };
  SmallInteger.prototype.isZero = function() {
    return this.value === 0;
  };
  NativeBigInt.prototype.isZero = function() {
    return this.value === BigInt(0);
  };

  BigInteger.prototype.isDivisibleBy = function(v) {
    var n = parseValue(v);
    if (n.isZero()) return false;
    if (n.isUnit()) return true;
    if (n.compareAbs(2) === 0) return this.isEven();
    return this.mod(n).isZero();
  };
  NativeBigInt.prototype.isDivisibleBy = SmallInteger.prototype.isDivisibleBy =
    BigInteger.prototype.isDivisibleBy;

  function isBasicPrime(v) {
    var n = v.abs();
    if (n.isUnit()) return false;
    if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
    if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
    if (n.lesser(49)) return true;
    // we don't know if it's prime: let the other functions figure it out
  }

  function millerRabinTest(n, a) {
    var nPrev = n.prev(),
      b = nPrev,
      r = 0,
      d,
      t,
      i,
      x;
    while (b.isEven()) (b = b.divide(2)), r++;
    next: for (i = 0; i < a.length; i++) {
      if (n.lesser(a[i])) continue;
      x = bigInt(a[i]).modPow(b, n);
      if (x.isUnit() || x.equals(nPrev)) continue;
      for (d = r - 1; d != 0; d--) {
        x = x.square().mod(n);
        if (x.isUnit()) return false;
        if (x.equals(nPrev)) continue next;
      }
      return false;
    }
    return true;
  }

  // Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2
  BigInteger.prototype.isPrime = function(strict) {
    var isPrime = isBasicPrime(this);
    if (isPrime !== undefined) return isPrime;
    var n = this.abs();
    var bits = n.bitLength();
    if (bits <= 64)
      return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]);
    var logN = Math.log(2) * bits.toJSNumber();
    var t = Math.ceil(strict === true ? 2 * Math.pow(logN, 2) : logN);
    for (var a = [], i = 0; i < t; i++) {
      a.push(bigInt(i + 2));
    }
    return millerRabinTest(n, a);
  };
  NativeBigInt.prototype.isPrime = SmallInteger.prototype.isPrime =
    BigInteger.prototype.isPrime;

  BigInteger.prototype.isProbablePrime = function(iterations) {
    var isPrime = isBasicPrime(this);
    if (isPrime !== undefined) return isPrime;
    var n = this.abs();
    var t = iterations === undefined ? 5 : iterations;
    for (var a = [], i = 0; i < t; i++) {
      a.push(bigInt.randBetween(2, n.minus(2)));
    }
    return millerRabinTest(n, a);
  };
  NativeBigInt.prototype.isProbablePrime = SmallInteger.prototype.isProbablePrime =
    BigInteger.prototype.isProbablePrime;

  BigInteger.prototype.modInv = function(n) {
    var t = bigInt.zero,
      newT = bigInt.one,
      r = parseValue(n),
      newR = this.abs(),
      q,
      lastT,
      lastR;
    while (!newR.isZero()) {
      q = r.divide(newR);
      lastT = t;
      lastR = r;
      t = newT;
      r = newR;
      newT = lastT.subtract(q.multiply(newT));
      newR = lastR.subtract(q.multiply(newR));
    }
    if (!r.isUnit())
      throw new Error(
        this.toString() + ' and ' + n.toString() + ' are not co-prime'
      );
    if (t.compare(0) === -1) {
      t = t.add(n);
    }
    if (this.isNegative()) {
      return t.negate();
    }
    return t;
  };

  NativeBigInt.prototype.modInv = SmallInteger.prototype.modInv =
    BigInteger.prototype.modInv;

  BigInteger.prototype.next = function() {
    var value = this.value;
    if (this.sign) {
      return subtractSmall(value, 1, this.sign);
    }
    return new BigInteger(addSmall(value, 1), this.sign);
  };
  SmallInteger.prototype.next = function() {
    var value = this.value;
    if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
    return new BigInteger(MAX_INT_ARR, false);
  };
  NativeBigInt.prototype.next = function() {
    return new NativeBigInt(this.value + BigInt(1));
  };

  BigInteger.prototype.prev = function() {
    var value = this.value;
    if (this.sign) {
      return new BigInteger(addSmall(value, 1), true);
    }
    return subtractSmall(value, 1, this.sign);
  };
  SmallInteger.prototype.prev = function() {
    var value = this.value;
    if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
    return new BigInteger(MAX_INT_ARR, true);
  };
  NativeBigInt.prototype.prev = function() {
    return new NativeBigInt(this.value - BigInt(1));
  };

  var powersOfTwo = [1];
  while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE)
    powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
  var powers2Length = powersOfTwo.length,
    highestPower2 = powersOfTwo[powers2Length - 1];

  function shift_isSmall(n) {
    return Math.abs(n) <= BASE;
  }

  BigInteger.prototype.shiftLeft = function(v) {
    var n = parseValue(v).toJSNumber();
    if (!shift_isSmall(n)) {
      throw new Error(String(n) + ' is too large for shifting.');
    }
    if (n < 0) return this.shiftRight(-n);
    var result = this;
    if (result.isZero()) return result;
    while (n >= powers2Length) {
      result = result.multiply(highestPower2);
      n -= powers2Length - 1;
    }
    return result.multiply(powersOfTwo[n]);
  };
  NativeBigInt.prototype.shiftLeft = SmallInteger.prototype.shiftLeft =
    BigInteger.prototype.shiftLeft;

  BigInteger.prototype.shiftRight = function(v) {
    var remQuo;
    var n = parseValue(v).toJSNumber();
    if (!shift_isSmall(n)) {
      throw new Error(String(n) + ' is too large for shifting.');
    }
    if (n < 0) return this.shiftLeft(-n);
    var result = this;
    while (n >= powers2Length) {
      if (result.isZero() || (result.isNegative() && result.isUnit()))
        return result;
      remQuo = divModAny(result, highestPower2);
      result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
      n -= powers2Length - 1;
    }
    remQuo = divModAny(result, powersOfTwo[n]);
    return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  };
  NativeBigInt.prototype.shiftRight = SmallInteger.prototype.shiftRight =
    BigInteger.prototype.shiftRight;

  function bitwise(x, y, fn) {
    y = parseValue(y);
    var xSign = x.isNegative(),
      ySign = y.isNegative();
    var xRem = xSign ? x.not() : x,
      yRem = ySign ? y.not() : y;
    var xDigit = 0,
      yDigit = 0;
    var xDivMod = null,
      yDivMod = null;
    var result = [];
    while (!xRem.isZero() || !yRem.isZero()) {
      xDivMod = divModAny(xRem, highestPower2);
      xDigit = xDivMod[1].toJSNumber();
      if (xSign) {
        xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers
      }

      yDivMod = divModAny(yRem, highestPower2);
      yDigit = yDivMod[1].toJSNumber();
      if (ySign) {
        yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers
      }

      xRem = xDivMod[0];
      yRem = yDivMod[0];
      result.push(fn(xDigit, yDigit));
    }
    var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0);
    for (var i = result.length - 1; i >= 0; i -= 1) {
      sum = sum.multiply(highestPower2).add(bigInt(result[i]));
    }
    return sum;
  }

  BigInteger.prototype.not = function() {
    return this.negate().prev();
  };
  NativeBigInt.prototype.not = SmallInteger.prototype.not =
    BigInteger.prototype.not;

  BigInteger.prototype.and = function(n) {
    return bitwise(this, n, function(a, b) {
      return a & b;
    });
  };
  NativeBigInt.prototype.and = SmallInteger.prototype.and =
    BigInteger.prototype.and;

  BigInteger.prototype.or = function(n) {
    return bitwise(this, n, function(a, b) {
      return a | b;
    });
  };
  NativeBigInt.prototype.or = SmallInteger.prototype.or =
    BigInteger.prototype.or;

  BigInteger.prototype.xor = function(n) {
    return bitwise(this, n, function(a, b) {
      return a ^ b;
    });
  };
  NativeBigInt.prototype.xor = SmallInteger.prototype.xor =
    BigInteger.prototype.xor;

  var LOBMASK_I = 1 << 30,
    LOBMASK_BI = ((BASE & -BASE) * (BASE & -BASE)) | LOBMASK_I;
  function roughLOB(n) {
    // get lowestOneBit (rough)
    // SmallInteger: return Min(lowestOneBit(n), 1 << 30)
    // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
    var v = n.value,
      x =
        typeof v === 'number'
          ? v | LOBMASK_I
          : typeof v === 'bigint'
          ? v | BigInt(LOBMASK_I)
          : (v[0] + v[1] * BASE) | LOBMASK_BI;
    return x & -x;
  }

  function integerLogarithm(value, base) {
    if (base.compareTo(value) <= 0) {
      var tmp = integerLogarithm(value, base.square(base));
      var p = tmp.p;
      var e = tmp.e;
      var t = p.multiply(base);
      return t.compareTo(value) <= 0
        ? { p: t, e: e * 2 + 1 }
        : { p: p, e: e * 2 };
    }
    return { p: bigInt(1), e: 0 };
  }

  BigInteger.prototype.bitLength = function() {
    var n = this;
    if (n.compareTo(bigInt(0)) < 0) {
      n = n.negate().subtract(bigInt(1));
    }
    if (n.compareTo(bigInt(0)) === 0) {
      return bigInt(0);
    }
    return bigInt(integerLogarithm(n, bigInt(2)).e).add(bigInt(1));
  };
  NativeBigInt.prototype.bitLength = SmallInteger.prototype.bitLength =
    BigInteger.prototype.bitLength;

  function max(a, b) {
    a = parseValue(a);
    b = parseValue(b);
    return a.greater(b) ? a : b;
  }
  function min(a, b) {
    a = parseValue(a);
    b = parseValue(b);
    return a.lesser(b) ? a : b;
  }
  function gcd(a, b) {
    a = parseValue(a).abs();
    b = parseValue(b).abs();
    if (a.equals(b)) return a;
    if (a.isZero()) return b;
    if (b.isZero()) return a;
    var c = Integer[1],
      d,
      t;
    while (a.isEven() && b.isEven()) {
      d = min(roughLOB(a), roughLOB(b));
      a = a.divide(d);
      b = b.divide(d);
      c = c.multiply(d);
    }
    while (a.isEven()) {
      a = a.divide(roughLOB(a));
    }
    do {
      while (b.isEven()) {
        b = b.divide(roughLOB(b));
      }
      if (a.greater(b)) {
        t = b;
        b = a;
        a = t;
      }
      b = b.subtract(a);
    } while (!b.isZero());
    return c.isUnit() ? a : a.multiply(c);
  }
  function lcm(a, b) {
    a = parseValue(a).abs();
    b = parseValue(b).abs();
    return a.divide(gcd(a, b)).multiply(b);
  }
  function randBetween(a, b) {
    a = parseValue(a);
    b = parseValue(b);
    var low = min(a, b),
      high = max(a, b);
    var range = high.subtract(low).add(1);
    if (range.isSmall) return low.add(Math.floor(Math.random() * range));
    var digits = toBase(range, BASE).value;
    var result = [],
      restricted = true;
    for (var i = 0; i < digits.length; i++) {
      var top = restricted ? digits[i] : BASE;
      var digit = truncate(Math.random() * top);
      result.push(digit);
      if (digit < top) restricted = false;
    }
    return low.add(Integer.fromArray(result, BASE, false));
  }

  var parseBase = function(text, base, alphabet, caseSensitive) {
    alphabet = alphabet || DEFAULT_ALPHABET;
    text = String(text);
    if (!caseSensitive) {
      text = text.toLowerCase();
      alphabet = alphabet.toLowerCase();
    }
    var length = text.length;
    var i;
    var absBase = Math.abs(base);
    var alphabetValues = {};
    for (i = 0; i < alphabet.length; i++) {
      alphabetValues[alphabet[i]] = i;
    }
    for (i = 0; i < length; i++) {
      var c = text[i];
      if (c === '-') continue;
      if (c in alphabetValues) {
        if (alphabetValues[c] >= absBase) {
          if (c === '1' && absBase === 1) continue;
          throw new Error(c + ' is not a valid digit in base ' + base + '.');
        }
      }
    }
    base = parseValue(base);
    var digits = [];
    var isNegative = text[0] === '-';
    for (i = isNegative ? 1 : 0; i < text.length; i++) {
      var c = text[i];
      if (c in alphabetValues) digits.push(parseValue(alphabetValues[c]));
      else if (c === '<') {
        var start = i;
        do {
          i++;
        } while (text[i] !== '>' && i < text.length);
        digits.push(parseValue(text.slice(start + 1, i)));
      } else throw new Error(c + ' is not a valid character');
    }
    return parseBaseFromArray(digits, base, isNegative);
  };

  function parseBaseFromArray(digits, base, isNegative) {
    var val = Integer[0],
      pow = Integer[1],
      i;
    for (i = digits.length - 1; i >= 0; i--) {
      val = val.add(digits[i].times(pow));
      pow = pow.times(base);
    }
    return isNegative ? val.negate() : val;
  }

  function stringify(digit, alphabet) {
    alphabet = alphabet || DEFAULT_ALPHABET;
    if (digit < alphabet.length) {
      return alphabet[digit];
    }
    return '<' + digit + '>';
  }

  function toBase(n, base) {
    base = bigInt(base);
    if (base.isZero()) {
      if (n.isZero()) return { value: [0], isNegative: false };
      throw new Error('Cannot convert nonzero numbers to base 0.');
    }
    if (base.equals(-1)) {
      if (n.isZero()) return { value: [0], isNegative: false };
      if (n.isNegative())
        return {
          value: [].concat.apply(
            [],
            Array.apply(null, Array(-n.toJSNumber())).map(
              Array.prototype.valueOf,
              [1, 0]
            )
          ),
          isNegative: false,
        };

      var arr = Array.apply(null, Array(n.toJSNumber() - 1)).map(
        Array.prototype.valueOf,
        [0, 1]
      );
      arr.unshift([1]);
      return {
        value: [].concat.apply([], arr),
        isNegative: false,
      };
    }

    var neg = false;
    if (n.isNegative() && base.isPositive()) {
      neg = true;
      n = n.abs();
    }
    if (base.isUnit()) {
      if (n.isZero()) return { value: [0], isNegative: false };

      return {
        value: Array.apply(null, Array(n.toJSNumber())).map(
          Number.prototype.valueOf,
          1
        ),
        isNegative: neg,
      };
    }
    var out = [];
    var left = n,
      divmod;
    while (left.isNegative() || left.compareAbs(base) >= 0) {
      divmod = left.divmod(base);
      left = divmod.quotient;
      var digit = divmod.remainder;
      if (digit.isNegative()) {
        digit = base.minus(digit).abs();
        left = left.next();
      }
      out.push(digit.toJSNumber());
    }
    out.push(left.toJSNumber());
    return { value: out.reverse(), isNegative: neg };
  }

  function toBaseString(n, base, alphabet) {
    var arr = toBase(n, base);
    return (
      (arr.isNegative ? '-' : '') +
      arr.value
        .map(function(x) {
          return stringify(x, alphabet);
        })
        .join('')
    );
  }

  BigInteger.prototype.toArray = function(radix) {
    return toBase(this, radix);
  };

  SmallInteger.prototype.toArray = function(radix) {
    return toBase(this, radix);
  };

  NativeBigInt.prototype.toArray = function(radix) {
    return toBase(this, radix);
  };

  BigInteger.prototype.toString = function(radix, alphabet) {
    if (radix === undefined) radix = 10;
    if (radix !== 10) return toBaseString(this, radix, alphabet);
    var v = this.value,
      l = v.length,
      str = String(v[--l]),
      zeros = '0000000',
      digit;
    while (--l >= 0) {
      digit = String(v[l]);
      str += zeros.slice(digit.length) + digit;
    }
    var sign = this.sign ? '-' : '';
    return sign + str;
  };

  SmallInteger.prototype.toString = function(radix, alphabet) {
    if (radix === undefined) radix = 10;
    if (radix != 10) return toBaseString(this, radix, alphabet);
    return String(this.value);
  };

  NativeBigInt.prototype.toString = SmallInteger.prototype.toString;

  NativeBigInt.prototype.toJSON = BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function() {
    return this.toString();
  };

  BigInteger.prototype.valueOf = function() {
    return parseInt(this.toString(), 10);
  };
  BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;

  SmallInteger.prototype.valueOf = function() {
    return this.value;
  };
  SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
  NativeBigInt.prototype.valueOf = NativeBigInt.prototype.toJSNumber = function() {
    return parseInt(this.toString(), 10);
  };

  function parseStringValue(v) {
    if (isPrecise(+v)) {
      var x = +v;
      if (x === truncate(x))
        return supportsNativeBigInt
          ? new NativeBigInt(BigInt(x))
          : new SmallInteger(x);
      throw new Error('Invalid integer: ' + v);
    }
    var sign = v[0] === '-';
    if (sign) v = v.slice(1);
    var split = v.split(/e/i);
    if (split.length > 2)
      throw new Error('Invalid integer: ' + split.join('e'));
    if (split.length === 2) {
      var exp = split[1];
      if (exp[0] === '+') exp = exp.slice(1);
      exp = +exp;
      if (exp !== truncate(exp) || !isPrecise(exp))
        throw new Error(
          'Invalid integer: ' + exp + ' is not a valid exponent.'
        );
      var text = split[0];
      var decimalPlace = text.indexOf('.');
      if (decimalPlace >= 0) {
        exp -= text.length - decimalPlace - 1;
        text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
      }
      if (exp < 0)
        throw new Error('Cannot include negative exponent part for integers');
      text += new Array(exp + 1).join('0');
      v = text;
    }
    var isValid = /^([0-9][0-9]*)$/.test(v);
    if (!isValid) throw new Error('Invalid integer: ' + v);
    if (supportsNativeBigInt) {
      return new NativeBigInt(BigInt(sign ? '-' + v : v));
    }
    var r = [],
      max = v.length,
      l = LOG_BASE,
      min = max - l;
    while (max > 0) {
      r.push(+v.slice(min, max));
      min -= l;
      if (min < 0) min = 0;
      max -= l;
    }
    trim(r);
    return new BigInteger(r, sign);
  }

  function parseNumberValue(v) {
    if (supportsNativeBigInt) {
      return new NativeBigInt(BigInt(v));
    }
    if (isPrecise(v)) {
      if (v !== truncate(v)) throw new Error(v + ' is not an integer.');
      return new SmallInteger(v);
    }
    return parseStringValue(v.toString());
  }

  function parseValue(v) {
    if (typeof v === 'number') {
      return parseNumberValue(v);
    }
    if (typeof v === 'string') {
      return parseStringValue(v);
    }
    if (typeof v === 'bigint') {
      return new NativeBigInt(v);
    }
    return v;
  }
  // Pre-define numbers in range [-999,999]
  for (var i = 0; i < 1000; i++) {
    Integer[i] = parseValue(i);
    if (i > 0) Integer[-i] = parseValue(-i);
  }
  // Backwards compatibility
  Integer.one = Integer[1];
  Integer.zero = Integer[0];
  Integer.minusOne = Integer[-1];
  Integer.max = max;
  Integer.min = min;
  Integer.gcd = gcd;
  Integer.lcm = lcm;
  Integer.isInstance = function(x) {
    return (
      x instanceof BigInteger ||
      x instanceof SmallInteger ||
      x instanceof NativeBigInt
    );
  };
  Integer.randBetween = randBetween;

  Integer.fromArray = function(digits, base, isNegative) {
    return parseBaseFromArray(
      digits.map(parseValue),
      parseValue(base || 10),
      isNegative
    );
  };

  return Integer;
})();

// Node.js check
if (typeof module !== 'undefined' && module.hasOwnProperty('exports')) {
  module.exports = bigInt;
}

//amd check
if (typeof define === 'function' && define.amd) {
  define('big-integer', [], function() {
    return bigInt;
  });
}

process.stdin.resume();
process.stdin.setEncoding('utf8');

// your code goes here

var input = ``;

function cacheInput(data) {
  input += data;
}

function main() {
  let lines = input.split('\r\n');
  let n = parseInt(lines[0]);
  for (var i = 1; i <= n; i++) {
    let steps = bigInt(0);
    let input = lines[i].split(' ');
    let n = bigInt(input[0]);
    let k = bigInt(input[1]);
    while (n.value) {
      let mod = bigInt(n).mod(bigInt(k));
      if (mod.value === 0) {
        n = bigInt(n).divide(bigInt(k));
        steps = bigInt(steps).add(1);
      } else {
        n = bigInt(n).minus(bigInt(mod));
        steps = bigInt(steps).add(bigInt(mod));
      }
    }
    console.log(steps.toString());
  }
}

process.stdin.on('data', cacheInput).on('end', main);

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

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

  let [n, k] = d.split(' ').map(BigInt);
  let curr = 0n;

  while (n > 0n) {
    let reminder = n % k;
    if (reminder === 0n) {
      n /= k;
      curr++;
    }
    else {
      curr += reminder;
      n -= reminder;
    }
  }

  ans += `${curr.toString()}\n`;

  c++;
});

rl.on('close', () => {
  console.log(ans);
});


Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular $$$n$$$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $$$OX$$$-axis and at least one of its edges is parallel to the $$$OY$$$-axis at the same time.Recall that a regular $$$n$$$-sided polygon is a convex polygon with $$$n$$$ vertices such that all the edges and angles are equal.Now he is shopping: the market has $$$t$$$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of polygons in the market.  Each of the next $$$t$$$ lines contains a single integer $$$n_i$$$ ($$$3 \le n_i \le 10^9$$$): it means that the $$$i$$$-th polygon is a regular $$$n_i$$$-sided polygon.

For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).

NoteIn the example, there are $$$4$$$ polygons in the market. It's easy to see that an equilateral triangle (a regular $$$3$$$-sided polygon) is not beautiful, a square (a regular $$$4$$$-sided polygon) is beautiful and a regular $$$12$$$-sided polygon (is shown below) is beautiful as well.

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStd) => {
  inputString += inputStd;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let t = +readLine();
  while (t--) {
    let num = +readLine();
    if (num % 4 === 0) console.log("YES");
    else console.log("NO");
  }
}

//↓見なくていいよ！ここから--------------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  myerr("-----start-----");
  var start = new Date();
  Main();
  var end = new Date() - start;
  myerr("----- end -----");
  myerr("time : " + (end) + "ms");
  myerr("memory : " + Math.round(process.memoryUsage().heapTotal / 1024) + "KB");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  var returnObj = {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
  };
  return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割  7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}

//↑見なくていいよ！ここまで--------------------------------------------------
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var N = nextInt();
		if(N % 4 == 0){
			output[i] = "YES";
		}else{
			output[i] = "NO";
		}
	}
	myout(myconv(output,9));
}

var data = '';
process.stdin.setEncoding('utf8');

process.stdin.on('data', (buffer) =>
{
    data += buffer;
});

process.stdin.on('end', () =>
{
    var x = data.split(/\s+/);
    for (var i = 1; i <= x[0]; i++)
        console.log(x[i] % 4 ? 'NO' : 'YES');
})

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let input = '';
process.stdin.on('data', (line) => {
    input += line;
});
process.stdin.on('end', () => {
    const lines = input.split('\n');
    let tests = parseInt(lines.shift(), 10);
    while (tests--) {
        const n = parseInt(lines.shift(), 10);
        console.log(n % 4 ? 'NO' : 'YES');
    }
});
// input helper
function lineToNumArray(line) {
    return line.split(' ').map(element => parseInt(element, 10));
}

const processData = (lines) => {
  let acc = 0
  const n = +lines[acc]
  acc++
  for (let i=0; i<n; i++) {
    const a = +lines[acc]
    acc++
    console.log(a %4 === 0 ? 'YES' : 'NO')
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

let _inputLines;
let _lineNumber = 0;
let inputReader = _inputReader ();

function _main() {

	_inputLines = _inputData.trim().split('\n').map((string) => {
		return string.trim();
	});;
	let t=inputReader.readNumber();
	while(t--){
	  let n=inputReader.readNumber();
	  if(n%4==0){
	    console.log('YES');

	  }else{
	    console.log("NO");
	  }
	}

}

var _inputData = '';
function cacheInput(data) {
	_inputData += data;
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', cacheInput).on('end', _main);

function _inputReader () {
	function readNumber(){
		return Number(_inputLines[_lineNumber++]);
	}

	return {
		readNumber,
	}
}

function solve() {
   var T = Number(read());
   for (var t = 0; t < T; t++) {
      var n = BigInt(read());
      write(n % 4n === 0n ? 'YES' : 'NO');
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

'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(){
    let n = readline();
    while(n--){
        let x = readline();
        x%4 === 0 ? console.log('YES') : console.log('NO');
    }
}

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() {
    let t = readLine()
    t = parseInt(t)
    let side = 0

    for (let i = 0; i < t; i++) {
        side = readLine()
        side = parseInt(side)

        if (side % 4 === 0) console.log("YES")
        else console.log("NO")
    }
}

"use strict";

let t = readline();

for(let i=0;i<t;i++){

	let n = readline();

	if((n-4)%4===0)print("YES");
	else print("NO");
}

var t = parseInt(readline());

while(t--){
  var n = parseInt(readline());

  if (n % 4 == 0)
    print("YES");
  else
    print("NO");
}

function solve() {
   var T = Number(read());
   for (var t = 0; t < T; t++) {
      var n = Number(read());
      write(n % 4 ? 'NO' : 'YES');
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

var Tc = parseInt(readline());
while (Tc-- > 0) {
	var n = parseInt(readline());
	print(n % 4 == 0 ? 'YES' : 'NO');
}

var t = Number(readline());
while (t--) {
    var n = Number(readline());
    print(n % 4 == 0 ? 'YES' : 'NO');
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStd) => {
  inputString += inputStd;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let t = +readLine();
  while (t--) {
    let num = +readLine();
    if (num % 2 === 0 && num >= 4) console.log("YES");
    else console.log("NO");
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStd) => {
  inputString += inputStd;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let t = +readLine();
  while (t--) {
    let num = +readLine();
    if (num % 2 === 0) console.log("YES");
    else console.log("NO");
  }
}

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() {
    let t = readLine()
    t = parseInt(t)
    let side = 0
    for (let i = 0; i < t; i++) {
        side = readLine()
        side = parseInt(side)
        if (side <= 3 || side === 5 || side === 6) console.log("NO")
        else console.log("YES")
    }

}


You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Print the number of local extrema in the given array.

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
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';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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 N = nextInt();
  var list = nextIntArray();
  var output = 0;
  for(var i = 1; i < N - 1; i++){
    if(list[i] < list[i + 1] && list[i] < list[i - 1] || list[i] > list[i + 1] && list[i] > list[i - 1]){
      output++;
    }
  }
  myout(output);
}

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 arr = d.split(' ').map(Number);
  let ans = 0;

  for (let i = 1; i < arr.length - 1; i++) {
    if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
      ans++;
    }

    if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
      ans++;
    }
  }

  console.log(ans);

  c++;
});

function readArr()
{
    return readline().split(' ').map(function(x){return +x;})
}

n=+readline()
a=readArr()
kol=0
for (i=1;i<n-1;i++)
{
    if (a[i]>a[i-1]&&a[i]>a[i+1])kol++;
    if (a[i]<a[i-1]&&a[i]<a[i+1])kol++;
}
print(kol)

function readPrefixedIntList() {
  var n = parseInt(readline());
  var r = readline().split(' ').map(function (f) {
    return parseInt(f, 10);
  });
  return r;
}

  var numbers = readPrefixedIntList();

  var n = 0;
  for (var i = 1; i < numbers.length - 1; i++) {
    var sign1 = Math.sign(numbers[i - 1] - numbers[i]);
    var sign2 = Math.sign(numbers[i + 1] - numbers[i]);
    if (sign1 !== 0 && sign1 === sign2) n++;
  }

  print(n.toString());

function toInt(x) {
	return parseInt(x);
}

var n = toInt(readline());
var ar = readline().split(' ').map(toInt);
var res = 0;

for (var i = 1; i < ar.length-1; i++) {
	if ((ar[i] < ar[i-1] && ar[i] < ar[i+1]) || (ar[i] > ar[i-1] && ar[i] > ar[i+1])) {
		res++;
	}
}

print(res);

var n = Number(readline());
var a = readline().split(" ").map(function(each) {return Number(each);});

var count = 0;
for (var i = 1; i < a.length - 1; ++i) {
    if ((a[i] > a[i - 1] && a[i] > a[i + 1]) || (a[i] < a[i - 1] && a[i] < a[i + 1]))
        ++count;
}

print(count);

n = readline();
str = readline().split(' ').map(Number);

var count = 0;

for (var i = 1; i < str.length - 1; i++) {
	if ((str[i] > str[i - 1] && str[i] > str[i + 1]) || (str[i] < str[i - 1] && str[i] < str[i + 1])) {
		count++;
	};
};

print(count);

var n = Number(readline());
var a = readline().split(" ").map(function(each) {return Number(each);});

var count = 0;
for (var i = 1; i < a.length - 1; ++i) {
    if ((a[i] > a[i - 1] && a[i] > a[i + 1]) || (a[i] < a[i - 1] && a[i] < a[i - 1]))
        ++count;
}

print(count);


Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Print a single integer – the answer to the problem.

NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.

;(function () {
    var n = readline().split(' ').map(Number),
        k = n[1],
        m = {},
        q = null,
        s = 0,
        i = 0;
    n = n[0];

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

    q = Object.keys(m).sort(function (a, b) {
        return m[b] - m[a];
    });

    while (k > 0) {
        s += Math.pow(Math.min(m[q[i]], k), 2);
        k -= m[q[i++]];
    }

    print(s);

}.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 data = tokenizeIntegers(readline()),
		n = data[0], k = data[1],
		cards = trim(readline()),
		counts = {};
	for (var i = 65; i <= 90; ++i) {
		counts[String.fromCharCode(i)] = 0;
	}
	for (var i = 0; i < n; ++i) {
		counts[cards.charAt(i)] += 1;
	}
	var pairs = [];
	for (var i = 65; i <= 90; ++i) {
		var ch = String.fromCharCode(i);
		pairs.push([ch, counts[ch]]);
	}
	pairs.sort(function (a, b) {
		return a[1] - b[1];
	});
	var score = 0, pos = 25;
	while (k != 0 && pos != -1) {
		var count = Math.min(k, pairs[pos][1]);
		k -= count;
		score += count*count;
		--pos;
	}
	print(score);
}

main();

var input = readline().replace(/\s{1,}/gi, ' ').split(/\s/).map(Number),
			n = input[0], k = input[1],
			aCards = readline().split(''),
			prevCard = '', count = 0, rs = [], sum = 0, ts = {};

		aCards.forEach(function(a, i)	{
			var tsCount = ts[a] == undefined ? 0 : ts[a];

			ts[a] = tsCount + 1;
		});

		Object.keys(ts).forEach(function(c, i)	{ rs.push(ts[c]); });

		rs.sort(function(a, b){return b-a});

		for(var i=0; i<rs.length; i++)	{
			var c = rs[i];

			count += c;

			if(count >= k)	{
				count -= c;

				sum += Math.pow(k - count, 2);

				break;
			} else	{
				sum += Math.pow(c, 2);
			}
		}

		print(sum);

/**
 * Created by artyom.
 */

var a = readline().split(' ').map(function (v) {
        return parseInt(v);
    }),
    n = a[0], k = a[1], i, sum = 0, t, size = 26,
    s = readline(),
    g = Array.apply(null, new Array(size)).map(Number.prototype.valueOf, 0);

for (i = 0; i < n; i++) {
    g[s.charCodeAt(i) - 65]++;
}
g.sort(function (a, b) {
    return a - b;
});
for (i = size - 1; i >= 0 && k > 0; i--) {
    t = Math.min(g[i], k);
    sum += t * t;
    k -= t;
}

print(sum);

function gao()
{
    var numbers = readline().split(' ');
	var str = readline();
	var n = numbers[0];
	var k = numbers[1];
    var alphbet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var amounts = new Array(26);
	for( var i = 0; i< alphbet.length; i++)
	{
	    var c = alphbet.charAt(i);
	    var reg = new RegExp( c, 'g' );
	    var result = str.match(reg);
		if(result != null )
		{
	        amounts[i] = result.length;
		}
	}

	amounts.sort(sortNum);

	var ans = 0;
	for( var i=0;i<amounts.length;i++)
	{
	    if( amounts[i] < k )
		{
		    ans+=amounts[i]*amounts[i];
			k-=amounts[i];
		}
		else
		{
		    ans+=k*k;
			k=0;
		}
		if(k==0){break;}
	}

	print(ans);
}

function sortNum(a, b)
{
    return b-a;
}

gao();

var input = readline().replace(/\s{1,}/gi, ' ').split(/\s/).map(Number),
			n = input[0], k = input[1],
			aCards = readline().split('').sort(),
			prevCard = '', count = 1, rs = [], sum = 0;

		for(var i=0; i<k; i++)	{
			var c = aCards[i];

			if(i === 0)	prevCard = c;
			else	{
				if(prevCard != c)	{
					prevCard = c;

					rs.push(count);

					count = 1;
				} else	{
					count++;
				}
			}
		}

		rs.push(count);
		rs.forEach(function(a)	{ sum += Math.pow(a, 2); });

		print(sum);

/**
 * Created by artyom.
 */

var a = readline().split(' ').map(function (v) {
        return parseInt(v);
    }),
    n = a[0], k = a[1], i, sum = 0, t, size = 26,
    s = readline(),
    g = Array.apply(null, new Array(size)).map(Number.prototype.valueOf, 0);

for (i = 0; i < n; i++) {
    g[s.charCodeAt(i) - 65]++;
}
g.sort();
for (i = size - 1; i >= 0 && k > 0; i--) {
    t = Math.min(g[i], k);
    sum += t * t;
    k -= t;
}

print(sum);