Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

C++: #include<stdio.h>

int hantei(char x[], char y[]) {
  int xn = 0, yn = 0, flag = 0, i, j, max = 0;
  int c[1001][1001];
  
  while (flag == 0)
  {
    if (x[xn] >= 97 && x[xn] <= 122)
    {
      xn++;
    }
    else
    {
      flag = 1;
    }
  }
  
  flag = 0;
  
  while (flag == 0)
  {
    if (y[yn] >= 97 && y[yn] <= 122)
    {
      yn++;
    }
    else
    {
      flag = 1;
    }
  }
  for (i = 1; i < xn + 1; i++) c[i][0] = 0;
  for (j = 1; j < yn + 1; j++) c[0][j] = 0;
  
  for (i = 1; i < xn + 1; i++)
  {
    for (j = 1; j < yn + 1; j++)
    {
      if (x[i - 1] == y[j - 1])
      {
        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];
      }
      if (max < c[i][j]) max = c[i][j];
    }
  }
  
  return max;
}

int main(void) {
  char x[1000], y[1000];
  int q, i;
  
  scanf("%d", &q);
  
  for (i = 0; i < q; i++)
  {
    scanf("%s", x);
    scanf("%s", y);
    printf("%d\n", hantei(x, y));
  }
  
  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();
    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] + 1;
            } else {
                c[i][j] = max(c[i - 1][j] , c[i][j - 1]);
            }
        }
    }

    return c[m][n];
}

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

C++: #include <iostream>
#include<string>
using namespace std;
int main(void){
    // Your code here!
    // Your code here!
    int q;
    cin>>q;
    string x[q],y[q];
   
    //string s[q*2];
    string tmp;
    for(int i=0;i<q;i++){
        cin>>tmp;
        x[i]=tmp;
        cin>>tmp;
        y[i]=tmp;
    }
    for(int i=0;i<q;i++){
        //cout<<sizeof(x)/sizeof(x[0])<<" "<<sizeof(y[i])<<"\n";
        int dp[x[i].length()+1][y[i].length()+1];
        for(int j=0;j<x[i].length()+1;j++){ 
            for(int k=0;k<y[i].length()+1;k++){ 
                if(j==0 || k==0){
                    dp[j][k]=0;
                }
                else if(x[i][j-1]==y[i][k-1]){
                    dp[j][k]=dp[j-1][k-1]+1;
                }
                else{
                dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
                }
            }
        }
        cout<<dp[x[i].length()][y[i].length()]<<"\n";
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int> ans(n);
	for (int cou = 0;cou < n;cou++) {
		string s, t;
		cin >> s >> t;
		int S = s.size(), T = t.size();
		vector<vector<int> > dp(S + 1, vector<int>(T + 1, 0));
		for (int i = 0;i < S;i++) {
			for (int j = 0;j < T;j++) {
				if (s[i] == t[j]) {
					dp[i + 1][j + 1] = dp[i][j] + 1;
				}
				else {
					dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
				}
			}
		}
		ans[cou] = dp[S][T];
	}
	for (int i = 0;i < n;i++) {
		cout << ans[i] << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T>
bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

vvi dp;
string gx, gy;
int dfs(int ix, int iy) {
    if (ix == gx.length() or iy == gy.length()) {
        return 0;
    }
    if (dp[ix][iy] != -1) return dp[ix][iy];

    if (gx[ix] != gy[iy]) {
        int c1 = dfs(ix+1, iy);
        int c2 = dfs(ix, iy+1);
        dp[ix][iy] = max(c1, c2);
    } else {
        int c1 = dfs(ix+1, iy+1)+1;
        int c2 = dfs(ix, iy+1);
        dp[ix][iy] = max(c1, c2);
    }

    return dp[ix][iy];
}

void solve(string x, string y) {
    gx = x, gy = y;
    dp = vvi(1005, vi(1000, -1));
    cout << dfs(0, 0) << endl;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    REP(i, n) {
        string x, y;
        cin >> x >> y;
        solve(x, y);
    }

    return 0;
}

C++: /*
 * c.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 1000;

/* typedef */

/* global variables */

int dp[MAX_N + 1][MAX_N + 1];

/* subroutines */

/* main */

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

  while (qn--) {
    string x, y;
    cin >> x >> y;

    int xn = x.size(), yn = y.size();

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i <= xn; i++)
      for (int j = 0; j <= yn; j++) {
	if (i < xn && j < yn && x[i] == y[j] &&
	    dp[i + 1][j + 1] < dp[i][j] + 1)
	  dp[i + 1][j + 1] = dp[i][j] + 1;

	if (i < xn && dp[i + 1][j] < dp[i][j])
	  dp[i + 1][j] = dp[i][j];

	if (j < yn && dp[i][j + 1] < dp[i][j])
	  dp[i][j + 1] = dp[i][j];
      }

    printf("%d\n", dp[xn][yn]);
  }

  return 0;
}