Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>

constexpr int nil = -1;

class ILcs
{
public:
    using vvi = std::vector<std::vector<int>>;
    virtual int lcs() = 0;
    virtual int lcs(int i1, int i2) = 0;
};

class LcsLoop : public ILcs
{
public:

    LcsLoop(std::string str1, std::string str2)
        : str1 {" " + str1}
        , str2 {" " + str2}
        , dp {vvi(str1.size() + 1, std::vector<int>(str2.size() + 1))}
    {
        CalcLcs();
    }

    int lcs() override
    {
        return lcs(str1.size() - 1, str2.size() - 1);
    }

    int lcs(int i1, int i2) override
    {
        return dp[i1][i2];
    }

private:

    std::string str1;
    std::string str2;
    vvi         dp;
    
    void CalcLcs()
    {
        for (int i = 0; i < str1.size(); ++i)
            dp[i][0] = 0;
        for (int i = 0; i < str2.size(); ++i)
            dp[0][i] = 0;

        for (int i = 1; i < str1.size(); ++i)
        {
            for (int j = 1; j < str2.size(); ++j)
            {
                if (str1[i] == str2[j])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = std::max(dp[i][j - 1], dp[i - 1][j]);
            }
        }
    }
};

//class LcsReccursion : public ILcs
//{
//public:
//    LcsReccursion(const std::string& str1, const std::string& str2)
//        : str1 {str1}
//        , str2 {str2}
//        , dp {vvi(str1.size(), std::vector<int>(str2.size(), nil))}
//    {}
//
//    int lcs() override
//    {
//        return lcs(str1.size() - 1, str2.size() - 1);
//    }
//
//    int lcs(int i1, int i2) override
//    {
//        if (dp[i1][i2] != nil)
//            return dp[i1][i2];
//
//        if (i1 == 0 || i2 == 0)
//        {
//            if (i1 == 0 && i2 == 0) return dp[i1][i2] = (str1[i1] == str2[i2]);
//            else if (i1 == 0)       return dp[i1][i2] = std::max(lcs(i1, i2 - 1), (str1[i1] == str2[i2] ? 1 : 0));
//            else                    return dp[i1][i2] = std::max(lcs(i1 - 1, i2), (str1[i1] == str2[i2] ? 1 : 0));
//        }
//
//        dp[i1][i2] = std::max(lcs(i1 - 1, i2), lcs(i1, i2 - 1))
//            + (str1[i1] == str2[i2]);
//        return dp[i1][i2];
//    }
//
//private:
//
//    std::string str1;
//    std::string str2;
//    vvi         dp;
//};

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    string s1, s2;

    for (int i = 0; i < n; ++i)
    {
        cin >> s1 >> s2;
        LcsLoop lcs {s1, s2};
        //LcsReccursion lcs {s1, s2};
        cout << lcs.lcs() << endl;
    }
}

int main()
{
    solve();
    return 0;
}

C++: #include<string>
#include<iostream>

using namespace std;

#define N=1000;
int lcs(string x,string y){
    int c[N+1][N+1];
    int m=x.size();
    int n=y.size();
    int maxl=0;
    x=' '+x;
    y=' '+y;
    for(int i=1;i<=m;i++)    c[i][0]=0;
    for(int j=1;j<=n;j++)    c[0][j]=0;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(x[i]==y[j]){
                c[i][j]=c[i-1][j-1]+1;
            }else{
                c[i][j]=max(c[i-1][j],c[i][j-1]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main(){
    string s1,s2;
    int n;  cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

const int LEN = 1000;

int main()
{
    int n, length;
    cin >> n;

    int c[LEN + 1][LEN + 1];

    string S, T;
    for (int i = 0; i < n; i++)
    {
        cin >> S;
        cin >> T;

        length = S.length() > T.length() ? S.length() : T.length();

        for (int j = 1; j < length+1; j++)
        {
            c[0][j] = c[j][0] = 0;
        }

        for (int j = 0; j < S.length(); j++)
        {
            for (int k = 0; k < T.length(); k++)
            {
                if (S[j] == T[k])
                {
                    c[j+1][k+1] = c[j][k] + 1;
                }
                else
                {
                    c[j+1][k+1] = c[j][k+1] > c[j+1][k] ? c[j][k+1] : c[j+1][k];
                }
            }
        }
        cout << c[S.length()][T.length()] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;


void solve() {
    string s, t;
    cin >> s >> t;
    vector<vector<int> > dp(s.size(), vector<int>(t.size()));
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < t.size(); j++) {
            dp[i][j] = (i < 1 || j < 1 ? 0 : dp[i-1][j-1]) + (s[i] == t[j] ? 1 : 0);
            int a = (i < 1 ? 0 : dp[i-1][j]);
            int b = (j < 1 ? 0 : dp[i][j-1]);
            dp[i][j] = max(dp[i][j], max(a, b));
        }
    }

    cout << dp[s.size()-1][t.size()-1] << endl;
}

int main() {
    int n;
    cin >> n;
    while (n--) solve();

    return 0;
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1001][1001];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<=1000;j++){
      for(int k=0;k<=1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<=x.size();j++){
      for(int k=0;k<=y.size();k++){
	if(x[j-1]==y[k-1]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(v) (v).begin(), (v).end()
#ifdef DBGPRT
#define dbg(n) cerr << "//" << #n << ":" << n <<endl
#else
#define dbg(n) ;
#endif
typedef pair<ll, ll> LLARR;
#define INF 999999999
const double PI = 3.1415926535897932384626433832795;

ll lcs(string s1, string s2)
{
	int m = len(s1);
	int n = len(s2);
	s1 = ' '+s1;
	s2 = ' '+s2;
	int (*mat)[1001];
	mat = new int[1001][1001];
	REP(i, m)
	{
		mat[i][0] = 0;
	}
	REP(i, n)
	{
		mat[0][i] = 0;
	}
	int maxx = 0;
	FOR(i, 1, m+1)
	{
		FOR(j, 1, n+1)
		{
			if (s1[i] == s2[j])
				mat[i][j] = mat[i-1][j-1] + 1;
			else
				mat[i][j] = max(mat[i][j-1], mat[i-1][j]);
			maxx = max(maxx, mat[i][j]);
		}
	}
	delete[] mat;
	return maxx;
}
int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	REP(i, n)
	{
		string s1;
		cin >> s1;
		string s2;
		cin >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}