Go: package main

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

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

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

    for(i = 1 ; i <= m ; i++ ) {
        for(j = 1 ; j <= n ; j++ ) {
            if (X[i] == Y[j]) {
                c[i][j] = c[i-1][j-1] + 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];
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;
    

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }

    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; //X[0]???????????????????????\
    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>
#include <iostream>
#include <math.h>
using namespace std;

int lcs(string x, string y) {
    int _lcs = 0;
    int x_size = x.size();
    int y_size = y.size();
    int dp[x_size + 1][y_size + 1];
    for (int i=0; i <= x_size; i++) {
        dp[i][0] = 0;
    }

    for (int j=0; j <= y_size; j++) {
        dp[0][j] = 0;
    }

    for (int i=1; i <= x_size; i++) {
        for (int j=1; j <= y_size; j++) {
            if (x[i-1] == y[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
            } else {
                dp[i][j] = max({dp[i][j-1], dp[i-1][j]});
            }
        }
    }

    return dp[x_size][y_size];
}
int main() {
    int q;
    cin >> q;
    for (int i=0; i < q; i++) {
        string x, y;
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }

    return 0;
}

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

int main(){
  
  int q, lcs[1010][1010] = {};
  string a, b;
  
  cin >> q;
  for(int i = 0;i < q;i++){
    cin >> a;
    cin >> b;
    
    for(int r = 0;r < a.size();r++){
      for(int c = 0;c < b.size();c++){
        if(a[r] == b[c]) lcs[r + 1][c + 1] = lcs[r][c] + 1;
        else lcs[r + 1][c + 1] = max(lcs[r + 1][c], lcs[r][c + 1]);
      }
    }
    
    cout << lcs[a.size()][b.size()] << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[1005][1005];

int main()
{
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	while (n-- ){
		memset (dp, 0, sizeof (dp ) );
		string s, t;
		cin >> s >> t;
		int n = s.length();
		int m = t.length();
		rep (j, m ) if (s[0] == t[j] ) dp[0][j] = 1;
		rep (i, n ) if (s[i] == t[0] ) dp[i][0] = 1;

		for (int i = 1; i < n; i++ ){
			for (int j = 1; j < m; j++ ){
				dp[i][j] = max (dp[i][j], dp[i-1][j-1] + (s[i] == t[j] ? 1 : 0 ) );
				dp[i][j] = max (dp[i][j], dp[i-1][j] );
				dp[i][j] = max (dp[i][j], dp[i][j-1] );
			} // end for
		} // end for
/*
		rep (i, n ){
			rep (j, m ){
				printf ("%3d", dp[i][j] );
			} // end rep
			printf ("\n" );
		} // end rep
*/		
		int res = dp[n-1][m-1]; 		
		cout << res << endl;
	} // end loop

//	printf ("%d\n", res );
		
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define bit(n) (1LL<<(n))
#define pcnt(x) __builtin_popcountll(x)
#define PB push_back
#define MP make_pair
#define endl "\n"
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}



int main() {
  int q; cin >> q;
  FOR(i,0,q-1){
    int dp[1010][1010];
    string a,b; cin >> a >> b;
    MEMSET(dp,0);
    FOR(j,1,a.length()){
      FOR(k,1,b.length()){
        if(a[j-1] == b[k-1]) dp[j][k] = dp[j-1][k-1] + 1;
        else dp[j][k] = max(dp[j-1][k],dp[j][k-1]);
      }
    }
    // FOR(i,1,a.length()){
    //   FOR(j,1,b.length()){
    //     cout << dp[i][j] << " ";
    //   }
    //   cout << "\n";
    // }
    cout << dp[a.length()][b.length()] << "\n";
  }
}

C++: #include <iostream>

using namespace std;

/**
 *  lcs(m,n) = 
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

int lcs(int m, int n)
{
	if (m == -1 || n== -1) return 0;
	if (x[m] == x[n]) return lcs(m-1,n-1) + 1;
	int xx = lcs(m-1,n);
	int yy = lcs(m,n-1);
	return xx > yy ? xx : yy;
}

int main()
{
	int n = 0;
	x.resize(150);
	y.resize(150);
	cin >> n; 
	for (int i=0;i<n;i++)
	{
		cin >> x;
		cin >> y;
		return lcs(x.size()-1, y.size()-1);
	}
}