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<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }

  cout<<N[X.size()][Y.size()];
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }

}

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

#ifdef DEBUG

#define printd printf

#else

#define printd(...)

#endif


int lcs(std::string &x, std::string &y) {
    static int dp[1002][1002];

    for (int i = 0; i < x.length() + 2; i++) {
        for (int j = 0; j < y.length() + 2; j++) {
            dp[i][j] = 0;
        }
    }

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

    #ifdef DEBUG
    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            std::cout << dp[i][j] << ", ";
        }
        std::cout << std::endl;
    }
    #endif

    return dp[x.length()][y.length()];
}

int main() {
    int q;
    std::cin >> q;

    for (int qi = 0; qi < q; qi++) {
        std::string x, y;
        std::cin >> x >> y;
        std::cout << lcs(x, y) << std::endl;
    }

    return 0;
}

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(), std::vector<int>(str2.size()))}
    {
        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)
        {
            for (int j = 0; j < str2.size(); ++j)
            {
                if (i == 0 || j == 0)
                {
                    dp[i][j] = (str1[i] == str2[j]);

                    if (i == 0 && j == 0)
                        continue;

                    if (i == 0)
                        dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
                    else
                        dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
                }
                else
                    dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]) + (str1[i] == str2[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() << ((i != n - 1) ? "\n" : "");
    }
}

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

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i,x) for(int i=0;i<x;++i)
#define rrep(i,x) for(int i=x-1;i>=0;--i)

int dp[1010][1010];

int main()
{
    int q; cin >> q;

    rep(_, q) {
        string a,b; cin >> a>>b;

        int n = a.size(), m = b.size();

        rep(i, n) {
            rep(j, m) {
                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]);
                }
            }
        }

        cout << dp[n][m] << endl;
    }
}