Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func f() {
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	dp := make([][]int, lenS+1)
	for i:=0; i<lenS+1; i++ {
		dp[i] = make([]int, lenT+1)
	}
	for i:=1; i<lenS+1; i++ {
		for j:=1; j<lenT+1; j++ {
			if S[i-1] == T[j-1] {
				dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
		}
	}
	fmt.Println(dp[lenS][lenT])
}

func main(){
	var n int 
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		f()
	}
}

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

using namespace std;

string  X,Y;
int q;

int do_dp(int ax,int ay){
  int dp[ax][ay] = {};
  
  for(int i = 0;i < ax;i++){
    if(X[i] == Y[0]) dp[i][0]++;
  }
  
  for(int j = 1;j < ay;j++){
    if(X[0] == Y[j]) dp[0][j]++;
  }

  for(int i = 1;i < ax;i++){
    for(int j = 1;j < ay;j++){
      if(X[i] == Y[j]){
	dp[i][j] = dp[i - 1][j - 1] + 1;
      }else{
	dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]);
      }
    }
  }
  
  /*for(int i = 0;i < ax;i++){
    for(int j = 0;j < ay;j++){
      cout << dp[i][j] << " ";
    }
    cout << '\n';
    }*/
  
  return dp[ax - 1][ay - 1];
}

int main(){
  string str;
  
  cin >> q;
  
  int A[q];
  
  for(int i = 0;i < q;i++){
    cin >> X;
    cin >> Y;
    A[i] = do_dp(X.size(),Y.size());
  }
  
  for(int i = 0;i < q;i++){
    cout << A[i] << '\n';
  }
  
  return 0;
}

C++: #include<iostream>
#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 = 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 ( i = 0; i < n; i++ ){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << sndl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <map>
#define INFTY (1<<30)
#define MAX 100000000

using namespace std;

int lcs(string x, string y) {
	int m = x.size(), n = y.size();
	int c[1000+1][1000+1];
	x = ' ' + x; y = ' ' + y;
	for( int i = 0; i <= m; i++) c[i][0] = 0;
	for( int j = 0; 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];
			else if(c[i-1][j] >= c[i][j-1]) c[i][j] = c[i-1][j];
			else c[i][j] = c[i][j-1];
		}
	}
	return c[m][n];
}

int main(){
	int q;
	cin >> q;
	for( int i = 0; i < q; i++) {
		string s, t;
		cin >> s >> t;
		cout << lcs(s, t) <<endl;
	}

	return 0;
}

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

using namespace std;

int lcs(string a, string b) {
  int longest_length = 0;
  int m = a.size(), n = b.size();
  int table[m+1][n+1];
  for(int i = 0; i < m+1; i++) table[i][0] = 0; 
  for(int i = 0; i < n+1; i++) table[0][i] = 0; 
  for(int i = 1; i < m+1; i++) {
    for(int j = 1; j < n+1; j++) {
      if(a[i] == b[j]){
        table[i][j] = table[i-1][j-1] + 1;
      } else {
        table[i][j] = max(table[i-1][j], table[i][j-1]);
      } 
      longest_length = max(longest_length, table[i][j]);
    }
  }
  return longest_length;
}

int main() {
  int n = 0;
  cin >> n;
  string a, b;
  for(int i = 0; i < n; i++) {
    cin >> a >> b;
    int l = lcs(a, b);
    cout << l << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define rng(i, l, r) for (size_t i = (l); i < (r); ++i)
#define rep(i, n) rng(i, 0, n)
#define gnr(i, l, r) for (size_t i = (r)-1; i >= (l); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(obj) (obj).begin(), (obj).end()    //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend() //...,3,2,1

using namespace std;
const int INF = 1e18;

struct edge
{
    int to;
    int cost;
};
using Graph = vector<vector<edge>>;

#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
Graph G;

int lcs(string s1, string s2)
{
    int num;
    num = 0;
    int m = s1.size();
    int n = s2.size();

    s1 = ' ' + s1;
    s2 = ' ' + s2;
    int dp[1100][1100];
    rep(i, 1100)
    {
        dp[i][0] = 0;
        dp[0][i] = 0;
    }
    for (int im = 1; im <= m; im++)
    {
        for (int jn = 1; jn <= n; jn++)
        {
            if (s1[im] == s2[jn])
            {
                dp[im][jn] = dp[im - 1][jn - 1] + 1;
            }
            else
            {
                dp[im][jn] = max(dp[im - 1][jn], dp[im][jn - 1]);
            }

            num = max(num, dp[im][jn]);
        }
    }

    return num;
}

void solve()
{

    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> ans;
    for (int i = 0; i < n; i++)
    {
        string s1, s2;
        cin >> s1 >> s2;
        ans.push_back(lcs(s1, s2));
    }

    rep(i, n) cout << ans[i] << endl;
}

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

C++: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[1010][1010];
int main(){
  int n;
  char x[1001],y[1001];

  //load n;
  cin >>n;
  for(int i=0;i<n;i++){
    //load string
    scanf("%s",x);
    scanf("%s",y);
    //dp
    for(int j=0;j<strlen(x);j++){
      for(int k=0;k<strlen(y);k++){
	if(x[j]==y[k]){
	  dp[j+1][k+1]=dp[j][k]+1;
	}

	else{
	  if(dp[j][k+1]>dp[j+1][k]){
	    dp[j+1][k+1]=dp[j][k+1];
	  }

	  else{
	    dp[j+1][k+1]=dp[j+1][k];
	  }


	}
      }
    }
    cout <<dp[strlen(x)][strlen(y)]<<endl;
  }



  return 0;
}