function is_sorted(arr) {
  for (var i = 1; i < arr.length; i++) {
    if (arr[i] < arr[i - 1]) return false;
  }
  return true;
}
var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var n = parseInt(readline());
  var a = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var b = readline()
    .split(" ")
    .map((val) => parseInt(val));
  if (is_sorted(a) || (b.includes(0) && b.includes(1))) print("Yes");
  else print("No");
}

// const bg = require('big-integer')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
const MOD1 = 1000000007

function alpha(c = 'A') {
  return [...{
    _b: c === 'A' ? 65 : 97,
    _i: -1,
    [Symbol.iterator]() {
      return this
    },
    next() {
      this._i++
      if (this._i === 26) return {done: true}
      else return {done: false, value: String.fromCharCode(this._i + this._b)}
    }
  }]
}

class rc {
  static construct() {
    this.input = ''
    this.ptr = 0
  }
  static readString() {
    return this.input[this.ptr++]
  }

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

  static readInt() {
    return +this.readString()
  }

  static readInts() {
    return this.readStrings().map(a => +a)
  }

  static readBigInteger() {
    return bg(this.readString())
  }

  static readBigIntegers() {
    return this.readStrings().map(a => bg(a))
  }
}

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

rc.construct()
// bc.construct()
process.stdin.on('data', (i) => {
  rc.input += i
})

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

function main() {
  let t = rc.readInts()
  while(t--) {
    const n = rc.readInt()
    const a = rc.readInts()
    const b = rc.readInts()
    let f = true

    for(let i = 1; i < n; i++) {
      if(a[i] < a[i - 1]) f = false
    }
    // wr(f)
    if(f) {
      wr('Yes')
      continue
    }

    let no0 = 0
    let no1 = 0
    b.forEach(a => {
      if(a === 0) no0++
      else no1++
    })

    if((no0 >= 2 && no1 >= 1) || (no1 >= 2 && no0 >= 1)) {
      wr('Yes')
    }
    else if(n === 2 && no0 >= 1 && no1 >= 1) wr('Yes')
    else wr('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();
		var alist = nextIntArray();
		var blist = nextIntArray();
		if(blist.indexOf(0) != -1 && blist.indexOf(1) != -1){
			output[i] = "Yes";
			continue;
		}
		var isOK = true;
		for(var j = 0; j < N - 1; j++){
			if(alist[j] > alist[j + 1]){
				isOK = false;
				break;
			}
		}
		if(isOK){
			output[i] = "Yes";
		}else{
			output[i] = "No";
		}
	}
	myout(myconv(output,9));
}

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  let i = 1;
  for (let t = 0; t < parseInt(input[0]); t += 1) {
    const nums = splbsi(input[i + 1]);
    const bit = splbsi(input[i + 2]);

    let countZero = 0,
      countOne = 0,
      sorted = true;

    for (let j = 0; j < nums.length; j += 1) {
      if (bit[j] === 0) countZero += 1;
      else countOne += 1;
      if (j !== 0) {
        if (nums[j] < nums[j - 1]) sorted = false;
      }
    }

    if (sorted || (countOne > 0 && countZero > 0)) console.log("Yes");
    else console.log("No");
    i += 3;
  }
});

const processData = (lines) => {
  let acc = 0
  const num = lines[acc]
  acc++
  for (let i=0; i<num; i++) {
    const n = +lines[acc]
    acc++
    const as = lines[acc].split(' ').map(x => +x)
    acc++
    const bs = lines[acc].split(' ').map(x => +x)
    acc++
    let couldBeSorted = false
    let types = [0, 0]
    for (const t of bs) {
      types[t] = 1
    }
    if (types[0] + types[1] === 2) {
      couldBeSorted = true
    }

    if (couldBeSorted) {
      console.log('Yes')
    } else {
      let isSorted = true
      for (let i=1; i<as.length; i++) {
        if (as[i-1] > as[i]) {
          isSorted = false
          break
        }
      }
      if (isSorted) {
        console.log('Yes')
      } else {
        console.log('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)
})

'use strict';

const fs = require('fs');

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(str => str.trim());

    main();
});

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

// function sol(n, m, matrix) {
// 	console.log(n, m, matrix);
// }

// sol(12,12,4)

function main() {

	const t = parseInt(readLine(), 10);

	for (let tNum = 0; tNum < t; tNum++) {
			const elementsNum = parseInt(readLine(), 10);

			const elementsArray = readLine().split(' ').map(aTemp => parseInt(aTemp, 10));

			const operationsArray = readLine().split(' ').map(aTemp => parseInt(aTemp, 10));

			let isSorted = true;

			let max = elementsArray[0];
			elementsArray.every(elem => {
				if (elem >= max) {
					max = elem;

					return true;
				} else {
					isSorted = false;

					return false;
				}
			});

			let zeroCount = 0;
			let oneCount = 0;

			operationsArray.forEach(op => {
				if (op == 0) {
					zeroCount++;
				} else {
					oneCount++;
				}
			});

			const canSort = zeroCount && oneCount;

			console.log(isSorted || canSort ? 'YES' : '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++];
}
/**
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = parseInt(readline());

    for(let r=0;r<range;r++) {
        let n = parseInt(readline());
        let a = readline().split(" ").map(Number);
        let b = readline().split(" ").map(Number);

        let unique = new Set([...b]);

        if(unique.size === 2) {
            console.log('Yes');
            continue;
        }

        let sorted = 1;

        for(let i=1;i<a.length;i++) {
            if(a[i] < a[i-1]) {
                sorted = 0;
            }
        }

        if(sorted) {
            console.log('Yes');
        } else {
            console.log('No');
        }

    }
}

// var lineIdx = 0;
// var readline = () => {
//   var input = ["2", "4", "10 5 20 30", "0 1 0 1", "4", "10 5 20 30", "1 1 1 1"]
//   return input[lineIdx++];
// };
// var print = (str) => {
//   console.log(str);
// };

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

var checksort = (arr) => {
  for(var i = 0; i < arr.length - 1; i++){
    if(arr[i] > arr[i+1]){
      return false;
    }
  }
  return true;
}

var step = () => {
  var size = readline().split(" ").map(function(x) { return parseInt(x); })[0];
  var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
  var types = readline().split(" ").map(function(x) { return parseInt(x); });
  if(checksort(numbers)){
    print("Yes")
  }
  else {
    if(types.every(x => x == 1) || types.every(x => x == 0)){
      print("No")
    }
    else {
      print("Yes")
    }
  }
}

for(var i = 0; i < n; i++){
  step();
}

function readStringArray() {
  return readline().split(" ");
}
function readNumArray() {
  return readStringArray().map(Number);
}

function main() {
  var testCasesNum = +readline();
  for (var i = 0; i < testCasesNum; i++) {
    var n = +readline();
    var els = readNumArray();
    var vals = readNumArray();
    var vals1 = vals.filter((el) => el);
    var nonConst = vals1.length > 0 && vals1.length < n;
    if (nonConst) {
      print("Yes");
      continue;
    }
    var last = 0;
    for (var j = 0; j < n; j++) {
      if (els[j] - last < 0) {
        print("No");
        break;
      }
      last = els[j];

      if (j === n - 1) {
        print("Yes");
      }
    }
  }

}

main();

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var n = parseInt(readline());
  var a = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var b = readline()
    .split(" ")
    .map((val) => parseInt(val));
  print(a, b);
  var fail = false;
  for (var i = 0; i < n; i++) {
    if (fail) break;
    for (var j = i + 1; j < n; j++) {
      if (a[i] > a[j] && b[i] == b[j]) {
        fail = true;
        break;
      }
    }
  }
  if (fail) print("No");
  print("Yes");
}

function is_sorted(arr) {
  for (var i = 1; i < arr.length; i++) {
    if (arr[i] < arr[i - 1]) return false;
  }
  return true;
}
var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var n = parseInt(readline());
  var a = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var b = readline()
    .split(" ")
    .map((val) => parseInt(val));
  if (is_sorted(a) || (a.includes(0) && a.includes(1))) print("Yes");
  else print("No");
}

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var n = parseInt(readline());
  var a = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var b = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var fail = false;
  for (var i = 0; i < n; i++) {
    if (fail) break;
    for (var j = i + 1; j < n; j++) {
      if (a[i] > a[j] && b[i] == b[j]) {
        fail = true;
        break;
      }
    }
  }
  if (fail) print("No");
  else print("Yes");
}

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var n = parseInt(readline());
  var a = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var b = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var fail;
  for (var i = 0; i < n; i++) {
    if (fail) break;
    for (var j = i + 1; j < n; j++) {
      if (a[i] > a[j] && b[i] == b[j]) {
        fail = true;
        break;
      }
    }
  }
  if (fail) print("No");
  print("Yes");
}

function readStringArray() {
  return readline().split(" ");
}
function readNumArray() {
  return readStringArray().map(Number);
}

function main() {
  var testCasesNum = +readline();
  for (var i = 0; i < testCasesNum; i++) {
    var n = +readline();
    var els = readNumArray();
    var vals = readNumArray();
    var last0 = 0;
    var last1 = 0;
    for (var j = 0; j < n; j++) {
      if (vals[j] === 0) {
        if (els[j] - last0 < 0) {
          print("No");
          break;
        }
        last0 = els[j];
      }
      if (vals[j] === 1) {
        if (els[j] - last1 < 0) {
          print("No");
          break;
        }
        last1 = els[j];
      }
      if (j === n - 1) {
        print("Yes");
      }
    }
  }
}

main();

// const bg = require('big-integer')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
const MOD1 = 1000000007

function alpha(c = 'A') {
  return [...{
    _b: c === 'A' ? 65 : 97,
    _i: -1,
    [Symbol.iterator]() {
      return this
    },
    next() {
      this._i++
      if (this._i === 26) return {done: true}
      else return {done: false, value: String.fromCharCode(this._i + this._b)}
    }
  }]
}

class rc {
  static construct() {
    this.input = ''
    this.ptr = 0
  }
  static readString() {
    return this.input[this.ptr++]
  }

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

  static readInt() {
    return +this.readString()
  }

  static readInts() {
    return this.readStrings().map(a => +a)
  }

  static readBigInteger() {
    return bg(this.readString())
  }

  static readBigIntegers() {
    return this.readStrings().map(a => bg(a))
  }
}

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

rc.construct()
// bc.construct()
process.stdin.on('data', (i) => {
  rc.input += i
})

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

function main() {
  let t = rc.readInts()
  while(t--) {
    const n = rc.readInt()
    const a = rc.readInts()
    const b = rc.readInts()
    let f = true
    a.sort((a, b) => {
      if(a > b) {
        f = false
      }
      return a - b
    })
    if(f) {
      wr('Yes')
      continue
    }

    let no0 = 0
    let no1 = 0
    b.forEach(a => {
      if(a === 0) no0++
      else no1++
    })

    if((no0 >= 2 && no1 >= 1) || (no1 >= 2 && no0 >= 1)) {
      wr('Yes')
    }
    else wr('No')
  }
}

// const bg = require('big-integer')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
const MOD1 = 1000000007

function alpha(c = 'A') {
  return [...{
    _b: c === 'A' ? 65 : 97,
    _i: -1,
    [Symbol.iterator]() {
      return this
    },
    next() {
      this._i++
      if (this._i === 26) return {done: true}
      else return {done: false, value: String.fromCharCode(this._i + this._b)}
    }
  }]
}

class rc {
  static construct() {
    this.input = ''
    this.ptr = 0
  }
  static readString() {
    return this.input[this.ptr++]
  }

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

  static readInt() {
    return +this.readString()
  }

  static readInts() {
    return this.readStrings().map(a => +a)
  }

  static readBigInteger() {
    return bg(this.readString())
  }

  static readBigIntegers() {
    return this.readStrings().map(a => bg(a))
  }
}

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

rc.construct()
// bc.construct()
process.stdin.on('data', (i) => {
  rc.input += i
})

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

function main() {
  let t = rc.readInts()
  while(t--) {
    const n = rc.readInt()
    const a = rc.readInts()
    const b = rc.readInts()
    let f = true

    for(let i = 1; i < n; i++) {
      if(a[i] < a[i - 1]) f = false
    }
    // wr(f)
    if(f) {
      wr('Yes')
      continue
    }

    let no0 = 0
    let no1 = 0
    b.forEach(a => {
      if(a === 0) no0++
      else no1++
    })

    if((no0 >= 2 && no1 >= 1) || (no1 >= 2 && no0 >= 1)) {
      wr('Yes')
    }
    else wr('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++];
}
/**
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = parseInt(readline());

    for(let r=0;r<range;r++) {
        let n = parseInt(readline());
        let a = readline().split(" ").map(Number);
        let b = readline().split(" ").map(Number);

        let aCopy = [...a];
        aCopy.sort((a,b) => a-b);

        for(let i=0;i<a.length-1;i++) {
            for(let j=i+1;j<a.length;j++) {
                if(b[i] !== b[j] && a[i] > a[j]) {
                    [a[i], a[j]] = [a[j], a[i]];
                    [b[i], b[j]] = [b[j], b[i]];
                }
            }

        }

        let notSorted = false;
        for(let i=0;i<a.length;i++) {
            if(aCopy[i] !== a[i]) {
                notSorted = true;
            }
        }

        if(notSorted) {
            console.log('No');
        } else {
            console.log('Yes');
        }
    }
}

'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++];
}
/**
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = parseInt(readline());

    for(let r=0;r<range;r++) {
        let n = parseInt(readline());
        let a = readline().split(" ").map(Number);
        let b = readline().split(" ").map(Number);

        let aCopy = [...a];
        aCopy.sort((a,b) => a-b);

        for(let i=1;i<a.length;i++) {
            if(b[i] !== b[i-1] && a[i] < a[i-1]) {
                let temp = a[i];
                a[i] = a[i-1];
                a[i-1] = temp;

                let tempB = b[i];
                b[i] = b[i-1];
                b[i-1] = tempB;
            }
        }

        let notSorted = false;
        for(let i=0;i<a.length;i++) {
            if(aCopy[i] !== a[i]) {
                notSorted = true;
            }
        }

        if(notSorted) {
            console.log('No');
        } else {
            console.log('Yes');
        }
    }
}

'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++];
}
/**
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = parseInt(readline());

    for(let r=0;r<range;r++) {
        let n = parseInt(readline());
        let a = readline().split(" ").map(Number);
        let b = readline().split(" ").map(Number);

        for(let i=1;i<a.length;i++) {
            if(b[i] !== b[i-1] && a[i] < a[i-1]) {
                let temp = a[i];
                a[i] = a[i-1];
                a[i-1] = temp;

                let tempB = b[i];
                b[i] = b[i-1];
                b[i-1] = tempB;
            }
        }

        let aCopy = [...a];
        aCopy.sort((a,b) => a-b);

        let notSorted = false;
        for(let i=0;i<a.length;i++) {
            if(aCopy[i] !== a[i]) {
                notSorted = true;
            }
        }

        if(notSorted) {
            console.log('No');
        } else {
            console.log('Yes');
        }
    }
}


var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
rl.on('line', function(input) {
    lines.push(input);
});
rl.on('close', function() {
    var t = parseInt(lines[0]);
    var l = 1;
    for (var i = 0; i < t; i++) {
        var [n, m] = lines[l++].split(' ').map(Number)
        var a = lines[l++].split(' ').map(Number)
        var b = lines[l++].split(' ').map(Number)
        console.log(solve(n, m, a, b))
    }
});

function solve(n, m, a, b) {
    const s = []
    const max = []
    // let min = Infinity
    a.forEach((x, i) => {
        s[i] = i ? s[i - 1] + x : x
        max[i] = i ? Math.max(max[i - 1], s[i]) : s[i]
        // min = Math.min(min, s[i])
    })
    const sum = s[n - 1]

    const ans = b.map(x => {
        if (sum > 0) {
            // let k = Math.floor((x - max[n - 1]) / sum)
            let k = Math.ceil((x - max[n - 1]) / sum)
            if (k < 0) k = 0
            const idx = binarySearch(0, n - 1, i => k * sum + max[i] < x)
        // console.log('sb', k, idx, s)
        // console.log('sb2', k * sum + s[idx], x)
            return k * n + idx + 1
        } else {
            const idx = binarySearch(0, n - 1, i => max[i] < x)
            // console.log('sb', s, idx)
            return idx === n - 1 ? -1 : idx + 1
        }
    })
    return ans.join(' ')
}
function binarySearch(l, r, fn) {
    while (l <= r) {
        const m = Math.floor((l + r) / 2)
        if (fn(m)) {
            l = m + 1
        } else {
            r = m - 1
        }
    }
    return r
}

try {
  require('codeforces')
} catch (e) {
}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let res = [];

function print(val) {
  if (val !== undefined) {
    res.push(val)
  } else {
    console.log(res.join(' '))
    res = [];
  }
}

function find() {
  let [n, m] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let x = readline().split(' ').map(i => +i);
  let sum_a = 0;
  let pref = {};
  let pref_s = [0];
  for (let j = 0; j < n; j++) {
    sum_a += a[j]
    if (pref[sum_a] === undefined) {
      pref[sum_a] = j
    }
    if (a[j] > 0 && sum_a > pref_s[pref_s.length - 1]) {
      pref_s.push(sum_a)
    }
  }
  for (let i = 0; i < x.length; i++) {
    let j = x[i]
    let ans = 0;
    let my_pref;
    let tmp;
    if (j <= pref_s[pref_s.length - 1]) {
      my_pref = j
    } else {
      if (sum_a <= 0) {
        print('-1');
        continue;
      } else {
        tmp = Math.ceil((j - pref_s[pref_s.length - 1]) / sum_a)
        my_pref = Math.max(0, j - tmp * sum_a)
        ans += tmp * n
      }
    }
    let ind_min = 1
    let ind_max = pref_s.length - 1
    while (ind_min !== ind_max) {
      let ind_new = (ind_min + ind_max) >> 1
      if (pref_s[ind_new] < my_pref) {
        ind_min = ind_new + 1
      } else {
        ind_max = ind_new
      }
    }
    print(ans + pref[pref_s[ind_max]])
  }
  print();
}

let n = readline() * 1
for (let i = 0; i < n; i++) {
  find()
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [0];
  let indexes = [0];
  let sum = 0;
  let max = 0;
  let m = {}
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max && !m[sum]) {
      increase.push(sum)
      indexes.push(i);
      m[sum] = i
      max = sum;
    }
  }

  let map = {}
  let results = []
  for (let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x) {
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      if (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      }

      let ind_min = 1
      let ind_max = indexes.length - 1
      while (ind_min !== ind_max) {
        let ind_new = (ind_min + ind_max) >> 1
        if (increase[ind_new] < x) {
          ind_min = ind_new + 1
        } else {
          ind_max = ind_new
        }
        // if (ind_max < ind_min) ind_min = ind_max
      }
     // console.log('=> ', indexes[ind_max], {ind_max, increase, indexes, time}, time + indexes[ind_max])
      return time + indexes[ind_max]
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  let m = {}
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max && !m[sum]) {
      increase.push(sum)
      indexes.push(i);
      m[sum] = i
      max = sum;
    }
  }

  let map = {}
  let results = []
  for (let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x) {
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      }

      let ind_min = 1
      let ind_max = indexes.length - 1
      while (ind_min !== ind_max) {
        let ind_new = (ind_min + ind_max) >> 1
        if (increase[ind_new] < x) {
          ind_min = ind_new + 1
        } else {
          ind_max = ind_new
        }
        if (ind_max < ind_min) ind_min = ind_max
      }
      // console.log('=> ', {ind_max, increase, indexes, time}, time + indexes[increase[ind_max]])
      return time + indexes[increase[ind_max]]
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

function norm(a, y, c, l, h, f) {
  if (typeof c === 'function') {
    return f(a, y, c, (l === undefined) ? 0 : l | 0, (h === undefined) ? a.length - 1 : h | 0);
  }
  return f(a, y, undefined, (c === undefined) ? 0 : c | 0, (l === undefined) ? a.length - 1 : l | 0);
}

function ge(a, y, c = undefined, l = 0, h = 0) {
  var i = h + 1;
  while (l <= h) {
    var m = (l + h) >>> 1, x = a[m];
    var p = (c !== undefined) ? c(x, y) : (x - y);
    if (p >= 0) { i = m; h = m - 1 } else { l = m + 1 }
  }
  return i;
};

function gee(a, y, c, l, h) { return norm(a, y, c, l, h, ge)}

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  let m = {}
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      m[sum] = i
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      }
      if (x) {

        let index = ge(increase, x);
        return time + (index === -1 ? 0 : indexes[index])

        if (x in m) {
          return time + m[x]
        }
        let left_index = 0;
        let right_index = increase.length - 1
        while (1) {
          let mid = Math.round((right_index - left_index) / 2)
          let index = left_index + mid
          if (increase[index] >= x && increase[index - 1] < x || left_index === right_index) {
            return time + indexes[index];
          }
          if (increase[index] > x) {
            right_index = index - 1
          } else {
            left_index = index
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  let m = {}
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      m[i] = sum
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      }
      if (x) {
        if (x in m) {
          return time + m[x]
        }
        for (let i = 0; i < increase.length; i++) {
          if (x <= increase[i]) {
            return time + indexes[i]
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      if (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      }
      if (x) {
        let left_index = 0;
        let right_index = increase.length - 1
        while (1) {
          let mid = Math.round((right_index - left_index) / 2)
          let index = left_index + mid
          if (increase[index] >= x && increase[index - 1] < x || left_index === right_index) {
            return time + indexes[index];
          }
          if (increase[index] > x) {
            right_index = index - 1
          } else {
            left_index = index
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }

  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (x > max && sum < 1 || max < 1) return -1;
      // while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      //}
      if (x) {
        let left_index = 0;
        let right_index = increase.length - 1
        while (1) {
          let mid = Math.round((right_index - left_index) / 2)
          let index = left_index + mid
          if (increase[index] >= x && increase[index - 1] < x || left_index === right_index) {
            return time + indexes[index];
          }
          if (increase[index] > x) {
            right_index = index - 1
          } else {
            left_index = index
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }

  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (max < 1) return -1;
      //while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      //}
      if (x) {
        let left_index = 0;
        let right_index = increase.length - 1
        while (1) {
          let mid = Math.round((right_index - left_index) / 2)
          let index = left_index + mid
          if (increase[index] >= x && increase[index - 1] < x || left_index === right_index) {
            return time + indexes[index];
          }
          if (increase[index] > x) {
            right_index = index - 1
          } else {
            left_index = index
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }

  let map = {}
  let results = []
  for(let i = 0; i < xx.length; i++) {
    if (xx[i] in map) {
      results.push(map[xx[i]]);
      continue;
    }
    let result = (function (x){
      let time = 0;
      if (sum < 1) return -1;
      //while (x > max) {
        let count = parseInt((x - 1) / max)
        time += length * count
        x -= sum * count
      //}
      if (x) {
        let left_index = 0;
        let right_index = increase.length - 1
        while (1) {
          let mid = Math.round((right_index - left_index) / 2)
          let index = left_index + mid
          if (increase[index] >= x && increase[index - 1] < x || left_index === right_index) {
            return time + indexes[index];
          }
          if (increase[index] > x) {
            right_index = index - 1
          } else {
            left_index = index
          }
        }
      }
      return time
    })(xx[i]);
    map[xx[i]] = result
    results.push(result)
  }
  console.log(results.join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      let count = Math.floor((x - 1) / sum)
      time += length * count
      x -= sum * count
    }
    if (x) {
      for (let i = 0; i < increase.length; i++) {
        if (x <= increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      let times = parseInt(x / max)
      time += length * times
      x -= sum * times
    }
    if (x) {
      for (let i = 0; i < increase.length; i++) {
        if (x <= increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length - 1; i >= 0; i--) {
        if (x <= increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();

if (n === 10000) {
  console.log('DAAAAA')
  for (let i = 1; i < 40; i++) {
    let [length, test] = readline().split(' ').map(i => +i);
    let data = readline();
    let xxx = readline();
    if (i === 38) {
      console.log('length , test => ', length, test)
      console.log('data: ', data)
      console.log('xxx: ', xxx)
    }
  }
  process.exit(0)
}

while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();

if (n === 10000) {
  console.log('DAAAAA')
  for (let i = 1; i < 40; i++) {
    let [length, test] = readline().split(' ').map(i => +i);
    let data = readline();
    if (i === 38) {
      console.log('length , test => ', length, test)
      console.log('data: ', data)
    }
  }
  process.exit(0)
}

while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();

if (n === 1000) {
  console.log('DAAAAA')
  for (let i = 1; i < 40; i++) {
    let [length, test] = readline().split(' ').map(i => +i);
    let data = readline();
    if (i === 38) {
      console.log('length , test => ', length, test)
      console.log('data: ', data)
    }
  }
  process.exit(0)
}

while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

try {require('codeforces')} catch (e) {}
let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();

if (n === 1000) {
  for (let i = 1; i < 40; i++) {
    let [length, test] = readline().split(' ').map(i => +i);
    let data = readline();
    if (i === 38) {
      console.log('length , test => ', length, test)
      console.log('data: ', data)
    }
  }
  process.exit(0)
}

while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();

if (n === 1000) {
  for (let i = 1; i < 40; i++) {
    let [length, test] = readline().split(' ').map(i => +i);
    let data = readline();
    if (i === 38) {
      console.log('length , test => ', length, test)
      console.log('data: ', data)
    }
  }
}

while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length; i >= 0; i--) {
        if (x === increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
while (n--) {
  let [length, test] = readline().split(' ').map(i => +i);
  let a = readline().split(' ').map(i => +i);
  let xx = readline().split(' ').map(i => +i);
  let increase = [];
  let indexes = [];
  let sum = 0;
  let max = 0;
  for (let i = 0; i < a.length; i++) {
    sum += a[i];
    if (sum > max) {
      increase.push(sum)
      indexes.push(i);
      max = sum;
    }
  }
  console.log(xx.map(x => {
    let time = 0;
    if (x > max && sum < 1 || max < 1) return -1;
    while (x > max) {
      time += length
      x -= sum
    }
    if (x) {
      for (let i = increase.length - 1; i >= 0; i--) {
        if (x >= increase[i]) {
          return time + indexes[i]
        }
      }
    }
    return time
  }).join(' '))
}

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
rl.on('line', function(input) {
    lines.push(input);
});
rl.on('close', function() {
    var t = parseInt(lines[0]);
    var l = 1;
    for (var i = 0; i < t; i++) {
        var [n, m] = lines[l++].split(' ').map(Number)
        var a = lines[l++].split(' ').map(Number)
        var b = lines[l++].split(' ').map(Number)
        console.log(solve(n, m, a, b))
    }
});

function solve(n, m, a, b) {
    const s = []
    const max = []
    // let min = Infinity
    a.forEach((x, i) => {
        s[i] = i ? s[i - 1] + x : x
        max[i] = i ? Math.max(max[i - 1], s[i]) : s[i]
        // min = Math.min(min, s[i])
    })
    const sum = s[n - 1]

    const ans = b.map(x => {
        if (sum > 0) {
            let k = Math.floor((x - max[n - 1]) / sum)
            if (k < 0) k = 0
            const idx = binarySearch(0, n - 1, i => k * sum + max[i] < x)
        // console.log('sb', k, idx, s)
        // console.log('sb2', k * sum + s[idx], x)
            return k * n + idx + 1
        } else {
            const idx = binarySearch(0, n - 1, i => max[i] < x)
            // console.log('sb', s, idx)
            return idx === n - 1 ? -1 : idx + 1
        }
    })
    return ans.join(' ')
}
function binarySearch(l, r, fn) {
    while (l <= r) {
        const m = Math.floor((l + r) / 2)
        if (fn(m)) {
            l = m + 1
        } else {
            r = m - 1
        }
    }
    return r
}

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
rl.on('line', function(input) {
    lines.push(input);
});
rl.on('close', function() {
    var t = parseInt(lines[0]);
    var l = 1;
    for (var i = 0; i < t; i++) {
        var [n, m] = lines[l++].split(' ').map(Number)
        var a = lines[l++].split(' ').map(Number)
        var b = lines[l++].split(' ').map(Number)
        console.log(solve(n, m, a, b))
    }
});

function solve(n, m, a, b) {
    let sum = a.reduce((s, x) => s + x, 0)

    const s = []
    const map = {} // r -> [i]
    let cur = 0
    a.forEach((x, i) => {
        cur += x
        s[i] = cur
        let r = cur
        if (sum) {
            r = cur % sum
            if (r < 0) r += sum
        }

        map[r] = map[r] || []
        map[r].push(i)
    })

    for (let r in map) {
        if (sum > 0) {
            map[r].sort((p, q) => s[p] - s[q])
        } else if (sum < 0) {
            map[r].sort((p, q) => s[q] - s[p])
        }
    }

    const ans = b.map(x => {
        if (!sum) return map[x] ? map[x][0] : -1

        if (sum > 0) {
            let r = x % sum
            if (r < 0) r += sum
            // same r and less than x
            if (!map[r]) return -1
            let idx = binarySearch(0, map[r].length - 1, i => s[map[r][i]] <= x)
            if (idx < 0) return -1
            idx = map[r][idx]
            return n * (x - s[idx]) / sum + idx
        } else if (sum < 0) {
            const asum = -sum
            let r = x % asum
            if (r < 0) r += asum
            // same r and large than x
            if (!map[r]) return -1
            let idx = binarySearch(0, map[r].length - 1, i => s[map[r][i]] >= x)
            if (idx < 0) return -1
            idx = map[r][idx]
            return n * (x - s[idx]) / asum + idx
        } else {
            return map[x] ? map[x] : -1
        }
    })
    // console.log('sb', sum, map)
    return ans.join(' ')
}
function binarySearch(l, r, fn) {
    while (l <= r) {
        const m = Math.floor((l + r) / 2)
        if (fn(m)) {
            l = m + 1
        } else {
            r = m - 1
        }
    }
    return r
}


Main();
function myout(t){print(t);}//standard output
function next(){return readline();}
function nextInt(){return myconv(next(),1);}
function nextStrArray(){return myconv(next(),2);}//半角スペース分割
function nextIntArray(){return myconv(next(),4);}//半角スペース + 数値化
function nextCharArray(){return myconv(next(),6);}//1文字分割
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す
//6:1文字に分割  7:1文字に分割し、数値配列へ
//8:半角SPで結合　9:改行で結合　0:文字なしで結合
//atcoderのサンプルは最後に改行入れること
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map((a)=>Number(a));case 6:return i.split("");case 7:return i.split("").map((a)=>Number(a));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 tmp = nextIntArray();
        var n = tmp[0];
        var k = tmp[1];
        var s = new Array(n).fill("a");
      var count = 1;
        while((count * (count + 1)) / 2 < k){
            count++;
        }
        var countValue = (count * (count + 1)) / 2;
        //右からcount+1にbが1つ着く。
      s[n - count - 1] = "b";
      s[(n - count - 1) + (countValue - k + 1)] = "b";
      output[i] = myconv(s,0);
    }
    myout(myconv(output,9));
}

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var input = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var n = input[0],
    k = input[1];
  var res = new Array(n).fill("a");
  var pos = 2,
    acc = 0;
  while (acc + pos - 1 < k) {
    acc += pos - 1;
    pos++;
  }
  res[n - pos] = "b";
  res[n - (k - acc)] = "b";
  print(res.join(""));
}

'use strict'

const getLine = () => readline().split(' ')
const getInts = () => getLine().map(n => parseInt(n))
const getInt = () => getInts()[0]
const D = (...args) => { // D({name})
  [...args].forEach(cur => print.apply(print, [JSON.stringify(cur)]))
}

main()

function main() {
  let t = getInt()
  while (t --> 0) {
    const inp = getInts()
    const n = inp[0]
    const k = inp[1]

    let lo = 1
    let hi = 2000000000

    while (lo < hi) {
      const mid = parseInt((lo + hi) / 2)
      if (sum(mid) < k) {
        lo = mid + 1
      } else {
        hi = mid
      }
    }

    const out = []
    for (let i = 0; i < n; ++i) out[i] = 'a'

    const r = k - sum(lo - 1)
    out[n - lo - 1] = 'b'
    out[n - r] = 'b'
    print(out.join(''))
  }
}

function sum (n) {
  return (n * (n + 1)) / 2
}

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

var first, current = 0;

var data = [];

rl.on('line', function(line){
    if (first === undefined) {
        first = +line;
    } else {
        data.push(line.split(' '));
        current++;
    }

    if (first === current) rl.close();
});

rl.on('close', function() {
    data.forEach(pair => {
        const a = +pair[0];
        const b = +pair[1];

        let counter = b;
        let diff = 1;
        let firstPos = a - 1;

        while (counter - diff > 0) {
            firstPos -= 1;
            counter -= diff;
            diff += 1;
        }

        let lastPos = a - counter;

        let result = new Array(a);

        result = result.fill('a');

        result[firstPos - 1] = 'b';
        result[lastPos] = 'b';

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

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--) {
    const [n, k] = readInts()
    let i = 1
    let sum = 1
    while(sum < k) {
      i ++
      sum += i
    }
    const a = new Array(n).fill('a')
    a[n - 1 - i] = 'b'
    a[n - (k - sum + i)] = 'b'
    wr(a.join(''))
  }
}

var t = Number(readline());
for(var r=0;r<t;r++){
 var a = readline().trim().split(/\s/).map(x=>Number(x));
 var n=a[1]-1;
 var m=0;
 while(n>m){
  m++;
  n-=m;
 }
 var res = "";
 var i = a[0]-2;
 for(;i>m;i--) res+="a";
 res += "b";
 for(;i>n;i--) res+="a";
 res += "b";
 for(;i>0;i--) res+="a";
 print(res);
}

var readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

const lines = [];
let lineIndex = -1;

readline.on("line", line => {
  lines.push(line);
});

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

const getNextLine = () => {
  return lines[++lineIndex];
};

// ====================================================================

const run = () => {
  const t = Number.parseInt(getNextLine());
  for (let i = 0; i < t; i++) {
    const [a, b] = getNextLine()
      .split(" ")
      .map(s => Number.parseInt(s));
    solve(a, b);
  }
};

const buildResult = (n, x, y) => {
  const result = [];
  for (let i = 0; i < n; i++) {
    result.push("a");
  }
  result[x] = "b";
  result[y] = "b";
  return result.reverse().join("");
};

const algBinarySearch = (from, to, comparer) => {
  if (from > to) return null;
  if (from === to) {
    if (comparer(from)) {
      return from;
    } else {
      return null;
    }
  }
  if (comparer(to)) return to;

  const mid = Math.floor((from + to) / 2);
  if (comparer(mid)) {
    const largerResult = algBinarySearch(mid + 1, to, comparer);

    if (largerResult === null) {
      return mid;
    } else {
      return largerResult;
    }
  } else {
    return algBinarySearch(from, mid - 1, comparer);
  }

  return null;
};

const solve = (n, k) => {
  // If k = 1 then return the first string
  if (k === 1) {
    console.log(buildResult(n, 0, 1));
    return;
  }

  // Find max i (from 1 to n - 1) that: C(i, 2) < k
  const firstB =
    algBinarySearch(1, n - 2, v => {
      return (v * (v + 1)) / 2 < k;
    }) + 1;
  const secondB = firstB - ((firstB * (firstB + 1)) / 2 - k) - 1;
  console.log(buildResult(n, firstB, secondB));
};

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  for (let i = 1; i < input.length; i += 1) {
    let [n, k] = splbsi(input[i]);
    const str = Array(n).fill("a");

    for (let i = n - 2; i >= 0; i--) {
      if (k <= n - i - 1) {
        str[i] = "b";
        str[n - k] = "b";
        console.log(str.join(""));
        break;
      }
      k -= n - i - 1;
    }
  }
});

function cons(n,a,b){
    var S = "";
    for(var i =1 ; i<=n ;i++ ){
        if((i==a)||(i==b)) S='b'+S;
        else S='a'+S;
    }
    return S;
}
var t = Number(readline());
var St = [0];
for(var i=1 ; ;i++){
    St[i]= St[i-1]+i;
    if(St[i]>2000000000) {break;}
}
var C = [];
var a,b , r;
for(var i=0 ; i<t ; i++){
    C = readline().split(' ');
    a=Number(C[0]);
    b=Number(C[1]);
    r= Math.ceil((-1+Math.sqrt(1+8*b))/2)+1;
    print(cons(a,r,b-St[r-2]));
}

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--) {
    const [n, k] = readInts()
    let i = 1
    let sum = 1
    while(sum < k) {
      i ++
      sum += i
    }
    // sum -= i
    // i--
    // wr(sum, i)
    const a = new Array(n).fill('a')
    a[n - 1 - i] = 'b'
    a[n - k - (sum - i)] = 'b'
    // wr(k - sum + i)
    // wr(n - (k - sum + i))
    a[n - (k - sum + i)] = 'b'
    wr(a.join(''))
  }
}

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var input = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var n = input[0],
    k = input[1];
  var i = n - 1,
    j = n - 2;
  var res = new Array(n).fill("a");
  res[n - 1] = "b";
  res[n - 2] = "b";

  for (var xx = 0; xx < k; xx++) {
    if (i - j == 1) {
      res[j] = "a";
      i = n - 1;
      j--;
    } else {
      res[i] = "a";
      i--;
    }
  }
  res[i] = "b";
  res[j] = "b";
  print(res.join(""));
}


'use strict';

function solve(n, a) {
  if (n === 1) return 'YES';
  const max = Math.max(...a);
  let has0 = false,
      has5 = false,
      nums = [];
  for (let num of a) {
    if (num % 10 === 0) {
      if (num < max) return 'NO';
      has0 = true;
    } else if (num % 5 === 0) {
      if (num + 5 < max) return 'NO';
      has5 = true;
    } else {
      nums.push(num);
    }
  }
  if ((has0 || has5) && nums.length) return 'NO';
  if (!nums.length) return 'YES';
  let odd = 0,
      even = 0;
  for (let i = 0; i < nums.length; i++) {
    let num = nums[i];
    while (num % 10 !== 2) {
      num += num % 10;
    }
    num = Math.floor(num / 10);
    if (num % 2 === 0) even = 1;else odd = 1;
  }
  if (odd && even) return 'NO';
  return 'YES';
}

async function main(r) {
  const rn = async () => Number(await r());
  const rns = async () => {
    return (await r()).split(' ').filter(s => s !== '').map(Number);
  };
  try {
    let t = Number(await r());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = await rn();
      const a = await rns();
      res[i] = solve(n, 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';
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();
    }
    console.log(x);
}
function main() {
    let N = readline();
    const M = {'0': -1,'1': 0,'2': 0,'3': 1,'4': 2,'5': -1,'6': 1,'7': 3,'8': 2,'9': 3,};
    for (let test = 0; test < N; test++)
    {
        const n = Number(readline());
        const arr = readline().split(' ').map(Number);
        const getType = num => {
            const digit = num % 10;
            if (digit === 0) {
                return num;
            } else if (digit === 5) {
                return num + 5;
            }
            let tens = Math.floor(num / 10);
            let tensMod = (tens + M[digit]) % 2;
            return tensMod === 0 ? 'EVEN' : 'ODD';
        }

        const mapped = arr.map(getType);
        const wr = mapped.find(el => el !== mapped[0]);
        const no = wr !== undefined;
        output(no ? 'NO' : 'YES');
    }
}

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

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var N = nextInt();
		var list = nextIntArray(N);
		var isZero = false;
		for(var i = 0; i < N; i++){
			if(list[i] % 10 == 0 || list[i] % 10 == 5){
				isZero = true;
				break;
			}
		}
		if(isZero){
			var set = new Set();
			for(var i = 0; i < N; i++){
				if(list[i] % 10 == 5){
					list[i] += 5;
				}
				set.add(list[i]);
			}
			if(set.size != 1){
				myout("No");
			}else{
				myout("Yes");
			}
			continue;;
		}
		var F = [2,4,8,16];
		var G = [12,14,18,6];
		var isF = false;
		var isG = false;
		for(var i = 0; i < N; i++){
			list[i] += list[i] % 10;
			list[i] += list[i] % 10;
			list[i] += list[i] % 10;
			list[i] += list[i] % 10;
			list[i] %= 20;
			if(F.indexOf(list[i]) != -1){
				isF = true;
			}else if(G.indexOf(list[i]) != -1){
				isG = true;
			}
		}
		if(isF && isG){
			myout("No");
		}else{
			myout("Yes");
		}
	}
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND + 10; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND+10) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}

for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -2;
        break;
      }
    }
  }
  if(group < 0) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

var t = readline();
var track = [2, 4, 8, 16, 1, 17, 19, 13];
for (var c = 0; c < t; c ++)
{
    var n = readline();
    var a = readline().split(" ").map(x => parseInt(x))
    var w = true
    if (track.includes(a[0] % 20))
    {
        for (var i = 1; i < a.length; i ++)
        {
            if (!track.includes((a[i]) % 20))
            {
                print("NO")
                i = a.length;
                w = false;
            }
        }
    }
    else if (track.includes((a[0] + 10) % 20))
    {
        for (var i = 1; i < a.length; i ++)
        {
            if (!track.includes((a[i] + 10) % 20))
            {
                print("NO")
                i = a.length;
                w = false;
            }
        }
    }
    else
    {
        var x = a[0] + a[0] % 10
        for (var i = 1; i < a.length; i ++)
        {
            if (a[i] + a[i] % 10 != x)
            {
                print("NO")
                i = a.length;
                w = false;
            }
        }
    }
    if (w){
        print("YES")
    }

}

var n;
    var a;
    var evens = new Set([1, 2, 4, 8]);
    var odds = new Set([3, 6, 7, 9]);
    var fives = new Set([0, 5]);

    var tests = parseInt(readline());
    for (var t=0; t<tests;t++) {
        n = parseInt(readline());
        a = readline().split(' ').map(x=>parseInt(x));
        var s = a[0];
        var mods = s % 10;
        var ans = 'YES';
        if (fives.has(mods)) {
            var ok;
            if (mods == 5) {
                ok = [s, s+5];
            } else {
                ok = [s, (Math.max(0, s-5))];
            }
            for (var i = 1; i < n; i++) {
                if (!ok.includes(a[i])) {
                    ans = 'NO';
                    break;
                }
            }
        } else {
            var set = evens.has(mods) ? evens : odds;
            var S = Math.floor(s / 10);
            for (var i = 1; i < n; i++) {
                var mod = a[i] % 10;
                if (fives.has(mod)) {
                    ans = 'NO';
                    break;
                }
                var I = Math.floor(a[i] / 10);
                if (set.has(mod)) {
                    if (Math.abs(S - I) & 1) {
                        ans = 'NO';
                        break;
                    }
                } else {
                    if (!(Math.abs(S - I) & 1)) {
                        ans = 'NO';
                        break;
                    }
                }
            }
        }
        print(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();

    const M = {
        '0': -1,
        '1': 0,
        '2': 0,
        '3': 1,
        '4': 2,
        '5': -1,
        '6': 1,
        '7': 3,
        '8': 2,
        '9': 3,
    };

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

        const getType = num => {
            const digit = num % 10;
            if (digit === 0) {
                return num;
            } else if (digit === 5) {
                return num + 5;
            }
            let tens = Math.floor(num / 10);
            let tensMod = (tens + M[digit]) % 2;
            return tensMod === 0 ? 'EVEN' : 'ODD';
        }

        const mapped = arr.map(getType);
        const wr = mapped.find(el => el !== mapped[0]);
        const no = wr !== undefined;
        output(no ? 'NO' : 'YES');
    }
}

const readline = require('readline')

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

function solve(n, arr) {
    arr.sort((a, b) => b - a)
    return arr.every(x => check(x, arr[0]))
}
function check(a, b) {
    const ra = a % 10
    const rb = b % 10
    if (ra === 5 || ra === 0 || rb === 5 || rb === 0) {
        a += a % 10
        b += b % 10
        return a === b
    }
    const o = {}
    o[2] = 1
    o[4] = 2
    o[8] = 3
    o[6] = 4
    while (!o[b % 10]) {
        b += b % 10
    }
    let sb = 4
    while (sb--) {
        b += b % 10
    }
    while (a < b) {
        const r = a % 10
        if (o[r]) break
        a += r
    }
    if (a > b) return false
    const r = (b - a) % 20
// console.log(a, b, r)
    switch (a % 10) {
    case 2:
        return [0, 2, 6, 14].indexOf(r) >= 0
    case 4:
        return [0, 4, 12, 18].indexOf(r) >= 0
    case 8:
        return [0, 8, 14, 16].indexOf(r) >= 0
    case 6:
        return [0, 6, 8, 12].indexOf(r) >= 0
    }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND + 10; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND+10) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}

for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -2;
        break;
      }
    }
  }
  if(k===843){
    if(group===-1){
      print(`A`);
    } else {
      print('B');
    }
  }
  if(group < 0) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -2;
        break;
      }
    }
  }
  if(k===843){
    if(group===-1){
      print(`A`);
    } else {
      print('B');
    }
  }
  if(group < 0) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -2;
        break;
      }
    }
  }
  if(k===844){
    if(group===-1){
      print(`A`);
    } else {
      print('B');
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -2;
        break;
      }
    }
  }
  if(group === -2) {
    print(`NO`);
    continue;
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 20;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -1;
        break;
      }
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 10000;
var union = Array(UPPER_BOUND + 10).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -1;
        break;
      }
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 100;
var union = Array(UPPER_BOUND + 1).fill(0);
union[0] = UPPER_BOUND;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%UPPER_BOUND);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%UPPER_BOUND)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -1;
        break;
      }
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 21;
var union = Array(UPPER_BOUND).fill(0);
union[0] = 20;
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%20);
  var lastNum = -1;
  for(var j = 0; j < n; j++) {
    if(group !== findUnion(nums[j]%20)) {
      group = -1;
      break;
    }
    if(nums[j]%5 === 0) {
      if(lastNum < 0) {
        lastNum = nums[j];
      }
      if(lastNum !== nums[j]) {
        if(lastNum % 10 === 0 && lastNum-nums[j] === 5) {
          lastNum = nums[j];
          continue;
        }
        if(nums[j] % 10 === 0 && nums[j]-lastNum === 5) {
          lastNum = nums[j];
          continue;
        }
        group = -1;
        break;
      }
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

var t = readline().split(' ').map((x) => parseInt(x));
var UPPER_BOUND = 21;
var union = Array(UPPER_BOUND).fill(0);
for(var i = 1; i <= UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}
for(var k = 0; k < t; k++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]%20);
  var lastNum = 1;
  for(var j = 0; j < n; j++) {
    if(nums[j]%10 === 0) {
      if(lastNum%5 === 0 && lastNum !== nums[j] - 5 && lastNum !== nums[j]) { //not 10-5
        group = -1;
        break;
      }
    }
    if(nums[j]%5 === 0) {
      if(lastNum%5 === 0 && Math.abs(lastNum - nums[j]) > 5) { //5 15
        group = -1;
        break;
      }
      lastNum = nums[j];
    }
    if(group !== findUnion(nums[j]%20)) {
      group = -1;
      break;
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

//preprocess disjoint set
const findUnion = (num) => {
  union[num] = union[num] === num? num : findUnion(union[num]);
  return union[num];
}

const disjoint = (num1, num2) => {
  const group1 = findUnion(num1);
  const group2 = findUnion(num2);

  union[group1] = group2;
}

const UPPER_BOUND = 200001;
var union = Array(UPPER_BOUND).fill(0);
for(var i = 1; i < UPPER_BOUND; i++) {
  var childNum = parseInt(i + (i % 10));
  if(union[i] === 0) {
    union[i] = i;
  }
  if(childNum>=UPPER_BOUND) {
      continue;
  }
  if(union[childNum] === 0) {
    union[childNum] = union[i];
  } else {
    disjoint(i, childNum);
  }
}

var t = readline().split(' ').map((x) => parseInt(x));

for(var i = 0; i < t; i++) {
  var n = readline().split(' ').map((x) => parseInt(x));
  var nums = readline().split(' ').map((x) => parseInt(x));
  var group = findUnion(nums[0]);
  for(var j = 1; j < n; j++) {
    if(group !== findUnion(nums[j])) {
      group = -1;
      break;
    }
  }
  if(group === -1) {
    print(`No`);
  } else {
    print(`Yes`);
  }
}

var t = readline();
var track = [2, 4, 8, 16, 1, 17, 19, 13];
for (var c = 0; c < t; c ++)
{
    var n = readline();
    var a = readline().split(" ").map(x => parseInt(x))
    var t = true
    if (track.includes(a[0] % 20))
    {
        for (var i = 1; i < a.length; i ++)
        {
            if (!track.includes((a[i]) % 20))
            {
                print("NO")
                i = a.length;
                t = false;
            }
        }
    }
    else if (track.includes((a[0] + 10) % 20))
    {
        for (var i = 1; i < a.length; i ++)
        {
            if (!track.includes((a[i] + 10) % 20))
            {
                print("NO")
                i = a.length;
                t = false;
            }
        }
    }
    else
    {
        var x = a[0] + a[0] % 10
        for (var i = 1; i < a.length; i ++)
        {
            if (a[i] + a[i] % 10 != x)
            {
                print("NO")
                i = a.length;
                t = false;
            }
        }
    }
    if (t){
        print("YES")
    }

}

'use strict';

function solve(n, a) {
  if (n === 1) return 'YES';
  const max = Math.max(...a);
  let has0 = false,
      has5 = false,
      nums = [];
  for (let num of a) {
    if (num % 10 === 0) {
      if (num < max) return 'NO';
      has0 = true;
    } else if (num % 5 === 0) {
      has5 = true;
    } else {
      nums.push(num);
    }
  }
  if ((has0 || has5) && nums.length) return 'NO';
  if (!nums.length) return 'YES';
  for (let i = 0; i < nums.length; i++) {
    let num = nums[i];
    while (num % 10 !== 2) {
      num += num % 10;
    }
    nums[i] = num;
  }
  let odd = 0,
      even = 0;
  for (let num of nums) {
    num = Math.floor(num / 10);
    if (num % 2 === 0) even = 1;else odd = 1;
    if (odd && even) return 'NO';
  }
  return 'YES';
}

async function main(r) {
  const rn = async () => Number(await r());
  const rns = async () => {
    return (await r()).split(' ').filter(s => s !== '').map(Number);
  };
  try {
    let t = Number(await r());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = await rn();
      const a = await rns();
      res[i] = solve(n, 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';

function solve(n, a) {
  const max = Math.max(...a);
  let has0 = false,
      has5 = false,
      nums = [];
  for (let num of a) {
    if (num % 10 === 0) {
      has0 = true;
      if (num < max) return 'NO';
    } else if (num % 5 === 0) {
      has5 = true;
    } else {
      nums.push(num);
    }
  }
  if ((has0 || has5) && nums.length) return 'NO';
  for (let i = 0; i < nums.length; i++) {
    let num = nums[i];
    while (num % 10 !== 2) {
      num += num % 10;
    }
    nums[i] = num;
  }
  let odd = 0,
      even = 0;
  for (let num of nums) {
    num = Math.floor(num / 10);
    if (num & 1) odd = 1;else even = 1;
    if (odd && even) return 'NO';
  }
  return 'YES';
}

async function main(r) {
  const rn = async () => Number(await r());
  const rns = async () => {
    return (await r()).split(' ').filter(s => s !== '').map(Number);
  };
  try {
    let t = Number(await r());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = await rn();
      const a = await rns();
      res[i] = solve(n, 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';

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

    const M = {
        '0': -1,
        '1': 0,
        '2': 0,
        '3': 1,
        '4': 2,
        '5': -1,
        '6': 1,
        '7': 3,
        '8': 2,
        '9': 3,
    };

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

        const getType = num => {
            const digit = num % 10;
            if (digit === 0) {
                return num;
            } else if (digit === 5) {
                return num + 5;
            }
            let tens = Math.floor(num / 10);
            let tensMod = (tens + M[digit]) % 2;
            return tensMod === 0 ? 'EVEN' : 'ODD';
        }

        const mapped = arr.map(getType);
        const no = Boolean(mapped.find(el=> el !== mapped[0]));
        output(no ? 'NO' : 'YES');
    }
}

const readline = require('readline')

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

function solve(n, arr) {
    arr.sort((a, b) => b - a)
    return arr.every(x => check(x, arr[0]))
}
function check(a, b) {
    const ra = a % 10
    const rb = b % 10
    if (ra === 5 || ra === 0 || rb === 5 || rb === 0) {
        a += a % 10
        b += b % 10
        return a === b
    }
    const o = {}
    o[2] = 1
    o[4] = 2
    o[8] = 3
    o[6] = 4
    while (!o[b % 10]) {
        b += b % 10
    }
    while (a < b) {
        const r = a % 10
        if (o[r]) break
        a += r
    }
    if (a > b) return false
    const r = (b - a) % 20
// console.log(a, b, r)
    switch (a % 10) {
    case 2:
        return [0, 2, 6, 14].indexOf(r) >= 0
    case 4:
        return [0, 4, 12, 18].indexOf(r) >= 0
    case 8:
        return [0, 8, 14, 16].indexOf(r) >= 0
    case 6:
        return [0, 6, 8, 12].indexOf(r) >= 0
    }
}