SP14905 ZIGZAG2 - Zig when you zag

Description

We say that a sequence of numbers x(1),x(2),...,x(k) is zigzag if no three of its consecutive elements create a nonincreasing or nondecreasing sequence. More precisely, for all i=1,2,...,k-2 either x(i+1)x(i),x(i+2). You are given two sequences of numbers a(1),a(2),...,a(n) and b(1),b(2),...,b(m). The problem is to compute the length of their longest common zigzag subsequence. In other words, you're going to delete elements from the two sequences so that they are equal, and so that they're a zigzag sequence. If the minimum number of elements required to do this is k then your answer is m+n-2k.

Input Format

There is a single integer t (t

Output Format

For each test case output one line containing the length of the longest common zigzag subsequence of a and b.