Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long int ll;
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string a,b;
        cin>>a>>b;
        int al=a.length();
        int bl=b.length();
        int dp[1005][1005];
        memset(dp,0,sizeof(dp));
       // for(int i=0;i<=al;i++)
           // dp[i][0]=0;
       // for(int i=0;i<=bl;i++)
            //dp[0][i]=0;
        for(int i=1;i<=al;i++)
            for(int j=1;j<=bl;j++)
            {
                if(a[i-1]==b[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=std::max(dp[i-1][j],dp[i][j-1]);
                    
            }
        cout<<dp[al][bl]<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<string>
#include<algorithm>
using namespase 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 = 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;

int main() {
  int q; cin >> q;
  rep(z,q) {
    string s,t; cin >> s >> t;
    int N = s.size();
    int M = t.size();
    s = ' ' + s;
    t = ' ' + t;

    vector<vector<int>> dp(N+1, vector<int>(M+1));

    rep(i,N+1) {
      rep(j,M+1) {
        if(i == 0 || j == 0) continue;
        if(s[i] == t[j]) dp[i][j] = dp[i-1][j-1] + 1;
        else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
      }
    }

    cout << dp[N][M] << endl;
  }
}

C++: /* test */
#include <bits/stdc++.h>
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 k=1;k<=n;k++) c[0][k] = 0;
 
    for(int i=1;i<=m;i++){
        for(int k=1;k<=n;k++){
            if(X[i]==Y[k]){
                c[i][k] = c[i-1][k-1] + 1;
            } else {
                c[i][k] = max(c[i-1][k],c[i][k-1]);
            }
            maxl = max(maxl,c[i][k]);
        }
    }
 
    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<algorithm>
#include<vector>
#include<string>

const int s=1001;

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int q;
	static unsigned int dp[s][s] = {0};
	
	dp[0][0] = 0; dp[0][1] = 0; dp[1][0] = 0;

	cin >> q;
	for(int k=0; k < q; k++){
		string X,Y;
		cin >> X >> Y;
		int i,j;
		for(i=0;i<X.size();i++){
			for(j=0;j<Y.size();j++){
				if(X[i] == Y[j]) dp[i+1][j+1] = dp[i][j] + 1;
				else dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
			}
		}
		cout << dp[i][j] << "\n";
	}
	return 0;
}

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

using namespace std;

int N=1000;

int LCS(string x,string y){
	int c[N+1][N+1],LCSlen=0;
	int m=x.size();
	int n=y.size();
	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][j-1],c[i-1][j]);
			LCSlen=max(LCSlen,c[i][j]);
		}
	}
	return LCSlen;
}

int main(){
	string x,y;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>x;
		cin>>y;
		printf("%d\n",LCS(x,y));
	}
}

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

int lcs(string X, string Y){
  int c[1001][1001];
  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;i <= 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 <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1010;

int c[N][N];
int lcs(char x[],char y[]){

    int lenx = strlen(x);
    int leny = strlen(y);

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

    for(int i=1;i<=lenx;i++){
        for(int j=1;j<=leny;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]);
        }
    }
    return c[lenx][leny];
}
int main(){
    int q;
    char x[N],y[N];
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        //gets(x+1);
        //gets(y+1);
        scanf("%s%s",x,y);
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

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

using namespace std;

string s[2];
int q;
int dp[1000][1000];

int solve(int x,int y) {
	if(x == -1 || y == -1) return 0;
	if(dp[x][y] == -1) {
		if(s[0][x] == s[1][y]) dp[x][y] = solve(x-1,y-1) + 1;
		else dp[x][y] = max(solve(x-1,y),solve(x,y-1));
	}
	return dp[x][y];
}

int main() {
	cin >> q;
	for(int i = 0;i < q;i++) {
		for(int i = 0;i < 1000;i++)for(int j = 0;j < 1000;j++)dp[i][j] = -1;
		cin >> s[0] >> s[1];
		cout << solve(s[0].size()-1,s[1].size()-1) << endl;
	}
}