You are given an array $$$a_1, a_2, \dots, a_n$$$, which is sorted in non-descending order. You decided to perform the following steps to create array $$$b_1, b_2, \dots, b_n$$$:   Create an array $$$d$$$ consisting of $$$n$$$ arbitrary non-negative integers.  Set $$$b_i = a_i + d_i$$$ for each $$$b_i$$$.  Sort the array $$$b$$$ in non-descending order. You are given the resulting array $$$b$$$. For each index $$$i$$$, calculate what is the minimum and maximum possible value of $$$d_i$$$ you can choose in order to get the given array $$$b$$$.Note that the minimum (maximum) $$$d_i$$$-s are independent of each other, i. e. they can be obtained from different possible arrays $$$d$$$.

The first line contains the single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of arrays $$$a$$$, $$$b$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_i \le a_{i+1}$$$) — the array $$$a$$$ in non-descending order. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$; $$$b_i \le b_{i+1}$$$) — the array $$$b$$$ in non-descending order. Additional constraints on the input:    there is at least one way to obtain the array $$$b$$$ from the $$$a$$$ by choosing an array $$$d$$$ consisting of non-negative integers;  the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print two lines. In the first line, print $$$n$$$ integers $$$d_1^{min}, d_2^{min}, \dots, d_n^{min}$$$, where $$$d_i^{min}$$$ is the minimum possible value you can add to $$$a_i$$$. Secondly, print $$$n$$$ integers $$$d_1^{max}, d_2^{max}, \dots, d_n^{max}$$$, where $$$d_i^{max}$$$ is the maximum possible value you can add to $$$a_i$$$. All $$$d_i^{min}$$$ and $$$d_i^{max}$$$ values are independent of each other. In other words, for each $$$i$$$, $$$d_i^{min}$$$ is just the minimum value among all possible values of $$$d_i$$$.

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = lowerBound(i, boulders);
            let j = boulders[bi];

            if (j < i) {
                j = b.length - 1;
            } else if (j === i) {
                if (bi === boulders.length - 1) {
                    j = b.length - 1;
                } else {
                    j = boulders[bi + 1] - 1;
                }
            } else if (j > i) {
                j = j - 1;
            }

            max.push(b[j] - a[i]);
            // const j = pickMaxIndex(i, a, b);
            // max.push(b[j] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

function solve(n, sa, sb) {
    const min = sa.map((x, i) => {
        const j = binarySearch(0, n - 1, p => sb[p] < x) + 1
        return sb[j] - x
    })
    const bar = [0]
    for (let i = 1; i < n; i++) {
        if (sb[i - 1] < sa[i]) {
            bar.push(i)
        }
    }
    bar.push(n)
    // console.log('bar', bar)
    const max = sa.map((x, i) => {
        const j = binarySearch(0, bar.length - 1, p => bar[p] <= i) + 1
        return sb[bar[j] - 1] - x
    })
    return min.join(' ') + '\n' + max.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
}

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = lowerBound(i, boulders);
            let j = boulders[bi];

            if (j === i) {
                if (bi === boulders.length - 1) {
                    j = b.length - 1;
                } else {
                    j = boulders[bi + 1];
                }
            }

            if (j > i) {
                j--;
            }

            max.push(b[j] - a[i]);
            // const j = pickMaxIndex(i, a, b);
            // max.push(b[j] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = lowerBound(i, boulders);
            let j = boulders[bi];
            if (j > i) {
                j--;
            } else if (j === i) {
                // last boulder
                if (bi === boulders.length - 1) {
                    j = b.length - 1;
                } else {
                    j = boulders[bi + 1] - 1;
                }
            }
            max.push(b[j] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = boulders[lowerBound(i, boulders)];
            if (bi > i) {
                bi--;
            } else if (bi === i) {
                bi = b.length - 1;
            }
            max.push(b[bi] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = boulders[lowerBound(i, boulders)];
            if (bi > i) {
                bi--;
            }
            max.push(b[bi] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a, iMin = 0, iMax = a.length - 1) {
    // let iMin = 0;
    // let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b, 0, j);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = boulders[lowerBound(i, boulders)];
            if (bi > i) {
                bi--;
            }
            max.push(b[bi] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

// Finds index where x should be
function lowerBound(x, a) {
    let iMin = 0;
    let iMax = a.length - 1;
    // 1 2 3 4 (1) => 0
    // 1 2 3 4 (2) => 1
    // 1 2 3 4 (3) => 2
    // 1 2 3 4 (4) => 3
    // 1 2 3 4 (5) => 3
    while (iMax - iMin > 0) {
        const iMiddle = Math.floor((iMax + iMin) / 2);

        if (a[iMiddle] >= x) {
            iMax = iMiddle;
        } else {
            // a[iMiddle] < x
            iMin = iMiddle + 1;
        }
    }

    if (x > a[iMin]) {
        return Math.min(iMin + 1, a.length - 1);
    }

    return iMin;
}

// console.log('lb test', lowerBound(1, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(2, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(3, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(4, [1, 2, 3, 4], 0, 3));
// console.log('lb test', lowerBound(5, [1, 2, 3, 4], 0, 3));

function pickMinIndex(x, b) {
    for (let j = 0; j < b.length; j++) {
        return lowerBound(x, b, 0, j);
    }
}

function pickMaxIndex(i, a, b) {
    for (let j = i; j < b.length - 1; j++) {
        if (a[j + 1] > b[j]) {
            return j;
        }
    }

    return b.length - 1;
}

// indecies of elements which cannot be moved by -1
function createArrayOfBoulders(a, b) {
    const boulders = [];

    for (let i = 1; i < a.length; i++) {
        if (a[i] > b[i - 1]) {
            boulders.push(i);
        }
    }

    return boulders;
}

function solve(a, b) {
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
    }

    const boulders = createArrayOfBoulders(a, b);
    // console.log(boulders);
    const max = [];
    for (let i = 0; i < a.length; i++) {
        if (boulders.length > 0) {
            let bi = boulders[lowerBound(i, boulders)];
            if (bi !== i) {
                bi--;
            }
            max.push(b[bi] - a[i]);
        } else {
            max.push(b[b.length - 1] - a[i]);
        }
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

function pickMinIndex(x, b /* a is sorted */, minMap) {
    for (let j = 0; j < b.length; j++) {
        const num = b[j];
        if (
            num >= x
        ) {
            if (b[j + 1] === num && minMap[j + 1] === undefined) {
                continue;
            }

            return j;
        }
    }

    throw new Error('Cant find min');
}

function pickMaxIndex(x, xi, b /* a is sorted */, minMap, xMap) {
    for (let j = xMap[xi] + 1; j < b.length; j++) {
        if (minMap[j] !== undefined) {
            return j - 1;
        }
    }

    return b.length - 1;
}

function solve(a, b) {
    const minMap = {};
    const xMap = {};
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b, minMap);
        min.push(b[j] - x);
        minMap[j] = true;
        xMap[i] = j;
    }

    const max = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMaxIndex(x, i, b, minMap, xMap);
        max.push(b[j] - x);
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

function pickMinIndex(x, a /* a is sorted */) {
    for (let i = 0; i < a.length; i++) {
        const num = a[i];
        if (num >= x && a[i + 1] !== num) {
            return i;
        }
    }

    throw new Error('Cant find min');
}

function pickMaxIndex(x, xi, b /* a is sorted */, minMap, xMap) {
    for (let j = xMap[xi] + 1; j < b.length; j++) {
        if (minMap[j] !== undefined) {
            return j - 1;
        }
    }

    return b.length - 1;
}

function solve(a, b) {
    const minMap = {};
    const xMap = {};
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
        minMap[j] = true;
        xMap[i] = j;
    }

    const max = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMaxIndex(x, i, b, minMap, xMap);
        max.push(b[j] - x);
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

function pickMinIndex(x, a /* a is sorted */) {
    for (let i = 0; i < a.length; i++) {
        const num = a[i];
        if (num >= x) {
            return i;
        }
    }

    throw new Error('Cant find min');
}

function pickMaxIndex(x, xi, b /* a is sorted */, minMap, xMap) {
    for (let j = xMap[xi] + 1; j < b.length; j++) {
        if (minMap[j] !== undefined) {
            return j - 1;
        }
    }

    return b.length - 1;
}

function solve(a, b) {
    const minMap = {};
    const xMap = {};
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
        minMap[j] = true;
        xMap[i] = j;
    }

    const max = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMaxIndex(x, i, b, minMap, xMap);
        max.push(b[j] - x);
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});

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

var input = '';
process.stdin.on('data', function(chunk) {
    input += chunk;
});

function pickMinIndex(x, a /* a is sorted */) {
    for (let i = 0; i < a.length; i++) {
        const num = a[i];
        if (num >= x) {
            return i;
        }
    }

    throw new Error('Cant find min');
}

function pickMaxIndex(x, xi, a /* a is sorted */, minMap) {
    for (let i = a.length - 1; i >= 0; i--) {
        const num = a[i];
        if (num < x) {
            throw new Error(`Cant find max ${x}`);
        }

        if (
            minMap[i] === undefined ||
            minMap[i] === xi
        ) {
            return i;
        }
    }

    throw new Error(`Cant find max ${x}`);
}

function solve(a, b) {
    const minMap = {};
    const min = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMinIndex(x, b);
        min.push(b[j] - x);
        minMap[j] = i;
    }

    const max = [];
    for (let i = 0; i < a.length; i++) {
        const x = a[i];
        const j = pickMaxIndex(x, i, b, minMap);
        max.push(b[j] - x);
    }

    console.log(min.join(' '));
    console.log(max.join(' '));
}

process.stdin.on('end', function() {
    const data = input.trim().split('\n');

    for (let i = 1; i < data.length; i += 3) {
        const a = data[i + 1].split(' ').map(Number);
        const b = data[i + 2].split(' ').map(Number);

        solve(a, b);
    }
});


The territory of Berland is represented by a rectangular field n × m in size. The king of Berland lives in the capital, located on the upper left square (1, 1). The lower right square has coordinates (n, m). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out — one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king.

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100, 2 ≤  n · m) — the field size. The upper left square has coordinates (1, 1), and the lower right square has coordinates of (n, m).

On the first line output integer k — the minimum number of teleporters. Then output k lines each containing 4 integers x1 y1 x2 y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the square where the teleporter is installed (x1, y1), and the coordinates of the square where the teleporter leads (x2, y2). Then print nm + 1 lines containing 2 numbers each — the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1, 1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time.

const readline = require('readline');

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

var moves = [];
var needTeleport = false;
rl.on('line', (input) => {
    let line = input.split(' ').map(item => { return parseInt(item) });
    let n = line[0];
    let m = line[1];
    for (var i = 1; i <= n; i++)
        moves.push([i, 1]);

    if (m % 2 == 0 && n > 1) {
        var up = true;
        for (let j = 2; j <= m; j++) {
            if (up) {
                for (let i = n; i > 1; i--)
                    moves.push([i, j]);
                up = false;
            } else {
                for (let i = 2; i <= n; i++)
                    moves.push([i, j]);
                up = true;
            }

        }
        for (let j = m; j >= 1; j--)
            moves.push([1, j]);

    }
    else if (n % 2 == 0 && m > 1) {
        var right = true;
        for (let i = n; i > 1; i--) {

            if (right) {
                for (let j = 2; j <= m; j++)
                    moves.push([i, j]);
                right = false;

            } else {
                for (let j = m; j > 1; j--)
                    moves.push([i, j]);
                right = true;
            }
        }
        for (let j = m; j >= 1; j--)
            moves.push([1, j]);
    }
    else {
        if (!(n == 1 && m == 2) && !(n == 2 && m == 1))
            needTeleport = true;
        var up = true;
        for (let j = 2; j <= m; j++) {

            if (up) {
                for (let i = n; i >= 1; i--)
                    moves.push([i, j]);
                up = false;
            } else {
                for (let i = 1; i <= n; i++)
                    moves.push([i, j]);
                up = true;
            }

        }

        moves.push([1, 1]);
    }
    if (needTeleport) {
        console.log(1);
        //console.log(n + " " + m + " " + 1 + " " + 1);
        console.log("%d %d %d %d", n, m, 1, 1);
    } else
        console.log(0);

    moves.forEach(function (Element) {
        //console.log(element[0] + " " + element[1])
        console.log("%d %d", Element[0], Element[1]);
    });
    rl.close();

});


One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Print a single integer — the maximum length of a repost chain.

5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya

6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp

var n = parseInt(readline());
var s, m=0, a = new Array(), b = new Array(), l = new Array();
for(i=0; i<n; i++) {
s = readline().toLowerCase().split(" ");
a[i] = s[0];
b[i] = s[2];
}
for (j = n-1; j>=0; j--) {
l[j] = 2;
kk = b[j];
while (kk != b[0]) {
for (i=0; i<j; i++) {
if (a[i] == kk) {
l[j]++;
kk = b[i];
break;
}
}
}
if (l[j] > m) m = l[j];
}
print(m);

function Stack()
{
    this.a = [];
}

Stack.prototype.empty = function()
{
    return this.a.length == 0;
}

Stack.prototype.push = function(v)
{
    this.a.push(v);
}

Stack.prototype.pop = function()
{
    if (!this.empty())
        return this.a.pop();
    return undefined;
}

Stack.prototype.top = function()
{
    if (!this.empty())
        return this.a[this.a.length - 1];
    return undefined;
}

Stack.prototype.clear = function()
{
    this.a = [];
}

Stack.prototype.size = function()
{
    return this.a.length;
}

function Queue()
{
    this.l = new Stack();
    this.r = new Stack();
}

Queue.prototype.push = function(v)
{
    this.l.push(v);
}

Queue.prototype.empty = function()
{
    return this.r.empty() && this.l.empty();
}

Queue.prototype.pop = function()
{
    if (this.r.empty())
    {
        while(!this.l.empty())
            this.r.push(this.l.pop());
    }
    if (!this.r.empty())
        return this.r.pop();
    return undefined;
}

Queue.prototype.front = function()
{
    if (this.r.empty())
    {
        while(!this.l.empty())
            this.r.push(this.l.pop());
    }
    if (!this.r.empty())
        return this.r.top();
    return undefined;
}

Queue.prototype.size = function()
{
    return l.size() + r.size();
}

Queue.prototype.clear = function()
{
    l.clear();
    r.clear();
}

function Reader()
{
    this.b = [];
    this.c = 0;
}

Reader.prototype.next = function()
{
    if (this.c == this.b.length)
    {
        this.c = 0;
        this.b = readline().split(' ');
    }
    return this.b[this.c++];
}

function Writer()
{
    this.l = [""];
    this.c = 0;
}

Writer.prototype.print = function(s)
{
    this.l[this.c] += s;
}

Writer.prototype.println = function(s)
{
    if (s == undefined)
        s = "";
    this.l[this.c] += s;
    this.l.push("");
    ++this.c;
}

Writer.prototype.flush = function()
{
    print(this.l.join('\n'));
    this.l = [];
}

Math.cmp = function(a, b)
{
    return (+a > +b)-(+a < +b);
}

function Treap(cmp)
{
    if (typeof cmp == "undefined")
        this.cmp = function(a,b){return ((a>b)-(a<b));};
    this.cmp = cmp;
    this._root = null;
}

Treap.Node = function(k, v, p, l, r)
{
    this.k = k;
    this.v = v;
    this.p = p;
    this.l = l;
    this.r = r;
    this.size = 1;
}

Treap.prototype._update = function(n)
{
    if (n)
        n.size = 1 + (n.l?n.l.size:0) + (n.r?n.r.size:0);
}

Treap.prototype._sizeOf = function(n)
{
    return (n?n.size:0);
}

Treap.prototype._split = function(t, k)
{
    if (!t)
        return {l : null, r : null};
    else
    {
        if (this.cmp(k, t.k) < 0)
        {
            var tmp = this._split(t.l, k);
            t.l = tmp.r;
            this._update(t);
            return {l: tmp.l, r: t};
        }
        var tmp = this._split(t.r, k);
        t.r = tmp.l;
        this._update(t);
        return {l : t, r : tmp.r};
    }
}

Treap.prototype._insert = function(t, n)
{
    if (!t)
        return n;
    if (n.p > t.p)
    {
        var tmp = this._split(t, n.k);
        n.l = tmp.l;
        n.r = tmp.r;
        this._update(n);
        return n;
    }
    if (this.cmp(n.k, t.k) <= 0)
        t.l = this._insert(t.l, n);
    else
        t.r = this._insert(t.r, n);
    this._update(t);
    return t;
}

Treap.prototype.nth = function(n)
{
    var cur = this._root;
    var lSize = 0;
    while(cur)
    {
        lSize = this._sizeOf(cur.l);
        if (lSize == n)
            return {k: cur.k, v: cur.v};
        cur = lSize > n ? cur.l : cur.r;
        if (lSize < n)
            n -= lSize + 1;
    }
    return null;
}

Treap.prototype._forEach = function(t, cb)
{
    if (t.l)
        this._forEach(t.l, cb);
    cb(t.k, t.v);
    if (t.r)
        this._forEach(t.r, cb);
}

Treap.prototype.forEach = function(cb)
{
    if (this._root)
        this._forEach(this._root, cb);
}

Treap.prototype.insert = function(k, v)
{
    this._root = this._insert(this._root, new Treap.Node(k, v, Math.random()+Math.random(), null, null));
}

Treap.prototype._merge = function(l, r)
{
    if (!l)
    {
        this._update(r);
        return r;
    }
    if (!r)
    {
        this._update(l);
        return l;
    }
    if (l.p > r.p)
    {
        l.r = this._merge(l.r, r)
        this._update(l);
        return l;
    }
    r.l = this._merge(l, r.l);
    this._update(r);
    return r;
}

Treap.prototype._erase = function(t, k)
{
    if (t.k == k)
    {
        var tmp = this._merge(t.l, t.r);
        this._update(tmp);
        return tmp;
    }
    if (this.cmp(k, t.k) <= 0)
        if (t.l)
            t.l = this._erase(t.l, k);
    else
        if (t.r)
            t.r = this._erase(t.r, k);
    this._update(t);
    return t;
}

Treap.prototype.erase = function(k)
{
    if (this._root)
        this._root = this._erase(this._root, k);
}

Treap.prototype._get = function(t, k)
{
    if (!t)
        return undefined;
    if (this.cmp(k, t.k) == 0)
        return t.v;
    if (this.cmp(k, t.k) < 0)
        return this._get(t.l, k);
    return this._get(t.r, k);
}

Treap.prototype.get = function(k)
{
    if (this._root)
        return this._get(this._root, k);
    return undefined;
}

Treap.prototype.clear = function()
{
    this._root = null;
}

Treap.prototype.size = function()
{
    return (this._root?this._root.size:0);
}

function cmp(a, b)
{
    if (a < b)
        return -1;
    if (a > b)
        return 1;
    return 0;
}

function main(cin, cout)
{
    var n = +cin.next();
    var treap = new Treap(cmp);
    treap.insert("polycarp", 1);
    for(var i = 0; i < n; ++i)
    {
        var s = cin.next().toLowerCase();
        cin.next();
        var t = cin.next().toLowerCase();
        treap.insert(s, treap.get(t) + 1);
    }
    var ans = 0;
    treap.forEach(function(k, v){ans=Math.max(ans,v);});
    cout.print(ans);
}

var cin = new Reader();
var cout = new Writer();

main(cin, cout);

cout.flush();

var n = parseInt(readline());
var mapa = new Array();
mapa['Polycarp'.toUpperCase()] = 1;
var rs = 0;
for (var i = 0; i < n; ++i) {
    var s = readline().split(' ');
    mapa[s[0].toUpperCase()] = mapa[s[2].toUpperCase()] + 1;
    if (mapa[s[0].toUpperCase()] > rs) {
        rs = mapa[s[0].toUpperCase()];
    }
}
print(rs);

"use strict";
var i = 0,
	depth = {
		'polycarp': 1
	},
	max = 0,
	pair,
	cur,
	n = +readline();

for (; i<n; i++) {
	pair = readline().split(' reposted ');
	cur = depth[pair[1].toLowerCase()] + 1;
	depth[pair[0].toLowerCase()] = cur;
	cur > max && (max = cur);
}
print(max);

var count = readline();
var chain = {polycarp: []};

for(var i = 0; i < count; i++) {
  var names = readline()
  .split(' reposted ')
  .map(function(v) { return v.toLowerCase() });

  if(!chain[names[0]]) chain[names[0]] = chain[names[1]].concat();
  chain[names[0]].push(names[1]);
}

var max = 0;
for(var key in chain) {
  var cur = chain[key].length + 1;
  if(max < cur) max = cur;
}

print(max);

'use strict';

// const rlLib = require('readline');

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

// let inputs = [];
// let allCount = 1;
// let currentLine = 0;

// rl.on('line', (input) => {
//  inputs.push(input);
//  if (inputs.length === 1) {
//     allCount = +inputs[0] + 1;
//  }
//  if (inputs.length >= allCount) {
//      main();

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

// function readline() {
//  return inputs[currentLine++];
// }
// function print() {
//  console.log.apply(this, arguments);
// }

// /**
//  * @param {number} s
//  * @param {number[]} nums
//  * @return {number}
//  */
//  function main() {
    var count = +readline();
    // parent => children
    var tree = {'polycarp': []};

    for (var i = 0; i < count; i++) {
        var str = readline().split(' ').map(x => x.toLowerCase());

        tree[str[2]].push(str[0]);

        if (!tree[str[0]]) {
            tree[str[0]] = [];
        }
    }

    // пройдем поиском в ширину и посчитаем

    var st = [];
    var set = new Set();
    var res = 1;

    st.push(['polycarp', 1]);
    set.add('polycarp');

    while (st.length) {
        var data = st.pop();
        var name = data[0];
        var count = data[1];

        if (res < count) {
            res = count;
        }

        tree[name].forEach(function(name) {
            if (!set.has(name)) {
                st.push([name, count + 1]);
                set.add(name);
            }
        });
    }

    print(res);
// };

var i = 0,
    repostsCount = 0,
    maxDeep = 1,
    users = {
        'polycarp': {
            reposts: [],
            repostedFrom: false,
            deep: maxDeep,
        },
    };

var repostsCount = parseInt(readline());

function lower(string) {
    return string.toLowerCase();
}

while(i < repostsCount) {
        var _ = readline().split(' reposted ').map(lower),
            destination = _[0], source = _[1];
        users[source].reposts.push(destination);
        destinationDeep = users[source].deep + 1;
        if(maxDeep < destinationDeep) {
            maxDeep = destinationDeep;
        }
        users[destination] = {
            reposts: [],
            repostedFrom: source,
            deep: users[source].deep + 1,
        }
    i++;
}

    print(maxDeep);

var lengths = [];

function callback(arr, name2, i, d) {
	var indOf = true;

	for (; i < arr.length; i++) {
		if (arr[i][1] === name2) {
			callback(arr, arr[i][0], i + 1, d + 1);
			indOf = false;
		}
	}

	if (indOf) {
		lengths.push(d);
	}
}

function maxLengthOfChain() {
	var n = readline();
    var arr = [];

    for (var i = 0; i < n; i++) {
        arr.push(readline().toLowerCase().split(' reposted '));
    }

	callback(arr, 'polycarp', 0, 1);

	return Math.max(...lengths);
}

print(maxLengthOfChain());

Tree.prototype.traverseDF = function(callback) {

	// это рекурсивная и мгновенно вызываемая функция
	(function recurse(currentNode) {
		// шаг 2
		for (var i = 0, length = currentNode.children.length; i < length; i++) {
			// шаг 3
			recurse(currentNode.children[i]);
		}

		// шаг 4
		callback(currentNode);

		// шаг 1
	})(this._root);
};

Tree.prototype.add = function(data, toData, traversal) {
	var child = new Node(data),
	parent = null,
	callback = function(node) {
		if (node.data === toData) {
			parent = node;
		}
	};

	this.contains(callback, traversal);

	if (parent) {
		parent.children.push(child);
		child.parent = parent;
	} else {
		throw new Error('Cannot add node to a non-existent parent.');
	}
};

Tree.prototype.contains = function(callback, traversal) {
	traversal.call(this, callback);
};

function length(node, startLength) {
	if (node.parent === null) {
		return startLength;
	}
	return length(node.parent, startLength + 1);
}

function maxLengthOfChain() {

    var n = readline();
    var arr = [];
    var line;

    while (line = readline()) {
        arr.push(line);
    }

// 	var s = str.substring(2).toLowerCase();
// 	var arr = s.split('\n');
	var tree = new Tree('polycarp');

	arr.forEach(function(elem) {
		var names = elem.toLowerCase().split(' reposted ');

		tree.add(names[0], names[1], tree.traverseDF);
	});

	var maxLength = 0;
	tree.contains(function(node) {
		var l = length(node, 1);
		if (maxLength < l) {
			maxLength = l;
		}
	}, tree.traverseDF);

	print(maxLength);
}

maxLengthOfChain();

function Node(data) {
	this.data = data;
	this.parent = null;
	this.children = [];
}

function Tree(data) {
	var node = new Node(data);
	this._root = node;
}

var lengths = [];

function callback(arr, name2, i, d) {
	var indOf = true;

	for (; i < arr.length; i++) {
		if (arr[i][1] === name2) {
			callback(arr, arr[i][0], i + 1, d + 1);
			indOf = false;
		}
	}

	if (indOf) {
		lengths.push(d);
	}
}

function maxLengthOfChain() {
	var n = readline();
    var arr = [];
    var line;

    while (line = readline()) {
        arr.push(line.toLowerCase().split(' reposted '));
    }

	callback(arr, 'polycarp', 0, 1);

	return Math.max(...lengths);
}

print(maxLengthOfChain());

var n = readline();
var reposts = {
	polycarp: 1
};
var max = 1;
for (var i=0; i < n; i++) {
	var str = readline().toLowerCase().split(' ');
	var newValue = reposts[str[2]] + 1;
	reposts[str[0]] = newValue;
	if (newValue > max) {
		max = newValue;
	}
}
print(max);

function parseRepost(repostString) {
    var splitNames = repostString.split(" reposted ");
    var parent = splitNames[1];
    var child = splitNames[0];

    return [parent.toLowerCase(), child.toLowerCase()];
}

function getPopularity() {
    var repostMap = {};
    var max = 0;
    var numPosts = parseInt(readline());
    for(var i = 0; i < numPosts; i++) {
        var users = parseRepost(readline());
        if(!repostMap.hasOwnProperty(users[0])) {
            repostMap[users[0]] = 1;
        }

        repostMap[users[1]] = repostMap[users[0]] + 1;
        if(repostMap[users[1]]> max) {
            max = repostMap[users[1]];
        }
    }
    return max;
}

print(getPopularity());

var l = +readline();
var chainLengths = {}

chainLengths["polycarp"] = 1
var maxLen = 0

for (var i = 0; i < l; i++) {
    var rec = readline().split(' reposted ');
    var a = rec[0].toLowerCase()
    var b = rec[1].toLowerCase()
    chainLengths[a] = chainLengths[b] + 1
    if (chainLengths[a] > maxLen) {
        maxLen = chainLengths[a]
    }
}

print(maxLen);

// #################################################################### //
/**
 * @class UserNode will constitute the nodes of our repost tree,
 * with each node in the tree maintaining a count of the longest
 * chain perturbing from this node, and a reference to its parent
 * node.
 *
 * NOTE: the popularity attribute of the node or the popularity of
 * of this node's subtree refers to the length of the longest chain
 * perturbing from this node, with it as root.
 */
var UserNode = function(name){
  // Method to append a child node to children list.
  this.appendChild = function(childName){
    var newChild = new UserNode(childName.toLowerCase());
      newChild.parent = self;
    self.children.push(newChild);
  }
  // Method to get reference to the last child of this node.
  this.getLastChild = function(){
    return self.children[self.children.length-1];
  }
  // Method to determine popularity of this nodes's subtree.
  this.computePopularity = function(){
    var maxPopularity = 0;
    for(var i = 0;i<self.children.length;++i){
      var child = self.children[i];
      maxPopularity = Math.max(maxPopularity,child.popularity);
    }
    self.popularity = maxPopularity+1;
  }
  // For storing children nodes and the one parent node.
  this.children = []; this.parent = null;
  // For storing popularity of node's subtree and username.
  this.popularity = 0; this.username = name;
  // For storing reference to this node.
  var self = this;
};
// #################################################################### //
// Creating root node for polycarp.
var root = new UserNode('polycarp');
// Storing references to nodes of posters at any given moment.
var posters = {'polycarp':root};
// For storing posts collected from codeforces.
var postCount = parseInt(readline());
// Method to update popularity for tree after every insertion.
function updatePopularity(newUser){
  var currNode = newUser;
  while(currNode !== null){
    currNode.computePopularity();
    currNode = currNode.parent;
  }
}
// Inserting nodes into tree.
for(var j = 0;j<postCount;++j){
  // Parsing post.
  var post = readline().split(' reposted ');
  var poster = post[0].toLowerCase();
  var postee = post[1].toLowerCase();
  // Appending Child.
  posters[postee].appendChild(poster);
  // Adding to Hash-Map.
  var lastChild = posters[postee].getLastChild();
  posters[poster] = lastChild;
  // Updating popularity.
  updatePopularity(lastChild);
}
// Solution.
print(root.popularity);
// #################################################################### //

// #################################################################### //
/**
 * @class UserNode will constitute the nodes of our repost tree,
 * with each node in the tree maintaining a count of the longest
 * chain perturbing from this node, and a reference to its parent
 * node.
 *
 * NOTE: the popularity attribute of the node or the popularity of
 * of this node's subtree refers to the length of the longest chain
 * perturbing from this node, with it as root.
 *
 * @authour atifcppprogrammer
 */
var UserNode = function(name){
  // Method to append a child node to children list.
  this.appendChild = function(childName){
    var newChild = new UserNode(childName.toLowerCase());
      newChild.parent = self;
    self.children.push(newChild);
  };
  // Method to get reference to the last child of this node.
  this.getLastChild = function(){
    return self.children[self.children.length-1];
  };
  // Method to determine popularity of this nodes's subtree.
  this.computePopularity = function(){
    var maxPopularity = 0;
    for(var i = 0;i<self.children.length;++i){
      var child = self.children[i];
      maxPopularity = Math.max(maxPopularity,child.popularity);
    }
    self.popularity = maxPopularity+1;
  };
  // For storing children nodes and the one parent node.
  this.children = []; this.parent = null;
  // For storing popularity of node's subtree and username.
  this.popularity = 0; this.username = name;
  // For storing reference to this node.
  var self = this;
};
// #################################################################### //
/**
 * @namespace PostTree uses the above UserNode class to construct an
 * M-Tree datastructure, equipped with methods for tree insertion and
 * and updating popularity of the joke i.e. the length of the longest
 * chain in this tree.
 *
 * NOTE: The tree is intialized with a root node for polycarp since
 * polycarp was the original poster of the joke.
 *
 * @authour atifcppprogrammer
 */
var PostTree = {
  // Method to insert node into tree.
  insertNode:function(){
    var post = readline().split(' reposted ').map(function(e){
      return e.toLowerCase();
    });
    this.posters[post[1]].appendChild(post[0]);
    var lastChild = this.posters[post[1]].getLastChild();
      this.posters[post[0]] = lastChild;
    return lastChild;
  },
  // Method to update popularity of node tree after insertion.
  updatePopularity:function(newUser){
    var currNode = newUser;
    for(;currNode !== null;currNode = currNode.parent){
      currNode.computePopularity();
    }
  },
  // Storing references to nodes of posters at any given moment.
  posters:{},
  // Stores root of the tree.
  root: null,
};
// #################################################################### //
PostTree.root = new UserNode('polycarp');
PostTree.posters.polycarp = PostTree.root;
// Getting node count and starting tree insertion.
var postCount = parseInt(readline());
for(var j = 0;j<postCount;++j){
    PostTree.updatePopularity(PostTree.insertNode());
}
// Solution.
print(PostTree.root.popularity);
// #################################################################### //

var max = 1;
var posts = {
    polycarp: 1
};
for (var i = parseInt(readline()); i > 0; i--) {
    var line = readline().split(" ");
    var len = posts[line[2].toLowerCase()] + 1;
    posts[line[0].toLowerCase()] = len;
    if (max < len) max = len;
}
print(max);

"use strict";
var i = 0,
	depth = {
		'polycarp': 0
	},
	max = 0,
	pair,
	cur,
	n = +readline();

for (; i<n; i++) {
	pair = readline().split(' reposted ');
	cur = depth[pair[1].toLowerCase()] + 1;
	depth[pair[0].toLowerCase()] = cur;
	cur > max && (max = cur);
}
print(max);

var count = readline();
var chain = {};

for(var i = 0; i < count; i++) {
    var names = readline()
    .split(' reposted ')
    .map(function(v) { return v.toLowerCase() });

    if(!chain[names[0]]) chain[names[0]] = chain[names[1]] || [];
    chain[names[0]].push(names[1]);
}

var max = 0;
for(var key in chain) {
    var cur = chain[key].length + 1;
    if(max < cur) max = cur;
}

print(max);

function parseRepost(repostString) {
    var splitNames = repostString.split(" reposted ");
    var parent = splitNames[1];
    var child = splitNames[0];

    return [parent.toLowerCase(), child.toLowerCase()];
}

function getPopularity() {
    var repostMap = {};
    var max = 0;
    var numPosts = parseInt(readline());
    for(var i = 0; i < numPosts; i++) {
        var users = parseRepost(readline());
        if(!repostMap.hasOwnProperty(users[0])) {
            repostMap[users[0]] = 0;
        }

        repostMap[users[1]] = repostMap[users[0]] + 1;
        if(repostMap[users[1]]> max) {
            max = repostMap[users[1]];
        }
    }
    return max;
}

print(getPopularity());