Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	n := scanInt(sc)
	results := []int{}
	for i := 0; i < n; i++ {
		results = append(results, resolveInstance())
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func resolveInstance() int {
	x, y := scanString(sc), scanString(sc)
	lx, ly := len(x), len(y)
	dp := make([][]int, lx+1)
	dp[0] = make([]int, ly+1)
	for i := 1; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
		dp[i][0] = dp[i-1][0]
		for j := 1; j <= ly; j++ {
			dp[i][j] = dp[i-1][j]
			if dp[i][j] < dp[i][j-1] {
				dp[i][j] = dp[i][j-1]
			}
			if x[i-1] == y[j-1] {
				cL := dp[i-1][j-1] + 1
				if dp[i][j] < cL {
					dp[i][j] = cL
				}
			}
		}
	}
	return dp[lx][ly]
}

const (
	remove = 0 + iota
	match
)

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 1005
#define INF 1000000005LL
//#define INF 1e18
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int dp[SIZE][SIZE];

void solve(){
  int n,m;
  string X,Y;
  cin >> X >> Y;
  n = X.size();
  m = Y.size();
  fill(dp[0],dp[0]+SIZE,0);
  rep(i,0,n) rep(j,0,m){
    int a=0,b=0,c=0;
    if(i>0)a = dp[i-1][j];
    if(j>0)b = dp[i][j-1];
    if(i>0 && j>0)c = dp[i-1][j-1];
    dp[i][j] = max({a,b,c+(X[i]==Y[j]?1:0)});
  }
  cout << dp[n-1][m-1] << endl;
}

int main(){

  int q;
  cin >> q;
  rep(i,0,q){
    solve();
  }

  return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;

int dp[1001][1001];
int q;

void solve(){
  char x[1001],y[1001];
  scanf("%s %s",x,y);
  int lx = strlen(x);
  int ly = strlen(y);
  for(int i=0;i<=lx;i++){
    for(int j=0;j<=ly;j++){
      if(i==0||j==0) dp[i][j] = 0;
      else if(x[i]==y[j]) dp[i][j] = dp[i-1][j-1]+1;
      else dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
    }
  }
  printf("%d\n",dp[lx][ly]);
  return;
}


int main(){
  scanf("%d",&q);
  for(int i=0;i<q;i++) solve();
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int 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 = 0; 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 <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int lcs(string s1, string s2) {
    int i, j, ret = 0;
    int n = s1.size();
    int m = s2.size();
    int c[n+1][m+1];
    s1 = ' ' + s1;
    s2 = ' ' + s2;

    for (i = 0; i <= n; i++) c[i][0] = 0;
    for (j = 1; j <= m; j++) c[0][j] = 0;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (s1[i] == s2[j]) {
                c[i][j] = c[i - 1][j - 1] + 1;
            }
            else {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            ret = max(ret, c[i][j]);
        }
    }
    
    return ret;
}

int main(void) {
    int q, i;
    string s1, s2;
    
    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        cin >> s1 >> s2;
        printf("%d\n", lcs(s1, s2));
    }

    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>

#define debug(x) cout << #x << "==" << x << endl;

using namespace std;

//??\???
#define MAX_LEN 1000
int N;
int dp[MAX_LEN + 1][MAX_LEN + 1];

void print_dp_table(int n, int m) {
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cout <<setw(2) << dp[i][j];
    }
    cout << "\n";
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;

  string s1, s2;

  while( cin >> s1 >> s2 ) {

    std::vector<char> a;
    std::vector<char> b;
    for(char& c : s1) {
      a.push_back(c);
    }
    for(char& c : s2) {
      b.push_back(c);
    }


    // dp ????????????????????????
    fill(dp[0], dp[MAX_LEN + 1], 0);

    for( int i = 0; i < (int)a.size(); i++) {
      for( int j = 0; j < (int)b.size(); j++) {
        if( a[i] == b[j] ) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max( dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }

    // print_dp_table(a.size(), b.size());
    cout << dp[a.size()][b.size()] << endl;

  }

  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <limits.h>
#include <bitset>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 1000000007;
const int INF = LLONG_MAX/3;
const int MAX = 510000;
int dp[1100][1100];
signed main(){
    int n;
    cin>>n;
    for(int c=0;c<n;c++){
        string s,t;
        cin>>s>>t;
        int a=s.size(),b=t.size();
        for(int i=0;i<1100;i++){
            for(int j=0;j<1100;j++){
                dp[i][j]=0;
            }
        }
        for(int i=1;i<=a;i++){
            for(int j=1;j<=b;j++){
                if(s[i-1]==t[j-1]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else{
                    dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        cout<<dp[a][b]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>

using namespace std;

int n;
string x,y;

vector<vector<int> > q(1001,vector<int>(1001,-1));

int search(int i,int j){
  int ans;
  if(q[i][j] != -1) return q[i][j];
  if(i==0 || j==0) return 0;
  if( x[i] == y[j] ){
    ans=max( {search(i-1,j-1)+1,search(i-1,j),search(i,j-1)} );
    q[i][j]=ans;
    return ans;
  }
  else{
    ans=max( search(i-1,j),search(i,j-1) );
    q[i][j]=ans;
    return ans;
  }

}


int main(void){

  cin>>n;

  for(int i=0;i<n;i++){
    cin>>x;
    cin>>y;

    for(int ix=0;ix<1001;ix++){
      for(int iy=0;iy<1001;iy++){
        q[ix][iy]=-1;
      }
    }

    search( x.size(),y.size() );
    cout << q[x.size()][y.size()] << endl;
  }



  return 0;
}