Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


int CalcLCS(string X, string Y){
  int c[1001][1001];
  int m = SZ(X);
  int n = SZ(Y);
  REP1(i,m) c[i][0]=0;
  REP1(j,n) c[0][j]=0;

  REP0(i,m){
    REP0(j,n){
      if (X[i]==Y[j]) {
        c[i+1][j+1] = c[i][j] + 1;
      } else {
        c[i+1][j+1] = max(c[i][j+1], c[i+1][j]);
      }
    }
  }
  return c[m][n];
}


int main(int argc, char const *argv[]) {
  int q; std::cin >> q;
  string X,Y;

  REP0(i,q){
    std::cin >> X >> Y;
    std::cout << CalcLCS(X,Y) << '\n';
  }

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
 
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A); 
strB = strlen(B);

    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
 
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
    using namespace std;
int lcs(string x,string y){
    int c[1010][1010];
    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(){
    int q;
    string x;
    string y;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        cin>>x>>y;
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a,b)   (a >> b) & 1
#define BitSet(a,b)       a |= (1 << b)
#define BitunSet(a,b)    a &= ~(1 << b)

int compare_int(const void* a, const void* b) { return *(int*) a - *(int*) b; }

int main() {
	int n, m, a, dp[102][102] = { 0 };
	char s[1001], t[1001];
	scanf("%d", &a);
	for (int k = 0; a > k; k++) {
		scanf("%s %s", s, t);
		n = strlen(s);
		m = strlen(t);
		for (int i = 1; n >= i; i++) {
			for (int j = 1; m >= j; j++) {
				if (s[i - 1] == t[j - 1])dp[i][j] = dp[i - 1][j - 1] + 1;
				else dp[i][j] = __max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		printf("%d\n", dp[n][m]);
	}
	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.length();
    int n=Y.length();
    int maxlcs=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]);
            }
            maxlcs=max(maxlcs,c[i][j]);
        }
    }
    return maxlcs;
}

int main(){
    string a1,a2;
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> a1 >> a2;
        cout << lcs(a1,a2) << endl;
    }
    return 0;
}

C++: #include <stdio.h>

int t[1001][1001];
 
int main(){
  char X[1000],Y[1000];
  int i, j, n, x, y, k;
 
  scanf("%d",&n);
 
  for(k=0; k<n; k++){
    scanf("%s%s",X+1,Y+1);
    x=strlen(X+1);
    y=strlen(Y+1);
 
    for(i=1; i<=x; i++)
      for(j=0; j<=y;j++)
    t[i][j]=0;
 
    for(i=1;i<=x;i++){
      for(j=1;j<=y;j++){
    if(X[i] == Y[j])  t[i][j]=t[i-1][j-1]+1;
    else if(t[i-1][j]>=t[i][j-1]) t[i][j]=t[i-1][j];
    else t[i][j]=t[i][j-1];
      }
    }
    printf("%d\n", t[i-1][j-1]);
  }
  return 0;
}