"use strict";
var input = '';
var inputResolve;
var inputPromise = new Promise(resolve => inputResolve = resolve);
process.stdin.on('data', c => input += c);
process.stdin.on('end', () => {
    inputResolve();
});
(async () => {
    await inputPromise;
    var inputs = input.split(/\r?\n/);
    var [n, a, b] = inputs[0].split(' ').map(v => +v);
    var h = inputs[1].split(' ').map(v => +v);
    var c = Array(n).fill(0);
    var d = [...h];
    var kq = 1e5;
    var kc = [];
    var tc = 0;
    function bf(i) {
        if (tc >= kq)
            return;
        if (i == n - 1) {
            if (d[n - 2] >= 0 || d[n - 1] >= 0)
                return;
            kq = tc;
            kc = [...c];
            return;
        }
        for (let j = Math.max(0, Math.ceil((d[i - 1] + 1) / b)); j < 1e2; j++) {
            tc += j;
            c[i] = j;
            d[i - 1] -= b * j;
            d[i + 1] -= b * j;
            d[i] -= a * j;
            bf(i + 1);
            tc -= j;
            c[i] = 0;
            d[i - 1] += b * j;
            d[i + 1] += b * j;
            d[i] += a * j;
            if (d[i] - a * j < 0 && i != n - 2)
                break;
            if (i == n - 2 && d[i] - a * j < 0 && d[i + 1] - b * j < 0)
                break;
        }
    }
    bf(1);
    console.log(kq);
    console.log(kc.map((v, i) => Array(v).fill(i + 1).join(' ')).join(' ').trim().replace(/\s+/g, ' '));
})();

if (!this.readline) print = console.log, readline = require('./readline');

print(function(n, a, b) {

	var h = readNums();
	var nHits = 0;
	var nBest = -1;
	var hits = new Int32Array(222);
	var best = new Int32Array(222);

	var ans = [];
	solve(1);
	for (var i = 0; i < nBest; i++) {
		ans.push(best[i] + 1);
	}

	return nBest + '\n' + ans.join(' ');

	function hit(k) {
		h[k] -= a;
		h[k - 1] -= b;
		h[k + 1] -= b;
		hits[nHits++] = k;
	}

	function heal(k) {
		h[k] += a;
		h[k - 1] += b;
		h[k + 1] += b;
		nHits--;
	}

	function solve(k) {
		if (k === n - 1) {
			var t = 0;
			while (h[k] >= 0) {
				++t;
				hit(k - 1);
			}
			if (h[k - 1] < 0) {
				if (nBest === -1 || nHits < nBest) {
					nBest = nHits;
					for (var i = 0; i < nHits; i++) {
						best[i] = hits[i];
					}
				}
			}
			while (t--) {
				heal(k - 1);
			}
			return;
		}
		var t = 0;
		while (h[k - 1] >= 0) {
			++t;
			hit(k);
		}

		while (h[k] >= 0) {
			++t;
			solve(k + 1);
			hit(k);
		}
		solve(k + 1);
		while (t--) {
			heal(k);
		}
	}

}.apply(0, readNums()));

function readNums() {
	return readline().split(' ').map(Number);
}

var input = '';
var inputResolve;
var inputPromise = new Promise(resolve => inputResolve = resolve);
process.stdin.on('data', c => input += c);
process.stdin.on('end', () => {
  inputResolve();
});

(async () => {
  await inputPromise;
  var inputs = input.split(/\r?\n/);
  var [n, a, b] = inputs[0].split(' ').map(v => +v);
  var h = inputs[1].split(' ').map(v => +v);
  var c = Array(n).fill(0);
  var d = [...h];
  var kq = 1e5;
  var kc = [];
  var tc = 0;

  function bf(i) {
    if (tc >= kq) return;
    if (i == n - 1) {
      if (d[n - 2] >= 0 || d[n - 1] >= 0) return;
      kq = tc;
      kc = [...c];
      return;
    }
    for (let j = Math.max(0, Math.ceil((d[i - 1] + 1) / b)); j < 1e2; j++) {
      tc += j;
      c[i] = j;
      d[i - 1] -= b * j;
      d[i + 1] -= b * j;
      d[i] -= a * j;
      bf(i + 1);
      tc -= j;
      c[i] = 0;
      d[i - 1] += b * j;
      d[i + 1] += b * j;
      d[i] += a * j;
      if (d[i] - a * j < 0 && i != n - 2) break;
      if (i == n - 2 && d[i] - a * j < 0 && d[i + 1] - b * j < 0) break;
    }
  }

  bf(1);
  console.log(kq);
  console.log(kc.map((v, i) => Array(v).fill(i + 1).join(' ')).join(' ').trim().replace(/\s+/g, ' '))
})();

if (!this.readline) print = console.log, readline = require('./readline');

print(function(n, a, b) {

	var h = readNums();
	var ans = [];

	h = h.map(function(v) {
		return v + 1;
	});
	while (true) {
		var i = maxP(n, a, b, h);
		if (i > -1) {
			ans.push(i + 1);
		} else {
			break;
		}
	}

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

}.apply(0, readNums()));

function maxP(n, a, b, h) {
	var max = -1;
	var maxIndex = 0;
	var f = true;
	for (var i = 0; i < n; i++) {
		if (h[i] > 0) {
			f = false;
			break;
		}
	}
	if (f) {
		return -1;
	}

	for (var i = 1; i < n - 1; i++) {
		var p = 0;
		if (h[i] >= a) {
			p += a;
		} else {
			p += h[i];
		}
		if (h[i - 1] >= b) {
			p += b;
		} else {
			p += h[i - 1];
		}
		if (h[i + 1] >= b) {
			p += b;
		} else {
			p += h[i + 1];
		}
		if (p > max) {
			max = p;
			maxIndex = i;
		}
	}
	if (h[maxIndex] >= a) {
		h[maxIndex] -= a;
	} else {
		h[maxIndex] = 0;
	}
	if (h[maxIndex - 1] >= b) {
		h[maxIndex - 1] -= b;
	} else {
		h[maxIndex - 1] = 0;
	}
	if (h[maxIndex + 1] >= b) {
		h[maxIndex + 1] -= b;
	} else {
		h[maxIndex + 1] = 0;
	}

	return maxIndex;
}

function readNums() {
	return readline().split(' ').map(Number);
}

var s = readline().split(' ');
var n = parseInt(s[0]);
var a = parseInt(s[1]);
var b = parseInt(s[2]);

var h = readline().split(' ').map(function(v){
	return parseInt(v);
});

var ret = [];
for(var i=0; i<n; i++){
	ret.push(0);
}

for(var i=1; i<n-1;i++){
	if( h[i-1]>=0 ){
		var ni = Math.ceil( (h[i-1]+1)/b );
		ret[i] += ni;
		h[i] -= ni*a;
		h[i+1] -= ni*b;
	}
}
var last = n-1;
if(h[last]>=0){
	var ni = Math.ceil( (h[last]+1)/b );
	ret[last-1] += ni;
	h[last-1] -= ni*a;
	h[last] -= ni*b;
}
if(h[last-1]>=0){
	var ni = Math.ceil( (h[last-1]+1)/a );
	ret[last-1] += ni;
	h[last-1] -= ni*a;
	h[last] -= ni*b;
}

s = [];
for(var i = 0; i<n; i++){
	if( ret[i] > 0){
		for(var j=0; j<ret[i]; j++){
			s.push(i+1);
		}
	}
}

print(s.length);
print(s.join(' '));

if (!this.readline) print = console.log, readline = require('./readline');

print(function(n, a, b) {

	var h = readNums();
	var ans = [];

	h = h.map(function(v) {
		return v + 1;
	});
	while (true) {
		var i = maxP(n, a, b, h);
		print(h);
		// print(i)
		if (i > -1) {
			ans.push(i + 1);
		} else {
			break;
		}
	}

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

}.apply(0, readNums()));

function maxP(n, a, b, h) {
	var max = -1;
	var maxIndex = 0;
	var f = true;
	for (var i = 0; i < n; i++) {
		if (h[i] > 0) {
			f = false;
			break;
		}
	}
	if (f) {
		return -1;
	}

	for (var i = 1; i < n - 1; i++) {
		var p = 0;
		if (h[i] >= a) {
			p += a;
		} else {
			p += h[i];
		}
		// if (i > 0) {
		if (h[i - 1] >= b) {
			p += b;
		} else {
			p += h[i - 1];
		}
		// }
		// if (i < n - 1) {
		if (h[i + 1] >= b) {
			p += b;
		} else {
			p += h[i + 1];
		}
		// }
		if (p > max) {
			max = p;
			maxIndex = i;
		}
	}
	if (h[maxIndex] >= a) {
		h[maxIndex] -= a;
	} else {
		h[maxIndex] = 0;
	}
	// if (maxIndex > 0) {
	if (h[maxIndex - 1] >= b) {
		h[maxIndex - 1] -= b;
	} else {
		h[maxIndex - 1] = 0;
	}
	// }
	// if (maxIndex < n - 1) {
	if (h[maxIndex + 1] >= b) {
		h[maxIndex + 1] -= b;
	} else {
		h[maxIndex + 1] = 0;
	}
	// }

	return maxIndex;
}

function readNums() {
	return readline().split(' ').map(Number);
}

print(function(n, a, b) {
	var h = readline().split(' ').map(Number).map(function(v) {
		return v + 1;
	});

	var ans = [];
	while (true) {
		var max = -1;
		var curr = -1;

		if (h.every(function(v) {
			return v === 0;
		})) break;

		for (var i = 1; i < n - 1; i++) {
			var x = 0;
			x += Math.min(h[i - 1], b);
			x += Math.min(h[i], a);
			x += Math.min(h[i + 1], b);
			if (x > max) {
				max = x;
				curr = i;
			}
		}

		ans.push(curr+1);
		h[curr - 1] = Math.max(0, h[curr - 1] - b);
		h[curr] = Math.max(0, h[curr] - a);
		h[curr + 1] = Math.max(0, h[curr + 1] - b);
	}

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

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

print(function(n, a, b) {
	var h = readline().split(' ').map(Number).map(function(v) {
		return v + 1;
	});

	var ans = [];
	while (true) {
		var max = -1;
		var curr = -1;

		if (h.every(function(v) {
			return v === 0;
		})) break;

		for (var i = 1; i < n - 1; i++) {
			var x = 0;
			x += Math.min(h[i - 1], b);
			x += Math.min(h[i], a);
			x += Math.min(h[i + 1], b);
			if (x > max) {
				max = x;
				curr = i;
			}
		}

		ans.push(curr);
		h[curr - 1] = Math.max(0, h[curr - 1] - b);
		h[curr] = Math.max(0, h[curr] - a);
		h[curr + 1] = Math.max(0, h[curr + 1] - b);
	}

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

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


'use strict';

var fs = require('fs');

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

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

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        d = rns(n);
  let res = [];
  const cache = Array.from({
    length: n
  }, () => new Map());
  const path = [];
  const dfs = (i, p) => {
    if (i === n) {
      res = [...path];
      return 1;
    }
    if (cache[i].has(p)) return cache[i].get(p);
    path.push(p + d[i]);
    let ans = dfs(i + 1, p + d[i]);
    path.pop();
    if (ans > 1) return ans;
    path.push(p + d[i]);
    if (d[i] && p >= d[i]) ans += dfs(i + 1, p - d[i]);
    path.pop();
    cache[i].set(p, ans);
    return ans;
  };
  let cnt = dfs(0, 0);
  if (cnt === 1) return res.join(' ');else return -1;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rn();
    return res;
  };
  const re = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rn() - 1;
    return res;
  };
  const rbs = n => {
    let res = new Array(n);
    for (let i = 0; i < n; i++) res[i] = rb();
    return res;
  };
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  const [rn, rb, rns, re, rbs] = convertInput(r);
  try {
    let t = rn();
    let res = new Array(t);
    let limit = 100;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

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

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

let currentLine = 0;
let inputString = "";

process.stdin.on("data", (raw_data) => {
  inputString += raw_data;
});

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

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

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

function readArrayInt() {
  return inputString[currentLine++].split(" ").map(Number);
}

function readArray() {
  return inputString[currentLine++].split(" ");
}

function solve() {
  const T = readInt();

  for (let i = 0; i < T; ++i) {
    const n = readInt();
    const drr = readArrayInt();
    const arr = [];

    let check = false;
    for (let i = 0; i < n; i++) {
      const e = drr[i];
      if (i === 0) {
        arr.push(e);
      } else {
        if (arr[arr.length - 1] - e < 0) {
          arr.push(arr[arr.length - 1] + e);
        } else {
          arr.push(arr[arr.length - 1] - e);
        }

        // console.log(arr[arr.length - 1], e, arr[arr.length - 1] - e, "---");
        if (arr[arr.length - 1] - arr[arr.length - 2] < e) {
          console.log(-1);
          check = true;
          break;
        }
      }
    }

    if (!check) {
      let ans = "";
      arr.map((e) => {
        ans += e + " ";
      });
      console.log(ans);
    }
  }
}

function main() {
  solve();
}

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    const a = [];
    a.push(d[0])
    for (let i = 1; i < n; i++) {

        let k = a.length - 1

        let op = ((a[k] - d[i]) < 0) ? (a[k] + d[i]) : (a[k] - d[i])
        a.push(op)

        k = a.length - 1
        if (a[k] - a[k - 1] < d[i]) return -1

    }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

const arr = []
readline.on('line', data => {
    arr.push(data.split(' ').map(i => parseInt(i)))
})

readline.on('close', () => {
    main()
} )

function main(){
    let [n] = arr

    let line = 2
    for(let i = 0; i < n; i++){
        let current = arr[line], result = -1
        line += 2

        let newarr = [0]
        for(let i = 0; i < current.length; i++){
            if(current[i] && newarr[newarr.length-1] - current[i] >= 0){
                newarr = -1
                break
            }else{
                newarr.push(current[i] + newarr[newarr.length -1])
            }
        }

        if(newarr === -1){
            console.log(newarr)
        }else {
            let s = ""
            for(let i of newarr.slice(1)) s += i + " "
            console.log(s)
        }

    }
}

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const mainLength = readline();
  for (let l = 0; l < mainLength; l++) {

    var n = readline();
    var d = readline().split(" ").map(x => parseInt(x));
    var a = [];
    var checked = false;
    var out

    for (let i = 0; i < n; i++) {
      if (i == 0)
        a[i] = (d[i])
      else {
        var i_one = a[i - 1];
        var x = d[i] + a[i - 1];
        // var y = d[i] - a[i - 1];
        var y = Math.abs(d[i] - a[i - 1]);
        var cond1 = (Math.abs(x - i_one))
        var cond2 = (Math.abs(y - i_one))
        if (
          cond1 == cond2
          && (x != y)
        ) {
          // if ((x < 0 && y < 0) && (x != y)) {
          checked = true;
          break;
        } else if (cond1 == d[i]) {
          a[i] = x
        } else {
          a[i] = y
        }
      }
    }
    if (checked)
      out = '-1' + '\n'
    else
      out = a.join(' ') + '\n'
    process.stdout.write(out);
    // console.log(out);

  }

}
// function foo(x) {
//   process.stdout.write("hello: ");   // without auto '\n' (newline)
//   console.log(x);  // with auto '\n' (newline)
// }

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {

        a[i] = d[i] + a[i - 1]
        if( a[i] <= 0 ) a[i - 1] - d[i]

        if (
            (d[i] + a[i - 1]) > 0
            && (a[i - 1] - d[i]) > 0
            && (d[i] + a[i - 1]) != (a[i - 1] - d[i])
        ) return -1

        i++
    }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {

        a[i] = d[i] + a[i - 1]
        if( a[i] <= 0 ) a[i - 1] - d[i]

        if ( (d[i] + a[i - 1]) != (a[i - 1] - d[i]) ) return -1

        i++
    }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {

        a[i] = d[i] + a[i - 1]
        if( a[i] <= 0 ) a[i - 1] - d[i]

        if (d[i] + a[i - 1] > 0 != a[i - 1] - d[i] ) return -1

        i++
    }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    let a = new Array(n).fill(0)
    a[0] = d[0]
    // a[1] = d[0] + d[1]

    let i = 1
    while (i < n) {
        if (d[i] < d[i - 1] && i > 1) return -1
        else a[i] = d[i] + a[i - 1]
        i++
    }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    // first array
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {
        if (d[i] < d[i - 1]) return -1
        else a[i] = d[i] + a[i - 1]
        i++
    }

    // second array
    // let b = new Array(n).fill(0)
    // b[0] = d[0]
    // b[1] = d[0] + d[1]

    // i = 2
    // while (i < n) {
    //     if(d[i] < d[i - 1]) b[i] = b[i] = Math.abs(d[i] - b[i - 1])
    //     else b[i] = d[i] + b[i - 1]
    //     i++
    // }

    return a.join(' ')
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    // first array
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {
        a[i] = d[i] + a[i - 1]
        i++
    }

    // second array
    let b = new Array(n).fill(0)
    b[0] = d[0]
    b[1] = d[0] + d[1]

    i = 2
    while (i < n) {
        if(d[i] < d[i - 1]) b[i] = b[i] = Math.abs(d[i] - b[i - 1])
        else b[i] = d[i] + b[i - 1]
        i++
    }

    return (a.join(' ') === b.join(' ')) ? a.join(' ') : -1
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    // first array
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {
        a[i] = d[i] + a[i - 1]
        i++
    }

    // second array
    let b = new Array(n).fill(0)
    b[0] = d[0]
    b[1] = d[0] + d[1]

    i = 2
    while (i < n) {
        if (d[i] >= d[i - 1]) b[i] = d[i] + b[i - 1]
        else b[i] = b[i] = Math.abs(d[i] - b[i - 1])
        i++
    }

    return (a.join(' ') === b.join(' ')) ? a.join(' ') : -1
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

let inputString = "";
let currentLine = 0;

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

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

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Common Template Ends //

const getSolution = (n, d) => {
    // first array
    let a = new Array(n).fill(0)
    a[0] = d[0]
    a[1] = d[0] + d[1]

    let i = 2
    while (i < n) {
        a[i] = d[i] + a[i - 1]
        i++
    }

    // second array
    let b = new Array(n).fill(0)
    b[0] = d[0]
    b[1] = d[0] + d[1]

    i = 2
    while (i < n) {
        if (d[i] >= d[i - 1]) b[i] = d[i] + b[i - 1]
        else b[i] = b[i] = Math.abs(d[i] - b[i - 1])
        i++
    }

    return (a.join(' ') === b.join(' ')) ? a.join(' ') : '-1'
}

// Driver Function Starts //
function main() {

    let testcases = parseInt(readline())
    while (testcases--) {

        let n = parseInt(readline())
        let d = readline().split(' ').map(Number)
        console.log(getSolution(n, d))
    }

}
// Driver Function Ends //

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

const arr = []
readline.on('line', data => {
    arr.push(data.split(' ').map(i => parseInt(i)))
})

readline.on('close', () => {
    main()
} )

function main(){
    let [n] = arr

    let line = 2
    for(let i = 0; i < n; i++){
        let current = arr[line], result = -1
        line += 2

        let newarr = [0]
        for(let i = 0; i < current.length; i++){
            if(current[i] !== 0 && newarr[newarr.length-1] - current[i] >= 0){
                newarr = -1
                break
            }else{
                newarr.push(current[i] + newarr[newarr.length -1])
            }
        }

        if(newarr === -1){
            console.log(newarr)
        }else {
            let s = ""
            for(let i of newarr) s += i + " "
            console.log(s)
        }

    }
}

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

const arr = []
readline.on('line', data => {
    arr.push(data.split(' ').map(i => parseInt(i)))
})

readline.on('close', () => {
    main()
} )

function main(){
    let [n] = arr

    let line = 2
    for(let i = 0; i < n; i++){
        let current = arr[line], result = -1
        line += 2

        let newarr = [0]
        for(let i = 0; i < current.length; i++){
            if(current[i] !== 0 && newarr[newarr.length-1] - current[i] >= 0){
                newarr = -1
                break
            }else{
                newarr.push(current[i] + newarr[newarr.length -1])
            }
        }
        console.log(
        newarr === -1 ? -1 : newarr.slice(1))

    }
}

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const x = readline();
  for (let i = 0; i < x; i++) {
    var n = readline();
    var drr = readline().split(" ").map(x => parseInt(x));
    var arr = [];
    var checked = false;
    var out

    for (let j = 0; j < n; j++) {

      var a = arr[j - 1] + drr[j];
      var b = arr[j - 1] - drr[j];
      if ((a > 0 && b > 0) && (a != b)) {
        checked = true;
        break;
      }

      if (j == 0)
        arr.push(drr[j])
      else
        arr.push(drr[j] + arr[j - 1])
    }
    if (checked)
      out = '-1' + '\n'
    else
      out = arr.join(' ') + '\n'
    process.stdout.write(out);
    // console.log(out);

  }

}
// function foo(x) {
//   process.stdout.write("hello: ");   // without auto '\n' (newline)
//   console.log(x);  // with auto '\n' (newline)
// }


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

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

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

function main() {
	var stampNum = parseInt(readline());

	var cities = [];

	var count = { augment: function(key) {
		if (this[key] === undefined) {
			this[key] = 1;
			cities.push(key);
		}
		else {
			this[key] += 1;
		}
	}};

	var link = { augment: function(key, value) {
		if (this[key] === undefined) {
			this[key] = [value];
		}
		else {
			this[key].push(value);
		}
	}};

	for (var i = 0; i < stampNum; ++i) {
		var pair = tokenizeIntegers(readline());
		count.augment(pair[0]);
		count.augment(pair[1]);
		link.augment(pair[0], pair[1]);
		link.augment(pair[1], pair[0]);
	}

	for (var i = 0; i < cities.length; ++i) {
		if (count[cities[i]] == 1) {
			var previous = cities[i];
			var path = [previous];
			var current = link[previous];
			while (true) {
				path.push(current);
				if (count[current] == 1) {
					print(path.join(" "));
					return;
				}
				if (link[current][0] != previous) {
					previous = current;
					current = link[current][0];
				}
				else {
					previous = current;
					current = link[current][1];
				}
			}
		}
	}
}

main();

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

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

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

function main() {
	var stampNum = parseInt(readline());

	var cities = [];

	var count = { augment: function(key) {
		if (this[key] === undefined) {
			this[key] = 1;
			cities.push(key);
		}
		else {
			this[key] += 1;
		}
	}};

	var link = { augment: function(key, value) {
		if (this[key] === undefined) {
			this[key] = [value];
		}
		else {
			this[key].push(value);
		}
	}};

	for (var i = 0; i < stampNum; ++i) {
		var pair = tokenizeIntegers(readline());
		count.augment(pair[0]);
		count.augment(pair[1]);
		link.augment(pair[0], pair[1]);
		link.augment(pair[1], pair[0]);
	}

	for (var i = 0; i < cities.length; ++i) {
		if (count[cities[i]] == 1) {
			var previous = cities[i];
			var path = [previous];
			var current = link[previous];
			while (true) {
				path.push(current);
				if (count[current] == 1) {
					print(path.join(" "));
					return;
				}
				var neighbors = link[current];
				if (neighbors[0] != previous) {
					previous = current;
					current = neighbors[0];
				}
				else {
					previous = current;
					current = neighbors[1];
				}
			}
		}
	}
}

main();


const readline = require('readline');

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

var stations = "";
var subST1 = "";
var subST2 = "";
var ind = 0;

rl.on('line', (input) => {
    if (ind == 0) {
        stations = input;
        ind++;
    } else if (ind == 1) {
        subST1 = input;
        ind++;
    }
    else {
        subST2 = input;
        /** */
        fw = false;
        bw = false;
        fw = ISTrue(stations, subST1, subST2);

        stations = reverceStr(stations);
        bw = ISTrue(stations, subST1, subST2);

        if (fw && bw)
            console.log("both");
        else if (fw)
            console.log("forward");
        else if (bw)
            console.log("backward");
        else
            console.log("fantasy");

        /** */
        rl.close();
    }
});

function ISTrue(s, first, second) {
    let indexOfFirst = s.indexOf(first);
    let lastIndexOFsecond = s.lastIndexOf(second);
    return indexOfFirst != -1 && lastIndexOFsecond != -1 &&
        (indexOfFirst + first.length - 1) < lastIndexOFsecond;
}

function reverceStr(s) {
  /*  let arr = s.split('');
    let limit = parseInt(s.length / 2);
    for (let i = 0, j = s.length - 1; i < limit; i++ , j--) {
        let t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
    return arr.join('');*/
   temp="";
   for(let i=s.length-1;i>=0;i--)
                  temp+=s.charAt(i);

   return  temp;
}

const readline = require("readline");

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

function myFunction(s, array, index) {
  for (var i = index.x; i < array.length; i++) {

    if (s == array.substr(i, s.length))
    {
        index.x = i+s.length;
        return true;
    }
  }
  index.x = 0;
  return false;
}

function reverseString(str) {
  var splitString = str.split("");
  var reverseArray = splitString.reverse();
  var joinArray = reverseArray.join("");
  return joinArray;
}

var n = -1;
var sleep1 = "";
var sleep2 = "";
var array = "";
var index = { x: 0 };
var startToFinish1 = false;
var startToFinish2 = false;
var finishToStart1 = false;
var finishToStart2 = false;
var answer = "";

rl.on("line", input => {
  if (n == -1)
  {
    array = input;
    n=0;
  }
  else if (n == 0) {
    sleep1 = input;
    startToFinish1 = myFunction(sleep1, array, index);
    n++;
  } else {
    sleep2 = input;
    startToFinish2 = myFunction(sleep2, array, index);
    var revarray = reverseString(array);
    index.x = 0;
    finishToStart1 = myFunction(sleep1, revarray, index);
    finishToStart2 = myFunction(sleep2, revarray, index);

    if (startToFinish1 && startToFinish2 && finishToStart1 && finishToStart2)
      answer = "both";
    else if (startToFinish1 && startToFinish2) answer = "forward";
    else if (finishToStart1 && finishToStart2) answer = "backward";
    else answer = "fantasy";
  }
}).on("close", () => {
  console.log(answer);
  //console.log(sleep2);
});

const readline = require('readline');

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

var check = function (flags, first, second) {
    var fIndex = flags.indexOf(first);
    var sLastIndex = flags.lastIndexOf(second);

    return fIndex != -1 && sLastIndex != -1 && fIndex + first.length - 1 < sLastIndex;

};

var flags, first, second;
var i = 0;
rl.on('line', (input) => {
    if (i == 0) {
        flags = input;
        i++;
    } else if (i == 1) {
        first = input;
        i++;
    } else {
        second = input;
        /* our code here...
         */
        var f = check(flags, first, second);
        flags = flags.split("").reverse().join("");
        var b = check(flags, first, second);
        if (f && b) console.log("both");
        else if (f) console.log("forward");
        else if (b) console.log("backward");
        else console.log("fantasy");
        rl.close();
        //end of our code....
    }
}).on('close', () => {

});

var fa = readline()
var fs = readline()
var fe = readline()

var f = false
var b = false

var sif = fa.indexOf(fs)
var eif = fa.lastIndexOf(fe)
var sib = fa.lastIndexOf(fs.split('').reverse().join(''))
var eib = fa.indexOf(fe.split('').reverse().join(''))

if (~sif && ~eif && eif - sif >= fs.length) f = true
if (~eib && ~sib && sib - eib >= fe.length) b = true

if (f && b) {print('both')}
else if (f && !b) {print('forward')}
else if (!f && b) {print('backward')}
else {print('fantasy')}

function stringReverse(str) {
  return str.split('').reverse().join('');
}

function check(str, subStr1, subStr2) {
  var index1 = str.indexOf(subStr1), index2 = str.lastIndexOf(subStr2);
  return index1 > -1 && index2 > -1 && index1 + subStr1.length - 1 < index2;
}

function has(first, last) {
  if(first && last) {
    return('both');
  }
  else if(first) {
    return('forward');
  }
  else if(last) {
    return('backward');
  }
  else {
    return('fantasy');
  }
}

var str = readline(), subStr1 = readline(), subStr2 = readline();
print(has(check(str, subStr1, subStr2), check(stringReverse(str), subStr1, subStr2)));

sequence = readline()
firstTest = readline()
secondTest = readline()

isForward = false
isBackward = false

if ((index = sequence.indexOf(firstTest)) >= 0) {
    if (sequence.indexOf(secondTest, index + firstTest.length) >= 0) {
        isForward = true;
    }
}

sequenceReverse = sequence.split("").reverse().join("")

if ((index = sequenceReverse.indexOf(firstTest)) >= 0) {
    if (sequenceReverse.indexOf(secondTest, index + firstTest.length) >= 0) {
        isBackward = true;
    }
}

if (isForward && isBackward) {
    print("both")
} else if (isForward) {
    print("forward")
} else if (isBackward) {
    print("backward")
} else {
    print("fantasy")
}

const readline = require('readline');

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

var stations = "";
var subST1 = "";
var subST2 = "";
var ind = 0;

rl.on('line', (input) => {
    if (ind == 0) {
        stations = input;
        ind++;
    } else if (ind == 1) {
        subST1 = input;
        ind++;
    }
    else {
        subST2 = input;
        /** */
        fw = false;
        bw = false;
        fw = ISTrue(stations, subST1, subST2);

        stations = reverceStr(stations);
        bw = ISTrue(stations, subST1, subST2);

        if (fw && bw)
            console.log("both");
        else if (fw)
            console.log("forward");
        else if (bw)
            console.log("backward");
        else
            console.log("fantasy");

        /** */
        rl.close();
    }
});

function ISTrue(s, first, second) {
    let indexOfFirst = s.indexOf(first);
    let lastIndexOFsecond = s.lastIndexOf(second);
    return indexOfFirst != -1 && lastIndexOFsecond != -1 &&
        (indexOfFirst + first.length - 1) < lastIndexOFsecond;
}

function reverceStr(s) {
    let arr = s.split('');
    let limit = parseInt(s.length / 2);
    for (let i = 0, j = s.length - 1; i < limit; i++ , j--) {
        let t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
    return arr.join('');
}

function reverseString(s)
{
	var str = "";
	for (var i = s.length; i >= 1; i--)
	{
		str += s[i - 1];
	}
	return str;
}

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(reverseString(x));

	var yIndex = s.indexOf(reverseString(y));
	var yLastIndex = s.lastIndexOf(y);

	var forwoardFlag = yLastIndex - xIndex >= x.length;
	if (yLastIndex == -1 || xIndex == -1)
	{
		forwoardFlag = false;
	}

	var backwardFlag = xLastIndex - yIndex >= y.length;
	if (xLastIndex == -1 || yIndex == -1)
	{
		backwardFlag = false;
	}

	if (forwoardFlag && !backwardFlag)
	{
		print("forward");
	}

	else if (!forwoardFlag && backwardFlag)
	{
		print("backward");
	}

	else if (forwoardFlag && backwardFlag)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

const readline = require('readline');

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

var check = function (flags, first, second) {
  /*for (let i = 0; i < flags.length; i++) {
    if (flags[i]==first) {
      flags.substr(i, first.length);
      for (let j = 0; j < flags.length; j++) {
          if (flags[j]==second)
              return true;
      }
      return false;
    }
  }*/
  let f_f_indx = flags.indexOf(first);
  let l_s_indx = flags.lastIndexOf(second);
  return f_f_indx != -1 && l_s_indx != -1 && f_f_indx + first.length - 1 < l_s_indx;
}

var revFlags = function (str) {
  return str.split("").reverse().join("");
}

var flags, first, second, indicator = 0;

rl.on('line', (input) => {
  if (indicator == 0) {
    flags = input;
    indicator++;
  }
  else if (indicator == 1) {
    first = input;
    indicator++;
  }
  else {
    second = input;

    let f = check(flags, first, second);
    //console.log("flags = %s", flags);
    flags = revFlags(flags);
    //console.log("flags = %s  first = %s second = %s ", flags, first, second);
    let b = check(flags, first, second);

    if (f && b)
      console.log("both");
    else if (f)
      console.log("forward");
    else if (b)
      console.log("backward");
    else
      console.log("fantasy");

    rl.close();
  }

});

var str = readline();
var first = readline();
var second = readline();

function checkStr(str) {
    pos1 = str.indexOf(first);
    flag = false;
    if (pos1 >= 0) {
        str = str.substr(pos1 + first.length);
        pos2 = str.indexOf(second);
        if (pos2 >= 0) flag = true;
    }
    return flag;
}

forwardFlag = checkStr(str);
backwardFlag = checkStr(str.split('').reverse().join(''));

if (forwardFlag && backwardFlag) print('both');
else if (forwardFlag) print('forward');
else if (backwardFlag) print('backward');
else print('fantasy');

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
/**
       0123456789|||
flags="abcFFdefgZZhi"
first="FF"
second="ZZ"

A            B
O----------->O   forward

A            B
O<-----------O   backward

A            B
O<----------->O   both

A            B
OX-----------XO   fantasy

       0123456
flags="aaacaaa"
first="aca"
second="aa"

       012345
flags="aaacaa"
first="aca"
second="aa"

 */
//our functions here...
var check = function (flags, first, second) {
    let FInd = flags.indexOf(first);
    let SLInd = flags.lastIndexOf(second);
    return FInd != -1 && SLInd != -1 && FInd + first.length - 1 < SLInd;
}

var revStr = function (s) {
    let ans = "";
    for (let i = s.length - 1; i >= 0; i--)
        ans += s[i];

    return ans;
}

var flags, first, second, indicator = 0;

rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;
    }
    else {
        second = input;
        //************ */
        //console.log("flags= %s      first= %s      second= %s",flags,first,second);
        let f = check(flags, first, second);
        flags = revStr(flags);
        //console.log("flags= %s      first= %s      second= %s",flags,first,second);
        let b = check(flags, first, second);
        //console.log("flags= %s      first= %s      second= %s   f= %s   b=%s",flags,first,second,f,b);
        if (f && b) console.log("both");
        else if (f) console.log("forward");
        else if (b) console.log("backward");
        else console.log("fantasy");
        /************** */
        rl.close();
    }

});

const readline = require('readline');

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

var check = function (flags, first, second) {
    FIndex = flags.indexOf(first);
    SLIndex = flags.lastIndexOf(second);
    return FIndex != -1 && SLIndex != -1 && FIndex + first.length - 1 < SLIndex;
}

var flags, first, second, identifier = 0

rl.on('line', (input) => {
    if (identifier == 0) {
        flags = input;
        identifier++;
    }
    else if (identifier == 1) {
        first = input;
        identifier++;
    }
    else {
        second = input;
        /*our code here*/
        var f = check(flags,first,second);
        flags = flags.split("").reverse().join("");
        var b = check(flags,first,second);

        if (f && b)
            console.log("both")
        else if (f)
            console.log("forward")
        else if (b)
            console.log("backward")
        else
            console.log("fantasy")

        /**end of our code */
        rl.close();
    }
});

'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 rev(s)
{
    return s.split('').reverse().join('');
}
function main()
{
    let s=readLine().trim();
    let a=readLine().trim();
    let b=readLine().trim();
    let x=0;
    let r=rev(s);
    if((s.includes(a))&&(s.includes(b)))
    {
        let aend=s.indexOf(a)+a.length-1,bstart=r.length-r.indexOf(rev(b))-b.length;
        //console.log(aend+' '+bstart);
        if((aend<bstart)&&(aend<s.length)&&(bstart>=0))
        {
           x+=1;
        }
    }
    r=s;
    s=rev(s);
    if((s.includes(a))&&(s.includes(b)))
    {
        let aend=s.indexOf(a)+a.length-1,bstart=r.length-r.indexOf(rev(b))-b.length;
        //console.log(aend+' '+bstart);
        if((aend<bstart)&&(aend<s.length)&&(bstart>=0))
        {
           x+=2;
        }
    }
    if(x===0)
    {
        console.log('fantasy');
    }
    else if(x==1)
    {
        console.log('forward');
    }
    else if(x==2)
    {
        console.log('backward');
    }
    else if(x==3)
    {
        console.log('both');
    }
}

function solve(s, a, b) {
  let forward = false;
  let backward = false;
  const a_indx = s.indexOf(a);
  if (a_indx < 0) {
    // do nothing ...
  } else {
    const b_indx = s.indexOf(b, a_indx + a.length);
    if (b_indx < 0) {
      // do nothing ...
    } else {
      forward = true;
    }
  }
  const s_r = s.split('').reverse().join('');
  const a_indx_r = s_r.indexOf(a);
  if (a_indx_r < 0) {
    // do nothing ...
  } else {
    const b_indx_r = s_r.indexOf(b, a_indx_r + a.length);
    if (b_indx_r < 0) {
      // do nothing ...
    } else {
      backward = true;
    }
  }
  if (forward) {
    if (backward) {
      return 'both';
    } else {
      return 'forward';
    }
  } else {
    if (backward) {
      return 'backward';
    } else {
      return 'fantasy'
    }
  }
};

function main(lines) {
  const trim_lines = [];
  for (const line of lines) {
    const t = line.trim();
    if (t.length > 0) {
      trim_lines.push(t);
    }
  }
  const [s, a, b] = trim_lines;
  const result = solve(s, a, b);
  console.log(result);
};

async function processLinesFromStream(rs, pf) {
  const content = [];
  rs.setEncoding("utf-8");
  for await (let chunk of rs) {
    content.push(chunk);
  }
  const lines = content.join('').split(/\n/);
  const result = pf(lines);
  return result;
};

processLinesFromStream(process.stdin, main);

const readline = require('readline');

const INPUT = 'test.in';
const LOCAL = process.env.ENABLE_LOCAL_ENV === 'true';
const log = (...args) => {
    LOCAL && console.log(...args)
};
let rl;
let lineNr = 0;

/**
 * MAIN
 */
(async () => {
    const promises = await setup(INPUT);
    await Promise.all(promises);
})();

function setup() {
    const promises = []
    rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false,
    });

    rl.on('line', (line) => {
        promises.push(work(line, lineNr));
        lineNr++;
    });

    rl.on('error', (err) => {
        log('Error:', err);
    });

    return new Promise(res => {
        rl.on('close', () => {
            res(promises)
            log('Done!');
        });
    });
}

const input = [];
async function work(line, lineNr) {
    input.push(line);
    if (input.length === 3) {
        const solutionForLine = await solve(input);
        const responseLine = `${solutionForLine}\n`;
        process.stdout.write(responseLine);
    }
}

/**
 *
 * Solution goes below
 *
 */
function solve(input) {
    const [str, s1, s2] = input;
    let opts = 'init_';
    if (isForward(str, s1, s2)) opts += 'f';
    if (isBackward(str, s1, s2)) opts += 'b';
    const res = {
        'init_': 'fantasy',
        'init_f': 'forward',
        'init_b': 'backward',
        'init_fb': 'both',
    };
    return res[opts];
}

function isForward(str, s1, s2) {
    const s1Index = str.indexOf(s1);
    if (s1Index !== -1) return str.indexOf(s2, s1Index + s1.length) !== -1;
    return false;
}

function isBackward(str, s1, s2) {
    return isForward(str.split('').reverse().join(''), s1, s2);
}

const readline = require('readline');

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

var check = function (flags, first, second) {
    let firstIndex = flags.indexOf(first);
    let lastIndex = flags.lastIndexOf(second);
    //console.log("inside check function: \nfirstIndex= %d  lastIndex = %d",firstIndex, lastIndex);
    return firstIndex != -1 && lastIndex != -1 && firstIndex + first.length - 1 < lastIndex;
}

function reverseString(str) {
    return str.split("").reverse().join("");
}
/*

flags= "aaacaaa"
first=" aca"
second= "aa"

*/
/*
C#
while(true){
   string input = Console.ReadLine();

}

nodejs
rl.on('line', (input)=>{

});
*/
var flags, first, second, indicator = 0;
rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;
    }
    else {
        second = input;
        //our code here.....
        //console.log("flags= %s  first= %s   second= %s",flags, first, second);
        let f = check(flags, first, second);
        flags = reverseString(flags);
        let b = check(flags, first, second);

        if (f && b)
            console.log("both");
        else if (f)
            console.log("forward");
        else if (b)
            console.log("backward");
        else
            console.log("fantasy");

        rl.close();
    }
});

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var check = function (flags, first, second) {
    fIndx = flags.indexOf(first);
    sIndx = flags.lastIndexOf(second);

    return fIndx != -1 && sIndx != -1 && fIndx + first.length - 1 < sIndx;
}
var flags, first, second, indicator = 0;
rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;

    }
    else {
        second = input;
        let f = check(flags, first, second);
        flags = flags.split('').reverse().join('');
        let b = check(flags, first, second);

        if (f && b)
            console.log("both");
        else if (f)
            console.log("forward");
        else if (b)
            console.log("backward");
        else
            console.log("fantasy");
        rl.close();
    }
});

const readline = require('readline');

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

var checkFlags = function(flags, first, second) {
  for (let i = 0; i < flags.length; i++) {
    if (flags[i]==first) {
      flags.substr(i, first.length);
      for (let j = 0; j < flags.length; j++) {
          if (flags[j]==second)
              return true;
      }
      return false;
    }
  }
}

var revFlags = function(str) {
  return str.split("").reverse().join("");
}

var temp = [], forw = false, backw = false;

rl.on('line', (input) => {
    temp.push(input);
    let flags = temp[0], first = temp[1], second = temp[2];
    let reversedFlags = revFlags(flags);

    if (checkFlags(flags,first,second))
      forw = true;
    if (checkFlags(reversedFlags,second,first))
      backw = true;

  }).on('close', function() {

    if (forw && backw)
      console.log("both");
    else if (forw)
      console.log("forward");
    else if (backw)
      console.log("backward");
    else
      console.log("fantasy");

  });

const readline = require('readline');

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

var check = function (flags, first, second) {

  let f_f_indx = flags.indexOf(first);
  let l_s_indx = flags.lastIndexOf(second);
  return f_f_indx != -1 && l_s_indx != -1 && f_f_indx + first.length - 1 < l_s_indx;
}

var revFlags = function (str) {
  return str.split("").reverse().join("");
}

var flags, first, second, indicator = 0;

rl.on('line', (input) => {
  if (indicator == 0) {
    flags = input;
    indicator++;
  }
  else if (indicator == 1) {
    first = input;
    indicator++;
  }
  else {
    second = input;

    let f = check(flags, first, second);
    let b = check(revFlags(flags), second, first);

    if (f && b)
      console.log("both");
    else if (f)
      console.log("forward");
    else if (b)
      console.log("backward");
    else
      console.log("fantasy");

    rl.close();
  }

});

'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 rev(s)
{
    return s.split('').reverse().join('');
}
function main()
{
    let s=readLine().trim();
    let a=readLine().trim();
    let b=readLine().trim();
    let x=0;
    let r=rev(s);
    if((s.includes(a))&&(s.includes(b)))
    {
        let aend=s.indexOf(a)+a.length-1,bstart=r.length-r.indexOf(rev(b))-b.length;
        //console.log(aend+' '+bstart);
        if((aend<bstart)&&(aend<s.length)&&(bstart>=0))
        {
           x+=1;
        }
    }
    r=s;
    s=rev(s);
    if((s.includes(a))&&(s.includes(b)))
    {
        let aend=s.indexOf(a)+a.length-1,bstart=r.length-r.indexOf(rev(b))-b.length;
        //console.log(aend+' '+bstart);
        if((aend<bstart)&&(aend<s.length)&&(bstart>=0))
        {
           x+=2;
        }
    }
    if(x===0)
    {
        console.log('<<fantasy>>');
    }
    else if(x==1)
    {
        console.log('<<forward>>');
    }
    else if(x==2)
    {
        console.log('<<backward>>');
    }
    else if(x==3)
    {
        console.log('<<both>>');
    }
}

const readline = require('readline');

const INPUT = 'test.in';
const LOCAL = process.env.ENABLE_LOCAL_ENV === 'true';
const log = (...args) => {
    LOCAL && console.log(...args)
};
let rl;
let lineNr = 0;

/**
 * MAIN
 */
(async () => {
    const promises = await setup(INPUT);
    await Promise.all(promises);
})();

function setup() {
    const promises = []
    rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false,
    });

    rl.on('line', (line) => {
        promises.push(work(line, lineNr));
        lineNr++;
    });

    rl.on('error', (err) => {
        log('Error:', err);
    });

    return new Promise(res => {
        rl.on('close', () => {
            res(promises)
            log('Done!');
        });
    });
}

const input = [];
async function work(line, lineNr) {
    input.push(line);
    if (input.length === 3) {
        const solutionForLine = await solve(input);
        const responseLine = `${solutionForLine}\n`;
        process.stdout.write(responseLine);
    }
}

/**
 *
 * Solution goes below
 *
 */
function solve(input) {
    const [str, s1, s2] = input;
    let opts = 'init_';
    if (isForward(str, s1, s2)) opts += 'f';
    if (isBackward(str, s1, s2)) opts += 'b';
    const res = {
        'init_': 'fantasy',
        'init_f': 'forward',
        'init_b': 'backward',
        'init_fb': 'both',
    };
    return res[opts];
}

function isForward(str, s1, s2) {
    const s1Index = str.indexOf(s1);
    if (s1Index !== -1) return str.indexOf(s2, s1Index + 1) !== -1;
    return false;
}

function isBackward(str, s1, s2) {
    const s1Index = str.lastIndexOf(s1);
    if (s1Index !== -1) return str.lastIndexOf(s2, s1Index + 1) !== -1;
    return false;
}

const readline = require('readline');

const INPUT = 'test.in';
const LOCAL = process.env.ENABLE_LOCAL_ENV === 'true';
const log = (...args) => {
    LOCAL && console.log(...args)
};
let rl;
let lineNr = 0;

/**
 * MAIN
 */
(async () => {
    const promises = await setup(INPUT);
    await Promise.all(promises);
})();

function setup() {
    const promises = []
    rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false,
    });

    rl.on('line', (line) => {
        promises.push(work(line, lineNr));
        lineNr++;
    });

    rl.on('error', (err) => {
        log('Error:', err);
    });

    return new Promise(res => {
        rl.on('close', () => {
            res(promises)
            log('Done!');
        });
    });
}

const input = [];
async function work(line, lineNr) {
    input.push(line);
    if (input.length === 3) {
        const solutionForLine = await solve(input);
        const responseLine = `${solutionForLine}\n`;
        process.stdout.write(responseLine);
    }
}

/**
 *
 * Solution goes below
 *
 */
function solve(input) {
    const [str, s1, s2] = input;
    let opts = 'init_';
    if (isForward(str, s1, s2)) opts += 'f';
    if (isBackward(str, s1, s2)) opts += 'b';
    const res = {
        'init_': 'fantasy',
        'init_f': 'forward',
        'init_b': 'backward',
        'init_fb': 'both',
    };
    return res[opts];
}

function isForward(str, s1, s2) {
    const s1Index = str.indexOf(s1);
    if (s1Index !== -1) return str.indexOf(s2, s1Index + s1.length) !== -1;
    return false;
}

function isBackward(str, s1, s2) {
    const s1Index = str.lastIndexOf(s1);
    if (s1Index !== -1) return str.lastIndexOf(s2, s1Index + s1.length) !== -1;
    return false;
}

const readline = require('readline');

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

var check = function (flags, first, second) {
    let firstIndex = flags.indexOf(first);
    let lastIndex = flags.lastIndexOf(second);
    //console.log("inside check function: \nfirstIndex= %d  lastIndex = %d",firstIndex, lastIndex);
    return firstIndex != -1 && lastIndex != -1 && firstIndex + first.length - 1 < lastIndex;
}

function reverseString(str) {
    return str.split("").reverse().join("");
}
/*

flags= "aaacaaa"
first=" aca"
second= "aa"

*/
/*
C#
while(true){
   string input = Console.ReadLine();

}

nodejs
rl.on('line', (input)=>{

});
*/
var flags, first, second, indicator = 0;
rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;
    }
    else {
        second = input;
        //our code here.....
        //console.log("flags= %s  first= %s   second= %s",flags, first, second);
        let f = check(flags, first, second);
        flags = reverseString(flags);
        let b = check(flags, first, second);

        if (f && b)
            console.log("both");
        else if (f)
            console.log("forward");
        else if (b)
            console.log("bakward");
        else
            console.log("fantacy");

        rl.close();
    }
});

const readline = require('readline');

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

var check = function (flags, first, second) {
    let firstIndex = flags.indexOf(first);
    let lastIndex = flags.lastIndexOf(second);
    //console.log("inside check function: \nfirstIndex= %d  lastIndex = %d",firstIndex, lastIndex);
    return firstIndex != -1 && lastIndex != -1 && firstIndex + first.length - 1 < lastIndex;
}

function reverseString(str) {
    return str.split("").reverse().join("");
}
/*

flags= "aaacaaa"
first=" aca"
second= "aa"

*/
/*
C#
while(true){
   string input = Console.ReadLine();

}

nodejs
rl.on('line', (input)=>{

});
*/
var flags, first, second, indicator = 0;
rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;
    }
    else {
        second = input;
        //our code here.....
        //console.log("flags= %s  first= %s   second= %s",flags, first, second);
        let f = check(flags, first, second);
        flags = reverseString(flags);
        let b = check(flags, first, second);

        if (f && b)
            console.log("both");
        else if (f)
            console.log("forward");
        else if (b)
            console.log("bakward");
        else
            console.log("fantasy");

        rl.close();
    }
});

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var check = function (flags, first, second) {
    fIndx = flags.indexOf(first);
    sIndx = flags.lastIndexOf(second);

    return fIndx != -1 && sIndx != -1 && fIndx + first.length - 1 < sIndx;
}
var flags, first, second, indicator = 0;
rl.on('line', (input) => {
    if (indicator == 0) {
        flags = input;
        indicator++;
    }
    else if (indicator == 1) {
        first = input;
        indicator++;

    }
    else {
        second = input;
        let f = check(flags, first, second);
        flags = flags.split('').reverse().join('');
        let b = check(flags, first, second);

        if (f && b)
            console.log("both");
        else if (f)
            console.log("forwards");
        else if (b)
            console.log("backwards");
        else
            console.log("fantasy");
        rl.close();
    }
});

const readline = require("readline");

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

function myFunction(s, array, index) {
  for (var i = index.x; i < array.length; i++) {
    index.x = i;
    if (s == array.substr(i, s.length)) return true;
  }
  index.x = 0;
  return false;
}

function reverseString(str) {
  var splitString = str.split("");
  var reverseArray = splitString.reverse();
  var joinArray = reverseArray.join("");
  return joinArray;
}

var n = -1;
var sleep1 = "";
var sleep2 = "";
var array = "";
var index = { x: 0 };
var startToFinish1 = false;
var startToFinish2 = false;
var finishToStart1 = false;
var finishToStart2 = false;
var answer = "";

rl.on("line", input => {
  if (n++ == -1) array = input;
  if (n++ == 0) {
    sleep1 = input;
    startToFinish1 = myFunction(sleep1, array, index);
  } else {
    sleep2 = input;
    startToFinish2 = myFunction(sleep2, array, index);
    var revarray = reverseString(array);
    index.x = 0;
    finishToStart1 = myFunction(sleep1, revarray, index);
    finishToStart2 = myFunction(sleep2, revarray, index);

    if (startToFinish1 && startToFinish2 && finishToStart1 && finishToStart2)
      answer = "both";
    else if (startToFinish1 && startToFinish2) answer = "forward";
    else if (finishToStart1 && finishToStart2) answer = "backward";
    else answer = "fantasy";
  }
}).on("close", () => {
  console.log(answer);
});

const readline = require("readline");

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

function myFunction(s, array, index) {
  for (var i = index.x; i < array.length-s.length; i++) {

    if (s == array.substr(i, s.length))
    {
        index.x = i+s.length;
        return true;
    }
  }
  index.x = 0;
  return false;
}

function reverseString(str) {
  var splitString = str.split("");
  var reverseArray = splitString.reverse();
  var joinArray = reverseArray.join("");
  return joinArray;
}

var n = -1;
var sleep1 = "";
var sleep2 = "";
var array = "";
var index = { x: 0 };
var startToFinish1 = false;
var startToFinish2 = false;
var finishToStart1 = false;
var finishToStart2 = false;
var answer = "";

rl.on("line", input => {
  if (n++ == -1) array = input;
  if (n++ == 0) {
    sleep1 = input;
    startToFinish1 = myFunction(sleep1, array, index);
  } else {
    sleep2 = input;
    startToFinish2 = myFunction(sleep2, array, index);
    var revarray = reverseString(array);
    index.x = 0;
    finishToStart1 = myFunction(sleep1, revarray, index);
    finishToStart2 = myFunction(sleep2, revarray, index);

    if (startToFinish1 && startToFinish2 && finishToStart1 && finishToStart2)
      answer = "both";
    else if (startToFinish1 && startToFinish2) answer = "forward";
    else if (finishToStart1 && finishToStart2) answer = "backward";
    else answer = "fantasy";
  }
}).on("close", () => {
  console.log(answer);
});

function stringReverse(str) {
  return str.split('').reverse().join('');
}

function check(str, subStr1, subStr2) {
  return str.indexOf(subStr1) + subStr1.length - 1 < str.lastIndexOf(subStr2);
}

function has(first, last) {
  if(first && last) {
    return('both');
  }
  else if(first) {
    return('forward');
  }
  else if(last) {
    return('backward');
  }
  else {
    return('fantasy');
  }
}

var str = readline(), subStr1 = readline(), subStr2 = readline();
print(has(check(str, subStr1, subStr2), check(stringReverse(str), subStr1, subStr2)));

function stringReverse(str) {
  return str.split('').reverse().join('');
}

function check(str, subStr1, subStr2) {
  return str.indexOf(subStr1) < str.lastIndexOf(subStr2);
}

function has(first, last) {
  if(first && last) {
    return('both');
  }
  else if(first) {
    return('forward');
  }
  else if(last) {
    return('backward');
  }
  else {
    return('fantasy');
  }
}

var str = readline(), subStr1 = readline(), subStr2 = readline();
print(has(check(str, subStr1, subStr2), check(stringReverse(str), subStr1, subStr2)));

sequence = readline()
firstTest = readline()
secondTest = readline()

isForward = false
isBackward = false

if ((index = sequence.indexOf(firstTest)) >= 0) {
    if (sequence.indexOf(secondTest, index) >= 0) {
        isForward = true;
    }
}

sequenceReverse = sequence.split("").reverse().join("")

if ((index = sequenceReverse.indexOf(firstTest)) >= 0) {
    if (sequenceReverse.indexOf(secondTest, index) >= 0) {
        isBackward = true;
    }
}

if (isForward && isBackward) {
    print("both")
} else if (isForward) {
    print("forward")
} else if (isBackward) {
    print("backward")
} else {
    print("fantasy")
}

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

	if (s[3] == 'a' && s[2] == 'a' && s[0] == 'a')
	{
//		print(s);
		print(x);
		print(y);
	}

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
		print("fantasy");
	}

	else if (xIndex < yLastIndex && xLastIndex <= yIndex)
	{
		print("forward");
	}

	else if (xIndex >= yLastIndex && xLastIndex > yIndex)
	{
		print("backward");
	}

	else if (xIndex < yLastIndex && xLastIndex > yIndex)
	{
		print("both");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
		print("fantasy");
	}

	else if (yLastIndex - xIndex > x.length && !(xLastIndex - yIndex > y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex > x.length) && xLastIndex - yIndex > y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex > x.length && xLastIndex - yIndex > y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

	if (s[4] == 'a')
	{
		print(s);
		print(x);
		print(y);
	}

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex > x.length && !(xLastIndex - yIndex > y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex > x.length) && xLastIndex - yIndex > y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex > x.length && xLastIndex - yIndex > y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
		print("fantasy");
	}

	else if (yLastIndex - xIndex <= x.length && !(xLastIndex - yIndex <= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex <= x.length) && xLastIndex - yIndex <= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex <= x.length && xLastIndex - yIndex <= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

	if (s[3] == 'a' && s[2] == 'a')
	{
		print(s);
		print(x);
		print(y);
	}

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

	if (s[3] == 'a')
	{
		print(s);
		print(x);
		print(y);
	}

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();

function main()
{
	var s = readline();
	var x = readline();
	var y = readline();

//	print(s);
//	print(x);
//	print(y);

	var xIndex = s.indexOf(x);
	var xLastIndex = s.lastIndexOf(x);
//	print(xIndex + " " + xLastIndex);

	var yIndex = s.indexOf(y);
	var yLastIndex = s.lastIndexOf(y);
//	print(yIndex + " " + yLastIndex);

	if (xIndex == -1 || yIndex == -1)
	{
//		print(xIndex);
//		print(yIndex);
		print("fantasy");
	}

	else if (yLastIndex - xIndex >= x.length && !(xLastIndex - yIndex >= y.length))
	{
		print("forward");
	}

	else if (!(yLastIndex - xIndex >= x.length) && xLastIndex - yIndex >= y.length)
	{
		print("backward");
	}

	else if (yLastIndex - xIndex >= x.length && xLastIndex - yIndex >= y.length)
	{
		print("both");
	}

	else
	{
		print("fantasy");
	}
}

main();


process.stdin.resume()
process.stdin.setEncoding('utf-8')
let inputBuf = ''
let curLine = 0
process.stdin.on('data', inputData => {
	inputBuf += inputData
})
process.stdin.on('end', _ => {
	inputBuf = inputBuf.trim().split('\n')
	main()
})

const log = (...params) => console.log(...params)
const R = new Proxy({}, {
	get: (target, property, receiver) => {
		if (property === 'i') {
			return parseInt(inputBuf[curLine++], 10)
		} else if (property === 'l') {
			return inputBuf[curLine++]
		} else if (property === 'ii') {
			return inputBuf[curLine++].split(' ').map(s => parseInt(s, 10))
		}
	},
	set: (target, property, value, receiver) => {
		if (property === 'lp') {
			let n = R.i
			for (let i = 0; i < n; i++) value(i)
		}
	}
})
const FLPProxy = new Proxy({}, {
	set: (target, property, value, receiver) => {
		for (let i = 0; i < property; i++) value(i)
	}
})
const F = new Proxy({}, {
	get: (target, property, receiver) => {
		if (property === 'lp') {
			return FLPProxy
		}
	}
})
const A = {
	repeat: (n, value) => Array(n).fill(value),
	range: (l, r) => {
		let arr = Array(r - l)
		for (let i = 0; i < arr.length; i++) arr[i] = i + l
		return arr
	}
}

function main() {
	R.lp=_=>{
		n=R.i
		cnt=R.ii.reduce((p,v)=>{
			F.lp[30]=i=>{
				if(1<<i & v)p[i]++
			}
			return p
		}, A.repeat(30,0))
		ans=A.range(1,n+1).filter(a=>!cnt.some(c=>c%a)).join(' ')
		log(ans)
	}
}

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

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

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

		const cnt = Array(30).fill(0);
		for (let i = 0; i < n; i++) {
			for (let j = 0; j < 30; j++) {
				cnt[j] += !!(a[i] & 1 << j);
			}
		}

		const ans = [];
		for (let k = 1; k <= n; k++) {
			let ok = true;
			for (let b = 0; ok && b < 30; b++) {
				ok = cnt[b] % k == 0
			}
			if (ok) {
				ans.push(k);
			}
		}

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

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};

const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    let res = new Array(31).fill(0);
    for (let i = 0; i < n; i++) {
      let j = arr[i],
        k = 0;
      while (j > 0) {
        if (j & 1) res[k]++;
        j = j >> 1;
        k++;
      }
    }
    let ans = [];
    for (let i = 1; i <= n; i++) {
      let flag = 0;
      for (let j = 0; j < res.length; j++) if (res[j] % i !== 0) flag = 1;
      if (flag === 0) ans.push(i);
    }
    console.log(ans.join(" "));
  }
};
solve();

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

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

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

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

		const cnt = Array(30).fill(0);
		for (let i = 0; i < n; i++) {
			for (let j = 0; j < 30; j++) {
				cnt[j] += !!(a[i] & 1 << j);
			}
		}

		let g = 0;
		for (let i = 0; i < 30; i++) {
			g = gcd(g, cnt[i]);
		}

		const ans = [];
		for (let k = 1; k <= n; k++) {
			if (g % k == 0)
				ans.push(k);
		}

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