Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Longest Common Subsequence

For given two sequences $X$ and $Y$, a sequence $Z$ is a common subsequence of $X$ and $Y$ if $Z$ is a subsequence of both $X$ and $Y$. For example, if $X = \{a,b,c,b,d,a,b\}$ and $Y = \{b,d,c,a,b,a\}$, the sequence $\{b,c,a\}$ is a common subsequence of both $X$ and $Y$. On the other hand, the sequence $\{b,c,a\}$ is not a longest common subsequence (LCS) of $X$ and $Y$, since it has length 3 and the sequence $\{b,c,b,a\}$, which is also common to both $X$ and $Y$, has length 4. The sequence $\{b,c,b,a\}$ is an LCS of $X$ and $Y$, since there is no common subsequence of length 5 or greater.

Write a program which finds the length of LCS of given two sequences $X$ and $Y$. The sequence consists of alphabetical characters.

Input

The input consists of multiple datasets. In the first line, an integer $q$ which is the number of datasets is given. In the following $2 \times q$ lines, each dataset which consists of the two sequences $X$ and $Y$ are given.

Output

For each dataset, print the length of LCS of $X$ and $Y$ in a line.

Constraints

$1 \leq q \leq 150$

$1 \leq$ length of $X$ and $Y$ $\leq 1,000$

$q \leq 20$ if the dataset includes a sequence whose length is more than 100

Sample Input 1

3
abcbdab
bdcaba
abc
abc
abc
bc

Sample Output 1

4
3
2

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Library;
using static Library.Input;

namespace AtCoderTemplate {
    class ProgramE {
        static void Main () {
            int q = NextInt;
            foreach (var item in Enumerable.Range(0, q))
            {
                string X = Line, Y = Line;
                var dp = Enumerable.Repeat(0, X.Length).ToArray();

                for (int i = 0; i < Y.Length; i++)
                {
                    int max = 0;
                    for (int j = 0; j < X.Length; j++)
                    {
                        if (X[j] == Y[i])
                        {
                            int tmp = dp[j];
                            dp[j] = Math.Max(dp[j], max + 1);
                            max = Math.Max(tmp, max);
                        }
                        else
                        {
                            max = Math.Max(dp[j], max);
                        }
                    }
                }

                System.Console.WriteLine(dp.Max());
            }
        }
    }

}

namespace Library {
    class Input {
        static IEnumerator<string> enumerator = new string[] { }.AsEnumerable ().GetEnumerator ();

        public static string Line => Console.ReadLine ();

        public static string[] StrArr => Line.Split (' ');

        public static int NextInt => int.Parse (NextWord ());

        public static long NextLong => long.Parse (NextWord ());

        public static List<int> IntList => StrArr.Select (int.Parse).ToList ();

        public static List<long> LongList => StrArr.Select (long.Parse).ToList ();

        public static IEnumerable<long[]> TakeLine (int N) {
            return Enumerable.Repeat (0, N).Select (_ => Console.ReadLine ().Split (' ').Select (long.Parse).ToArray ());
        }

        public static string NextWord () {
            while (!enumerator.MoveNext ()) {
                enumerator = StrArr.AsEnumerable ().GetEnumerator ();
            }
            return enumerator.Current;
        }
    }
}

namespace Library {
    
    class Modular {
        public const int mod = 1000000007;
        public readonly long value;
        public Modular (long value) { this.value = value; }
        public static implicit operator Modular (long a) {
            var m = a % mod;
            return new Modular ((m < 0) ? m + mod : m);
        }
        public static Modular operator + (Modular a, Modular b) {
            return a.value + b.value;
        }
        public static Modular operator - (Modular a, Modular b) {
            return a.value - b.value;
        }
        public static Modular operator * (Modular a, Modular b) {
            return a.value * b.value;
        }
        public static Modular Pow (Modular a, int n) {
            switch (n) {
                case 0:
                    return 1;
                case 1:
                    return a;
                default:
                    var p = Pow (a, n / 2);
                    return p * p * Pow (a, n % 2);
            }
        }
        public static Modular operator / (Modular a, Modular b) {
            return a * Pow (b, mod - 2);
        }
        private static readonly List<int> facs = new List<int> { 1 };
        private static Modular Fac (int n) {
            for (int i = facs.Count; i <= n; ++i) {
                facs.Add ((int) (Math.BigMul (facs.Last (), i) % mod));
            }
            return facs[n];
        }
        public static Modular Ncr (int n, int r) {
            return (n < r) ? 0 :
                (n == r) ? 1 :
                Fac (n) / (Fac (r) * Fac (n - r));
        }
        public static explicit operator int (Modular a) {
            return (int) a.value;
        }
    }

}
namespace Library {
    class MyMath {
        public static long Gcd (long a, long b) {
            return b == 0 ? a : Gcd (b, a % b);
        }

        public static long Sqrt (long n) {
            if (n < 0) return 0;
            long a = 0, tmp = 0, b = 0;
            for (int i = 62; i >= 0; i -= 2) {
                tmp = (b << 1) + 1 <= (n >> i) ? 1 : 0;
                a = a << 1 | tmp;
                n -= (((b << 1) + 1) * tmp) << i;
                b = (b << 1) + tmp + tmp;
            }
            return a;
        }

        public static long Nck (int n, int k) {
            if (k == 0) return 1;
            if (n == 0) return 0;
            return n * Nck (n - 1, k - 1) / k;
        }
    }
}

namespace Library {
        public class PriorityQueue<T> {

        readonly List<T> _heap = new List<T> ();
        readonly Comparison<T> _comparison;

        public PriorityQueue (Comparison<T> comparison) {
            _comparison = comparison;
        }

        public PriorityQueue () : this (Comparer<T>.Default.Compare) { }

        public PriorityQueue (IComparer<T> comparer) : this (comparer.Compare) { }

        public void Enqueue (T item) {
            _heap.Add (item);

            int childID = _heap.Count - 1;
            int parentID = (childID - 1) / 2;

            while (childID > 0 && _comparison (_heap[parentID], _heap[childID]) > 0) {
                Swap (parentID, childID);
                childID = parentID;
                parentID = (parentID - 1) / 2;
            }
        }

        public T Dequeue () {
            var first = _heap.First ();
            _heap[0] = _heap.Last ();
            _heap.RemoveAt (_heap.Count - 1);

            int parentID = 0;
            int childID = parentID * 2 + 2;
            if (childID >= _heap.Count || _comparison (_heap[childID], _heap[childID - 1]) > 0) childID--;

            while (childID < _heap.Count && _comparison (_heap[parentID], _heap[childID]) > 0) {
                Swap (parentID, childID);
                parentID = childID;
                childID = parentID * 2 + 2;
                if (childID >= _heap.Count || _comparison (_heap[childID], _heap[childID - 1]) > 0) childID--;
            }

            return first;
        }

        void Swap (int parent, int child) {
            var tmp = _heap[parent];
            _heap[parent] = _heap[child];
            _heap[child] = tmp;
        }

        public T Peek () { return _heap[0]; }
        public int Count => _heap.Count;
        public bool Any () { return _heap.Any (); }
        public List<T> Data => _heap;
    }
}

namespace Library {
    
    public class UnionFind {
        public int[] Parents { get; set; }
        public UnionFind (int size) {
            Parents = Enumerable.Repeat (-1, size).ToArray ();
        }

        public int Find (int num) {
            if (Parents[num] < 0) return num;

            Parents[num] = Find (Parents[num]);
            return Parents[num];
        }

        public int Size (int num) {
            return -Parents[Find (num)];
        }

        public bool Same (int a, int b) {
            return Find (a) == Find (b);
        }

        public void Unite (int a, int b) {
            int x = Find (a), y = Find (b);

            if (Size (x) > Size (y)) {
                Parents[x] += Parents[y];
                Parents[y] = x;
            } else {
                Parents[y] += Parents[x];
                Parents[x] = y;
            }
        }
    }
}