Go: package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	for q := 0; q < n; q++ {
		var s1, s2 string
		fmt.Scan(&s1, &s2)
		s1, s2 = " " + s1, " " + s2

		dp := make([][]int, len(s1))
		for i := range dp {
			dp[i] = make([]int, len(s2))
		}

		max := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					dp[i][j] = dp[i-1][j-1] + 1
				} else {
					dp[i][j] = Max(dp[i-1][j], dp[i][j-1])
				}
				max = Max(max, dp[i][j])
			}
		}

		fmt.Println(max)
	}
}

func Max(n ...int) int {
	max := n[0]
	for _, v := range n[1:] {
		if v > max {
			max = v
		}
	}
	return max
}

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

int n;
string x, y;
bool mat[1000][1000];
int a[1000][1000];

int max(int i, int j) {
	int r = (!mat[i - 1][j] ? a[i - 1][j] : 0);
	r = ((!mat[i][j - 1] && a[i][j - 1] > r) ? a[i][j - 1] : r);
	return r > a[i - 1][j - 1] ? r : a[i - 1][j - 1];
}

int main(){
	cin >> n;
	for (int ia = 0; ia < n; ia++) {
		int ans = 0, p = 0;
		cin >> x >> y;
		for (int i = 0; i < x.length(); i++) {
			mat[i][0] = x[i] == y[0] ? true : false;
			a[i][0] = mat[i][0] ? 1 : 0;
		}
		for (int i = 1; i < y.length(); i++) {
			mat[0][i] = x[0] == y[i] ? true : false;
			a[0][i] = mat[0][i] ? 1 : 0;
		}
		for (int i = 1; i < x.length(); i++) {
			for (int j = 1; j < y.length(); j++) {
				mat[i][j] = x[i] == y[j] ? true : false;
				a[i][j] = max(i, j) + (mat[i][j] ? 1 : 0);
				if (a[i][j] > p) p = a[i][j];
			}
		}
		if (a[x.length() - 1][0] > p) p = a[x.length() - 1][0];
		if (a[0][y.length() - 1] > p) p = a[0][y.length() - 1];
		cout << p << endl;
	}


	return 0;
}

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

using namespace std;



int n;
string a, b;



int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    
   
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        if(j == 0 || i == 0){
          longest[i][j] = 0;
        }
        else if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

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

int q;
int **len;
int common(string a,string b,int m,int n){
    if(m==0||n==0){
        return 0;
    }
    if(a[m-1]==b[n-1]){
        if(m>1&&n>1){
            if(len[m-2][n-2]!=-1)len[m-1][n-1]=len[m-2][n-2]+1;
            else len[m-1][n-1]=common(a.substr(0,m-1),b.substr(0,n-1),m-1,n-1)+1;
        }else{
            len[m-1][n-1]=1;
        }
    }
    else{
        int l1,l2;
        //?±?l1
        if(n>1){
            if(len[m-1][n-2]!=-1)l1=len[m-1][n-2];
            else l1=len[m-1][n-2]=common(a,b.substr(0,n-1),m,n-1);
        }else l1=0;
        if(m>1){
            if(len[m-2][n-1]!=-1)l2=len[m-2][n-1];
            else l2=len[m-2][n-1]=common(a.substr(0,m-1),b,m-1,n);
        }else l2=0;

        len[m-1][n-1]=max(l1,l2);
    }
    return len[m-1][n-1];
}

int main()
{
    cin>>q;
    int i;
    for(i=0;i<q;i++){
        string a,b;
        cin>>a>>b;
        int m=a.length();
        int n=b.length();
        len=new int*[m];
        int j;
        for(j=0;j<m;j++){
            len[j]=new int[n];
            memset(len[j],-1,n*sizeof(int));
        }
        cout<<common(a,b,m,n)<<endl;
    }
    return 0;
}

//int main(){
//    int **p=new int*[5];
//    p[1]=new int[5];
//    memset(p[1],-1,5*sizeof(int));
//    cout<<p[1][3]<<" "<<p[0][1];
//}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n);++i)
using namespace std;
template <class _t> inline bool chmax (_t& a,_t b){if(a<b){a=b;return true;}return false;} template <class _t> inline bool chmin (_t& a,_t b){if(a>b){a=b;return true;}return false;}

const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1,0,-1,0};int dy[4] = {0,-1,0,1};

int main() {
    int t;cin >> t;
    for(int i=0;i<t;i++){
	string s1,s2;cin >> s1 >> s2;
	int m=s1.size(),n = s2.size();
	int dp[m+1][n+1];
	rep(i,m+1)rep(j,n+1) dp[i][j] = 0;
	for(int i=1;i<=m;i++){
	    for(int j=1;j<=n;j++){
		if(s1[i-1]==s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
		else{
		    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
		}
	    }
	}
	cout << dp[m][n] << endl;
    }
    return 0;
}

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

using namespace std;

int main(void)
{
    int q;
    char x[1001], y[1001];
    int table[1001][1001];
    
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        int xl = strlen(x), yl = strlen(y);

        for (int i = 0; i < xl; i++)
            for (int j = 0; j < yl; j++)
                if (x[i] == y[j])
                    table[i+1][j+1] = 1 + table[i][j];
                else
                    table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);

        cout << table[xl][yl] << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

int table[1001][1001];

int lcs(char *x, char *y){
    int i, j, lx, ly;
    lx = strlen(x+1);
    ly = strlen(y+1);
    for(i=1;i<=lx;i++){
        for(j=1;j<=ly;j++){
            if(x[i]==y[j])table[i][j] = table[i-1][j-1] + 1;
            else table[i][j] = std::max(table[i][j-1],table[i-1][j]);
        }
    }
    return table[lx][ly];
}

int main(){
    int i, j, q;
    char x[1002],y[1002];
    scanf("%d",&q);
    for(i=0;i<1001;i++){
        table[i][0] = 0;
    }
    for(j=0;j<1001;j++){
        table[0][j] = 0;
    }
    for(i=0;i<q;i++){
        scanf("%s %s",x+1,y+1);
        printf("%d\n",lcs(x,y));
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

int q;
int dp[1001][1001];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( ret, std::max( rec( i+1, j ), rec( i, j+1 ) ) );

  return ret;
}

int main()
{
  std::cin >> q;
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define _GLIBCXX_DEBUG

void printv(vector<int> v) {
  for (int i : v)
    cout << i << " ";
  cout << endl;
}

int main() {
  int q;
  cin >> q;
  rep(loop, q) {
    string x, y;
    cin >> x >> y;
    vector<vector<int>> L(1010, vector<int>(1010, 0));
    int lx = x.size(), ly = y.size();
    for (int i = 1; i <= lx; i++) {
      for (int j = 1; j <= ly; j++) {
        if (x[i-1] == y[j-1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else
          L[i][j] = max(L[i - 1][j], L[i][j - 1]);
      }
    }
    cout << L[lx][ly] << endl;
  }
}