/* 2012-10-26T16:24:59 */
#define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<int>(cerr, " "));                            \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
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();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    IFC(s[i] == ' ');
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/

int main() {
  std::ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int pre;
    cin >> pre;
    int s = pre;
    VI dist;
    REP(i, n - 1) {
      int x;
      cin >> x;
      dist.PB(x - pre);
      pre = x;
    }
    int e = pre;

    int ans = e - s;
    RSORT(dist);
    REP(i, k - 1) { ans -= dist[i]; }
    cout << max(0, ans) << endl;
  }
}

/* 2012-10-26T16:24:59 */
#define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<int>(cerr, " "));                            \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
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();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    IFC(s[i] == ' ');
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/

int main() {
  std::ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int pre;
    cin >> pre;
    int s = pre;
    VI dist;
    REP(i, n - 1) {
      int x;
      cin >> x;
      dist.PB(x - pre);
      pre = x;
    }
    int e = pre;

    int ans = e - s;
    RSORT(dist);
    REP(i, k - 1) {
      if (i >= dist.size()) {
        break;
      }
      ans -= dist[i];
    }
    cout << max(0, ans) << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < k - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < min(n, k) - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) // the number of dataset
  {
    int n, k;
    cin >> n >> k;
    int min_Length;
    if (k >= n) {
      min_Length = 0;
    } else {
      vector<int> x(100001);
      for (int i = 0; i < n; i++) {
        cin >> x[i];
      }
      vector<int> A(n);
      for (int i = 0; i < n - 1; i++) {
        A[i] = x[i + 1] - x[i];
      }
      sort(A.begin(), A.end());
      min_Length = x[n - 1] - x[0];
      for (int m = n - 2; m >= n - k; m--) {
        min_Length -= A[m];
      }
    }
    cout << min_Length << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) // the number of dataset
  {
    int n, k;
    cin >> n >> k;
    int min_Length;
    vector<int> x(100001);
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (k >= n) {
      min_Length = 0;
    } else {
      vector<int> A(n);
      for (int i = 0; i < n - 1; i++) {
        A[i] = x[i + 1] - x[i];
      }
      sort(A.begin(), A.end());
      min_Length = x[n - 1] - x[0];
      for (int m = n - 1; m >= n - k + 1; m--) {
        min_Length -= A[m];
      }
    }
    cout << min_Length << endl;
  }
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

bool cmp(const P &p, const P &q) {
  if (p.S != q.S)
    return p.second > q.second;
  return p.F < q.F;
}

int main() {
  int n, k, T, ti[110000], Max, Min;
  cin >> T;
  while (T-- > 0) {
    vector<P> dif;
    cin >> n >> k;
    Max = 0;
    Min = 1000000000;
    for (int i = 0; i < n; i++) {
      cin >> ti[i];
      Max = max(Max, ti[i]);
      Min = min(Min, ti[i]);
      if (i != 0) {
        dif.push_back(P(i, ti[i] - ti[i - 1]));
      }
    }
    sort(dif.begin(), dif.end(), cmp);
    int sum = 0;

    for (int i = 0; i < k - 1; i++)
      sum += dif[i].S;
    if (n > k)
      cout << Max - sum - Min << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

bool cmp(const P &p, const P &q) {
  if (p.S != q.S)
    return p.second > q.second;
  return p.F < q.F;
}

int main() {
  int n, k, T, ti[110000], Max, Min;
  cin >> T;
  while (T-- > 0) {
    vector<P> dif;
    cin >> n >> k;
    Max = 0;
    Min = 1000000000;
    for (int i = 0; i < n; i++) {
      cin >> ti[i];
      Max = max(Max, ti[i]);
      Min = min(Min, ti[i]);
      if (i != 0) {
        dif.push_back(P(i, ti[i] - ti[i - 1]));
      }
    }
    sort(dif.begin(), dif.end(), cmp);
    int sum = 0;

    if (n > k) {
      for (int i = 0; i < k - 1; i++)
        sum += dif[i].S;
      cout << Max - sum - Min << endl;
    } else
      cout << "0" << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n, k;
int house_position[100000];
int bitween_house[100000];
int min_long;

int main() {

  while (cin >> n) {
    cin >> k;

    for (int i = 0; i < n; ++i) {
      cin >> house_position[i];
    }

    for (int i = 0; i < n - 1; ++i) {
      bitween_house[i] = house_position[i + 1] - house_position[i];
    }

    sort(bitween_house, bitween_house + n - 1);

    for (int i = 0; i < n - k - 2; ++i) {
      min_long += bitween_house[i];
    }

    cout << min_long << endl;
  }

  return 0;
}

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int t, n, k;
int house_position[100000 + 10];
int bitween_house[100000 + 10];
int min_long;

int main() {

  cin >> t;

  for (int i = 0; i < t; ++i) {

    cin >> n >> k;

    for (int i = 0; i < n; ++i) {
      cin >> house_position[i];
    }

    for (int i = 0; i + 1 < n; ++i) {
      bitween_house[i] = house_position[i + 1] - house_position[i];
    }

    sort(bitween_house, bitween_house + n - 1);

    for (int i = 0; i < n - k; ++i) {
      min_long += bitween_house[i];
    }

    cout << min_long << endl;

    min_long = 0;
  }

  return 0;
}


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

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

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

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

  return 0;
}

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

using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

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

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

  return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, k - 1) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, min(n - 1, k - 1)) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

long long n, k, m, x[100010], d[100010], result, l = 0;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    for (int j = 0; j < n - 1; j++) {
      cin >> x[j];
      if (j >= 1)
        d[j - 1] = x[j] - x[j - 1];
    }
    sort(d, d + n - 1);
    m = max(l, n - k);
    for (int j = 0; j < m; j++)
      result += d[j];
    cout << result << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

long long n, k, m, x[100010], d[100010], result, l = 0;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    result = 0;
    cin >> n >> k;
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    for (int j = 0; j + 1 < n; j++) {
      d[j] = x[j + 1] - x[j];
    }
    sort(d, d + n - 1);
    m = max(l, n - k);

    for (int j = 0; j < m; j++)
      result += d[j];
    cout << result << endl;
  }
}


#include <algorithm>
#include <iostream>
using namespace std;
int A[20000];
int B[20000];

int deleteNmin(int n, int k) {
  int all = A[n - 1] - A[0];
  int j = 0;
  for (int i = n - 1; i > 0; i--) {
    B[j++] = A[i] - A[i - 1];
  }
  sort(B, B + j);
  if (k >= n)
    return 0;
  if (k > 1)
    for (int i = 1; i < k; i++) {
      all -= B[j - i];
    }

  return all;
}

int main() {
  int times;
  int n;
  int k;
  cin >> times;
  while (times--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int ans = deleteNmin(n, k);
  }
}

#include <algorithm>
#include <iostream>
using namespace std;
int A[200000];
int B[200000];

int deleteNmin(int n, int k) {
  int all = A[n - 1] - A[0];
  int j = 0;
  for (int i = n - 1; i > 0; i--) {
    B[j++] = A[i] - A[i - 1];
  }
  sort(B, B + j);
  if (k >= n)
    return 0;
  if (k > 1)
    for (int i = 1; i < k; i++) {
      all -= B[j - i];
    }

  return all;
}

int main() {
  int times;
  int n;
  int k;
  cin >> times;
  while (times--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int ans = deleteNmin(n, k);

    cout << ans << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int i, j, t, n, m, x, y, min, sum;
  vector<int> data;

  cin >> t;

  for (i = 0; i < t; ++i) {
    cin >> n >> m;

    for (j = 0; j < n; ++j) {
      cin >> x;
      if (j != 0)
        data.push_back(x - y);
      y = x;
    }

    sum = 0;
    while (n > m) {
      for (j = 1, min = 0; j < data.size(); ++j) {
        if (data[min] > data[j])
          min = j;
      }
      sum += data[min];
      data.erase(data.begin() + min);
      --n;
    }

    cout << sum << endl;
    data.clear();
  }

  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int i, j, t, n, m, x, y, sum;
  vector<int> data;

  cin >> t;

  for (i = 0; i < t; ++i) {
    cin >> n >> m;

    for (j = 0; j < n; ++j) {
      cin >> x;
      if (j != 0)
        data.push_back(x - y);
      y = x;
    }

    sum = 0;
    if (n > m) {
      sort(data.begin(), data.end());
      for (j = 0; n > m; ++j, --n)
        sum += data[j];
    }

    cout << sum << endl;
    data.clear();
  }

  return 0;
}


#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      std::cin >> n;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      for (int j = 0; j < n; ++j)
        std::cin >> k;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}


#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; t++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}


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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    int sum = 0;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> a.at(j);
    }
    for (int m = 0; m < n - 1; m++) {
      b.at(m) = a.at(i + 1) - a.at(i);
    }
    sort(b.begin(), b.end());
    int c = n - k;
    for (int l = 0; l < c; l++) {
      sum += b.at(l);
      cout << sum << endl;
    }
  }
}

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    int sum = 0;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> a.at(j);
    }
    for (int m = 0; m < n - 1; m++) {
      b.at(m) = a.at(m + 1) - a.at(m);
    }
    sort(b.begin(), b.end());
    int c = n - k;
    for (int l = 0; l < c; l++) {
      sum += b.at(l);
    }
    cout << sum << endl;
  }
}

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 2)


#include <algorithm>
#include <iostream>
using namespace std;

int k;
int n;
int m;

void rec(int dist[], int nk, int i, int l) {
  if (nk == k - 1 && i == n - 1) {
    if (l > m)
      m = l;
    return;
  }
  for (int j = i; j + 1 < n; j++) {
    if (nk != k - 1)
      rec(dist, nk + 1, j + 1, l + (dist[j + 1] - dist[j]));
    rec(dist, nk, j + 1, l);
  }
}

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    cin >> n >> k;
    int dist[n];
    for (int i = 0; i < n; i++) {
      cin >> dist[i];
    }

    int ans;
    if (k >= n) {
      ans = 0;
    } else {
      m = -1;
      rec(dist, 0, 0, 0);
      ans = dist[n - 1] - dist[0] - m;
    }
    cout << ans << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int n, k;
    cin >> n >> k;
    int dist;
    int aida[n - 1];
    int bfrD;
    for (int i = 0; i < n; i++) {
      int d;
      cin >> d;
      if (i == 0)
        dist = d;
      if (i + 1 == n)
        dist = d - dist;

      if (i != 0) {
        aida[i - 1] = d - bfrD;
      }

      bfrD = d;
    }

    if (k >= n)
      cout << 0 << endl;
    else {
      sort(aida, &aida[n - 1]);
      for (int i = 0; i < k - 1; i++) {
        dist -= aida[n - 2 - i];
      }

      cout << dist << endl;
    }
  }
  return 0;
}


#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;

int main() {
  int set, i, j, p, q, n, k, input[10010], st, en, sa[10010], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\\n", cont);
  }
}

#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;

int main() {
  int set, i, j, p, q, n, k, input[100100], st, en, sa[100100], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\\n", cont);
  }
}


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e9 + 9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int t, n, k;
int x[1000100];

bool C(int d) {
  int co = 0;

  REP(i, n) {
    if (co >= k)
      return false;
    int pos = i;
    while (x[pos] - x[i] <= d) {
      pos++;
      if (pos == n)
        break;
    }
    i = pos - 1;
    co++;
  }

  return true;
}

int main() {
  cin >> t;
  REP(o, t) {
    cin >> n >> k;
    REP(i, n) cin >> x[i];

    int lb = 0, ub = 1000000;
    while (ub - lb > 0) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        ub = mid;
      else
        lb = mid + 1;
    }

    vector<ll> v;
    int pos = 0, co = 0;
    REP(i, n) {
      if (x[i] - x[pos] > ub) {
        v.push_back(x[i - 1] - x[pos]);
        pos = i;
        co++;
      }
      if (i == n - 1) {
        v.push_back(x[i] - x[pos]);
        co++;
      }
    }

    sort(ALL(v));
    REP(i, k - co) v.pop_back();

    ll ans = 0;
    REP(i, v.size()) ans += v[i];
    cout << ans << endl;
  }
}

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e9 + 9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

// int t, n, k;
// int x[1000100];
//
// bool C(int d){
//	int co = 0;
//
//	REP(i, n){
//		if (co >= k)return false;
//		int pos = i;
//		while (x[pos] - x[i] <= d){
//			pos++;
//			if (pos == n)break;
//		}
//		i = pos - 1;
//		co++;
//	}
//
//	return true;
// }
//
// int main(){
//	cin >> t;
//	REP(o, t){
//		cin >> n >> k;
//		REP(i, n)cin >> x[i];
//
//		int lb = 0, ub = 1000000;
//		while (ub - lb > 0){
//			int mid = (ub + lb) / 2;
//			if (C(mid))ub = mid;
//			else lb = mid + 1;
//		}
//
//		vector<ll> v;
//		int pos = 0, co = 0;
//		REP(i, n){
//			if (x[i] - x[pos] > ub){
//				v.push_back(x[i - 1] - x[pos]);
//				pos = i;
//				co++;
//			}
//			if (i == n - 1){
//				v.push_back(x[i] - x[pos]);
//				co++;
//			}
//		}
//
//		sort(ALL(v));
//		REP(i, k - co){
//			if (!v.empty())v.pop_back();
//		}
//
//		ll ans = 0;
//		REP(i, v.size())ans += v[i];
//		cout << ans << endl;
//	}
// }

ll t, n, k;
ll x[1000100];

int main() {
  cin >> t;
  REP(i, t) {
    cin >> n >> k;
    REP(i, n) cin >> x[i];
    V v;
    FOR(i, 1, n) { v.push_back(x[i] - x[i - 1]); }
    sort(ALL(v));
    ll ans = 0;
    REP(i, n - k) ans += v[i];
    cout << ans << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  int t;
  std::cin >> t;
  std::vector<int> v(t);
  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> x(n);
    int denki = 0;
    for (int j = 0; j < n; ++j) {
      std::cin >> x[j];
    }
    if (n > k) {
      std::vector<int> l(n - 1);
      for (int j = 0; j < n; ++j) {
        l[j] = -x[j + 1] - x[j];
      }
      std::sort(l.begin(), l.end());
      for (int j = 0; j < k; ++j) {
        denki += x[j];
      }
      v[i] = denki;
    } else {
      v[i] = 0;
    }
  }
  for (int i = 0; i < t; ++i) {
    std::cout << v[i] << std::endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  int t;
  std::cin >> t;
  std::vector<int> v(t);
  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> x(n);
    int denki = 0;
    for (int j = 0; j < n; ++j) {
      std::cin >> x[j];
    }
    if (n > k) {
      std::vector<int> l(n - 1);
      for (int j = 0; j < n - 1; ++j) {
        l[j] = x[j + 1] - x[j];
      }
      std::sort(l.begin(), l.end());
      for (int j = n - 2; j > n - 1 - k; --j) {
        denki += l[j];
      }
      v[i] = x[n - 1] - x[0] - denki;
    } else {
      v[i] = 0;
    }
  }
  for (int i = 0; i < t; ++i) {
    std::cout << v[i] << std::endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void do_country(int &n, int &k, vector<int> &x);

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x;
    for (int j = 0; j < n; j++) {
      int input_x;
      cin >> input_x;
      x.push_back(input_x);
    }
    do_country(n, k, x);
  }
}

void do_country(int &n, int &k, vector<int> &x) {
  sort(x.begin(), x.end());
  reverse(x.begin(), x.end());
  // cout << x.at(0) << x.at(n - 1) << endl;
  int ans = x[0] - x[n - 1];
  vector<int> distance;
  for (int i = 0; i < n - 1; i++) {
    distance.push_back(x[i] - x[i + 1]);
  }
  sort(distance.begin(), distance.end());
  reverse(distance.begin(), distance.end());
  // cout << ans << endl;
  for (int i = 1; i < k; i++) {
    ans -= distance[i - 1];
  }
  cout << ans << endl;
}

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void do_country(int &n, int &k, vector<int> &x);

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x;
    for (int j = 0; j < n; j++) {
      int input_x;
      cin >> input_x;
      x.push_back(input_x);
    }
    do_country(n, k, x);
  }
}

void do_country(int &n, int &k, vector<int> &x) {
  sort(x.begin(), x.end());
  // reverse(x.begin(), x.end());
  // cout << x.at(0) << x.at(n - 1) << endl;
  int ans = x[n - 1] - x[0];
  vector<int> distance;
  for (int i = 0; i < n - 1; i++) {
    distance.push_back(x[n - 1 - i] - x[n - 2 - i]);
  }
  sort(distance.begin(), distance.end());
  // reverse(distance.begin(), distance.end());
  // cout << ans << endl;
  for (int i = 1; i < (n > k ? k : n); i++) {
    ans -= distance[n - i - 1];
  }
  cout << ans << endl;
}


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int num(string s, int &i);
int calc(string s, int &i);
int x;

bool isnum(char c) {
  int y = c - 'a';
  return (y >= 0) && (y <= 3);
}
int calc(string s, int &i) {
  if (isnum(s[i]))
    return num(s, i);
  i++;
  char op = s[i];
  i++;
  int ret = calc(s, i);
  int res = calc(s, i);
  i++;
  if (op == '+')
    return ret | res;
  if (op == '*')
    return ret & res;
  if (op == '^')
    return ret ^ res;
  return 0;
}

int num(string s, int &i) {
  i++;
  if (s[i - 1] == 'a')
    return x / 1000;
  if (s[i - 1] == 'b')
    return (x / 100) % 10;
  if (s[i - 1] == 'c')
    return (x / 10) % 10;
  if (s[i - 1] == 'd')
    return x % 10;
  return 0;
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    map<int, int> mp;
    rep(i, 10000) {
      x = i;
      int j = 0;
      mp[calc(s, j)]++;
    }
    int n;
    cin >> n;
    x = n;
    int j = 0;
    int ans = calc(s, j);
    cout << ans << " " << mp[ans] << endl;
  }
  return 0;
}

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

void solve() {
  int y, m, d;
  cin >> y >> m >> d;
  vector<int> h = {31, 4, 30};
  if (h < vector<int>{y, m, d}) {
    cout << "? " << y - 30 << " " << m << " " << d << endl;
  } else
    cout << "HEISEI " << y << " " << m << " " << d << endl;
}
int main() {
  string s;
  while (cin >> s, s != "#")
    solve();
  return 0;
}


#include <iostream>
using namespace std;
int main() {
  int n, i, j;
  double a[112345] = {};
  int p[112345] = {};
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++)
      if (a[i] > a[j])
        p[i] += 3;
      else if (a[i] < a[j])
        p[j] += 3;
      else {
        p[i]++;
        p[j]++;
      }
  for (i = 0; i < n; i++)
    cout << p[i] << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, o, n) for (int i = o; i < n; ++i)
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define all(X) X.begin(), X.end()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<pair<string, int>> ps(n);
  rep(i, n) cin >> ps[i].X, ps[i].Y = i;
  sort(all(ps));
  vector<int> re(n);
  for (auto p : ps) {
    int l = lower_bound(all(ps), make_pair(p.X, 0)) - ps.begin();
    int r = lower_bound(all(ps), make_pair(p.X, n)) - ps.begin();
    re[p.Y] = l * 2 + r - 1;
  }
  rep(i, n) cout << re[i] << "\\n";
  return 0;
}


#include <iostream>
using namespace std;
int main() {
  double gpa[100000];
  int N, win[100000] = {};
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> gpa[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (gpa[i] > gpa[j])
        win[i] += 3;
      else if (gpa[i] == gpa[j])
        win[i]++;
    }
    cout << (win[i] - 1) << "\\n";
  }
  return 0;
}


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

int main() {
  int N;
  vector<string> v, w;

  while (cin >> N, N) {
    for (int i = 0; i < N; ++i) {
      cin >> v[i];
      w[i] = v[i];
    }
    sort(w.begin(), w.end());

    for (int i = 0; i < N; ++i) {
      int win = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
      int lose = w.end() - upper_bound(w.begin(), w.end(), v[i]);
      int draw = N - win - lose - 1;
      cout << win * 3 + draw << endl;
    }
  }
  return 0;
}

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

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

  vector<string> v(N), w(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    w[i] = v[i];
  }
  sort(w.begin(), w.end());

  for (int i = 0; i < N; ++i) {
    int win = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
    int lose = w.end() - upper_bound(w.begin(), w.end(), v[i]);
    int draw = N - win - lose - 1;
    cout << win * 3 + draw << endl;
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

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

  vector<double> GPA(N);
  for (int i = 0; i < N; i++) {
    cin >> GPA[i];
  }

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      if (GPA[i] > GPA[j])
        ans[i] += 3;
      else if (GPA[i] == GPA[j])
        ans[i] += 1;
    }
  }

  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

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

  vector<pair<double, int>> GPA(N);
  for (int i = 0; i < N; i++) {
    double val;
    cin >> val;
    GPA[i] = make_pair(val, i);
  }
  sort(GPA.begin(), GPA.end());

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int id = GPA[i].second;

    int lb = lower_bound(GPA.begin(), GPA.end(), make_pair(GPA[i].first, -1)) -
             GPA.begin();
    int ub =
        upper_bound(GPA.begin(), GPA.end(), make_pair(GPA[i].first, N + 1)) -
        GPA.begin();

    // printf("lb = %d, ub = %d\\n", lb, ub);
    ans[id] = 3 * lb + (ub - lb - 1);
  }
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}


#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int lcs(string x, string y) {
  int c[10][10];
  int m = x.size();
  int n = y.size();
  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;
  c[0][0] = 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 n;
  cin >> n;
  string a, b;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
}

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int lcs(string x, string y) {
  int c[1200][1200];
  int m = x.size();
  int n = y.size();
  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;
  c[0][0] = 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 n;
  cin >> n;
  string a, b;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
}


#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX(a, b) (a > b ? a : b)
using namespace std;
const int MAXN = 1010;
int dp[MAXN][MAXN];
char a[MAXN], b[MAXN];
int main() {
  int n;
  cin >> n;
  for (int k = 0; k < n; k++) {
    while (scanf("%s%s", a + 1, b + 1)) {
      memset(dp, 0, sizeof(dp));
      int i, j;
      for (i = 1; a[i]; i++) {
        for (j = 1; b[j]; j++) {
          if (a[i] == b[j])
            dp[i][j] = dp[i - 1][j - 1] + 1;
          else
            dp[i][j] = MAX(dp[i][j - 1], dp[i - 1][j]);
        }
      }
      printf("%d\\n", dp[i - 1][j - 1]);
    }
  }
  return 0;
}

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <string>
using namespace std;
#define MAX 1003

char A[MAX];
char B[MAX];

void print() {}
int DP[MAX][MAX];
int dp(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  if (DP[i][j] != -1)
    return DP[i][j];
  if (A[i] == B[j])
    return DP[i][j] = dp(i - 1, j - 1) + 1;
  return DP[i][j] = max(dp(i, j - 1), dp(i - 1, j));
}
void solve() {

  int tmp;
  scanf("%d", &tmp);
  int r, c;
  while (tmp--) {

    scanf("%s%s", A, B);
    r = strlen(A);
    c = strlen(B);
    memset(DP, -1, sizeof(DP));
    printf("%d\\n", dp(r - 1, c - 1));
  }
}

int main() {

  // freopen("in","r",stdin);
  solve();
  return 0;
}


#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int p[1001][1001] = {0};
int main() {
  int i, j, q, e, l, z, m, u, n;
  char s[2001], c[2001];
  cin >> q;
  for (e = 0; e < q; e++) {
    scanf("%s %s", s, c);
    l = strlen(s);
    z = strlen(c);

    for (i = 0; i <= l; i++) {
      for (j = 0; j <= z; j++) {
        p[i][j] = 0;
      }
    }
    for (i = 1; i <= z; i++) {
      for (j = 1; j <= l; j++) {
        if (c[i - 1] == s[j - 1])
          p[j][i] = p[j - 1][i - 1] + 1;
        else
          p[j][i] = max(p[j - 1][i], p[j][i - 1]);
      }
    }
    cout << p[l][z] << endl;
  }
  return 0;
}


#include <cstdio>
#include <cstring>
#define N 1000

int LCS[N][N];

int LCSLength(char X[], int m, char Y[], int n) {
  for (int i = 0; i <= m; i++)
    LCS[i][n] = 0;
  for (int j = 0; j <= n; j++)
    LCS[m][j] = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      LCS[i][j] = LCS[i + 1][j + 1];
      if (X[i] == Y[j])
        LCS[i][j]++;
      if (LCS[i][j + 1] > LCS[i][j])
        LCS[i][j] = LCS[i][j + 1];
      if (LCS[i + 1][j] > LCS[i][j])
        LCS[i][j] = LCS[i + 1][j];
    }
  }
  return LCS[0][0];
}

int main() {
  char X[N], Y[N];
  int num;
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) {
    scanf("%s %s", X, Y);
    int m = strlen(X);
    int n = strlen(Y);
    printf("%d\\n", LCSLength(X, m, Y, n));
  }
  return 0;
}

#include <cstdio>
#include <cstring>
#define N 1000

int LCS[N + 1][N + 1];

int LCSLength(char X[], int m, char Y[], int n) {
  for (int i = 0; i <= m; i++)
    LCS[i][n] = 0;
  for (int j = 0; j <= n; j++)
    LCS[m][j] = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      LCS[i][j] = LCS[i + 1][j + 1];
      if (X[i] == Y[j])
        LCS[i][j]++;
      if (LCS[i][j + 1] > LCS[i][j])
        LCS[i][j] = LCS[i][j + 1];
      if (LCS[i + 1][j] > LCS[i][j])
        LCS[i][j] = LCS[i + 1][j];
    }
  }
  return LCS[0][0];
}

int main() {
  char X[N + 1], Y[N + 1];
  int num;
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) {
    scanf("%s %s", X, Y);
    int m = strlen(X);
    int n = strlen(Y);
    printf("%d\\n", LCSLength(X, m, Y, n));
  }
  return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int q;
  cin >> q;
  for (int m = 0; m < q; m++) {
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1000][1000] = {0};

    for (int j = 0; j < wordy.size(); j++) {
      for (int i = 0; i < wordx.size(); i++) {
        if (wordx[i] == wordy[j]) {
          L[i + 1][j + 1] = 1 + L[i][j];
        } else {
          L[i + 1][j + 1] = max(L[i + 1][j], L[i][j + 1]);
        }
      }
    }
    cout << L[wordx.size()][wordy.size()] << endl;
  }
}

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int q;
  cin >> q;
  for (int m = 0; m < q; m++) {
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1010][1010] = {0};

    for (int j = 0; j < wordy.size(); j++) {
      for (int i = 0; i < wordx.size(); i++) {
        if (wordx[i] == wordy[j]) {
          L[i + 1][j + 1] = 1 + L[i][j];
        } else {
          L[i + 1][j + 1] = max(L[i + 1][j], L[i][j + 1]);
        }
      }
    }
    cout << L[wordx.size()][wordy.size()] << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <string.h>

#define MAX 10000

using namespace std;

int lcs(string s1, string s2) {
  int C[MAX + 1][MAX + 1];
  int m = s1.size();
  int n = s2.size();
  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 (s1[i - 1] == s2[j - 1]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i][j - 1], C[i - 1][j]);
      }
    }
  }

  return C[m][n];
}

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

#include <algorithm>
#include <iostream>
#include <string.h>

#define MAX 1000

using namespace std;

int lcs(string s1, string s2) {
  int C[MAX + 1][MAX + 1];
  int m = s1.size();
  int n = s2.size();
  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 (s1[i - 1] == s2[j - 1]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i][j - 1], C[i - 1][j]);
      }
    }
  }

  return C[m][n];
}

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


#include <cassert>
#include <climits>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <vector>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...)                                                           \\
  do {                                                                         \\
    printf("D(L%d) %s: ", __LINE__, __func__);                                 \\
    printf(__VA_ARGS__);                                                       \\
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

vector<string> makeSubsequences(string str, int length) {
  if (length == 0)
    return vector<string>();

  DPRINTF("str %s  length %d\\n", str.c_str(), length);
  assert(0 <= length && length <= str.length());

  vector<string> subsequences;

  if (length == 1) {
    for (int i = 0; i < str.length(); i++) {
      subsequences.push_back(str.substr(i, 1));
    }

    return subsequences;
  }

  for (int i = 0; i <= str.length() - length; i++) {
    const string substr = str.substr(i + 1, str.length() - (i + 1));
    vector<string> strs = makeSubsequences(substr, length - 1);

    for (string s : strs)
      subsequences.push_back(str[i] + s);
  }

  return subsequences;
}

bool isFindSubseq(string str, string subseq) {
  DPRINTF("str %s  subseq %s\\n", str.c_str(), subseq.c_str());

  assert(str.length() > 0 && subseq.length() > 0);

  int j = 0;

  for (int i = 0; i < str.length(); i++) {
    if (str[i] == subseq[j])
      j++;

    if (j == subseq.length())
      return true;
  }

  return false;
}

int setLCSlen(string strShort, string strLong, int idx, int *pLength) {
  DPRINTF("strLong %s  strShort %s  idx %d\\n", strLong.c_str(),
          strShort.c_str(), idx);

  assert(strShort.length() && strLong.length() && idx >= 0 && pLength);

  if (idx == 0) {
    const string s = strShort.substr(0, 1);

    if (isFindSubseq(strLong, s))
      pLength[0] = 1;
    else
      pLength[0] = 0;

    return pLength[0];
  }

  if (!pLength[idx - 1])
    setLCSlen(strShort, strLong, idx - 1, pLength);

  const string sub = strShort.substr(0, idx);

  vector<string> candidates = makeSubsequences(sub, pLength[idx - 1]);

  for (string &s : candidates)
    s = s + strShort[idx];

#if DEBUG
  DPRINTF("Candidates\\n");
  for (string s : candidates)
    DPRINTF(" - %s\\n", s.c_str());
#endif // DEBUG

  for (string s : candidates) {
    if (isFindSubseq(strLong, s)) {
      pLength[idx] = s.length();
      break;
    }
  }

  if (!pLength[idx])
    pLength[idx] = pLength[idx - 1];

  DPRINTF("pLength[%d] %d\\n", idx, pLength[idx]);

  return pLength[idx];
}

int getLCSlen(string s0, string s1) {
  const string *pStrLong;
  const string *pStrShort;

  if (s0.length() < s1.length()) {
    pStrShort = &s0;
    pStrLong = &s1;
  } else {
    pStrShort = &s1;
    pStrLong = &s0;
  }

  int *pLongestLens = new int[pStrShort->length()];

  // init
  for (int i = 0; i < pStrShort->length(); i++)
    pLongestLens[i] = 0;

  const int longest =
      setLCSlen(*pStrShort, *pStrLong, pStrShort->length() - 1, pLongestLens);

  delete[] pLongestLens;

  return longest;
}

int main() {
  int numOfPair;
  cin >> numOfPair;
  cin.ignore();

  DPRINTF("num of pair %d\\n", numOfPair);

  for (int i = 0; i < numOfPair; i++) {
    string str0, str1;
    cin >> str0 >> str1;
    cin.ignore();

    DPRINTF("str0 %s  str1 %s\\n", str0.c_str(), str1.c_str());

    cout << getLCSlen(str0, str1) << endl;
  }

  return 0;
}

#include <cassert>
#include <climits>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <vector>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...)                                                           \\
  do {                                                                         \\
    printf("D(L%d) %s: ", __LINE__, __func__);                                 \\
    printf(__VA_ARGS__);                                                       \\
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

class LongestCommonSubsequence {
public:
  LongestCommonSubsequence(string s0, string s1) : s0_(s0), s1_(s1) {
    size_ = s0_.length() * s1_.length();
    pLongest_ = new int[size_];
    for (int i = 0; i < size_; i++)
      pLongest_[i] = kInvalid;
  }

  ~LongestCommonSubsequence() {
    if (pLongest_)
      free(pLongest_);
  }

  int getLCSlen();
  void printLength();

  const int kInvalid = -1;

private:
  string s0_;
  string s1_;
  int *pLongest_;
  int size_;

  int getIndex(int x, int y);
  int getLCSlenOf(int n0, int n1);
};

/*********************************
Input:
  x[0], x[1], x[2], ..., x[n-1]
  y[0], y[1], y[2], ..., y[m-1]

Algorithm:
  if x[n-1] == y[m-1]
    LCS(n-1, m-1) = LCS(n-2,m-2) + 1

  if x[n-1] != y[m-1]
    LCS(n-1, m-1) = max(LCS(n-2, m-1), LCS(n-1, m-2))

  LCS(0, *) = LCS(*, 0) = 0;

  a b c b d a b
  b d c a b a
*********************************/

int LongestCommonSubsequence::getLCSlen() {
  int n0 = s0_.length() - 1;
  int n1 = s1_.length() - 1;
  return getLCSlenOf(n0, n1);
}

int LongestCommonSubsequence::getLCSlenOf(int n0, int n1) {
  DPRINTF("n0 %d  n1 %d\\n", n0, n1);

  assert(-1 <= n0 && n0 < static_cast<int>(s0_.length()));
  assert(-1 <= n1 && n1 < static_cast<int>(s1_.length()));

  if (n0 < 0 || n1 < 0)
    return 0;

  const int idx = getIndex(n0, n1);

  if (pLongest_[idx] != kInvalid)
    return pLongest_[idx];

  if (s0_[n0] == s1_[n1])
    pLongest_[idx] = getLCSlenOf(n0 - 1, n1 - 1) + 1;
  else
    pLongest_[idx] = max(getLCSlenOf(n0 - 1, n1), getLCSlenOf(n0, n1 - 1));

  return pLongest_[idx];
}

int LongestCommonSubsequence::getIndex(int x, int y) {
  return y * s0_.length() + x;
}

void LongestCommonSubsequence::printLength() {
  for (int i = 0; i < s0_.length(); i++)
    for (int j = 0; j < s1_.length(); j++)
      DPRINTF("pLongest_[%d][%d] %d\\n", i, j, pLongest_[getIndex(i, j)]);
}

int main() {
  int numOfPair;
  cin >> numOfPair;
  cin.ignore();

  DPRINTF("num of pair %d\\n", numOfPair);

  for (int i = 0; i < numOfPair; i++) {
    string str0, str1;
    cin >> str0 >> str1;
    cin.ignore();

    DPRINTF("str0 %s  str1 %s\\n", str0.c_str(), str1.c_str());

    LongestCommonSubsequence lcs(str0, str1);
    cout << lcs.getLCSlen() << endl;

#if (1) && DEBUG
    lcs.printLength();
#endif
  }

  return 0;
}


// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_C: Longest Common Subsequence

#include <algorithm>
#include <cstdio>
#include <cstring>

static unsigned int lcs(const char *x, const char *y) {
  auto m = static_cast<int>(std::strlen(x));
  auto n = static_cast<int>(std::strlen(y));
  unsigned int c0[m + 1][n + 1]; // extend -1
  auto const c = (c0 + 1) + 1;

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

  for (auto i = 0; i < m; ++i) {
    for (auto j = 0; j < n; ++j) {
      c[i][j] = (x[i] == y[j]) ? c[i - 1][j - 1] + 1
                               : std::max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

int main() {
  unsigned int q;
  std::scanf("%u", &q);
  const unsigned int MAX_LEN = 1000;
  char x[MAX_LEN + 1], y[MAX_LEN + 1];
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%1000s", x);
    std::scanf("%1000s", y);
    std::printf("%u\\n", lcs(x, y));
  }
}

// eof

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_C: Longest Common Subsequence

#include <algorithm>
#include <cstdio>
#include <cstring>

static unsigned int lcs(const char *x, const char *y) {
  auto m = static_cast<int>(std::strlen(x));
  auto n = static_cast<int>(std::strlen(y));
  unsigned int c[m + 1][n + 1]; // extend -1

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

  for (auto i = 0; i < m; ++i) {
    for (auto j = 0; j < n; ++j) {
      c[i + 1][j + 1] =
          (x[i] == y[j]) ? c[i][j] + 1 : std::max(c[i + 1][j], c[i][j + 1]);
    }
  }
  return c[m][n];
}

int main() {
  unsigned int q;
  std::scanf("%u", &q);
  const unsigned int MAX_LEN = 1000;
  char x[MAX_LEN + 1], y[MAX_LEN + 1];
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%1000s", x);
    std::scanf("%1000s", y);
    auto l = lcs(x, y);
    std::printf("%u\\n", l);
  }
}

// eof


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

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int dp[1000][1000];
int main() {
  int n;
  cin >> n;
  while (n--) {
    memset(dp, 0, sizeof(dp));
    string str1, str2;
    cin >> str1 >> str2;
    // int l =min(str1.length(),str2.length());
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[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]);
        }
      }
    }
    cout << dp[str1.length()][str2.length()] << endl;
  }
  return 0;
}

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

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int dp[1050][1050];
int main() {
  int n;
  cin >> n;
  while (n--) {
    memset(dp, 0, sizeof(dp));
    string str1, str2;
    cin >> str1 >> str2;
    // int l =min(str1.length(),str2.length());
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[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]);
        }
      }
    }
    cout << dp[str1.length()][str2.length()] << endl;
  }
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int q;
string s1, s2;
int ans;
int dp[150][150];
int main() {
  cin >> q;
  rep(i, q) {
    cin >> s1 >> s2;
    rep(i, s1.size()) {
      rep(j, s2.size()) { dp[i][j] = 0; }
    }
    if (s1[0] == s2[0])
      dp[0][0] = 1;
    rep1(i, s1.size() - 1) {
      if (s1[i] == s2[0])
        dp[i][0] = 1;
      dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }
    rep1(i, s2.size() - 1) {
      if (s1[0] == s2[i])
        dp[0][i] = 1;
      dp[0][i] = max(dp[0][i], dp[0][i - 1]);
    }
    rep1(i, s1.size() - 1) {
      rep1(j, s2.size() - 1) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s1[i] == s2[j])
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
    cout << dp[s1.size() - 1][s2.size() - 1] << endl;
  }
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int q;
string s1, s2;
int ans;
int dp[1510][1501];
int main() {
  cin >> q;
  rep(i, q) {
    cin >> s1 >> s2;
    rep(i, s1.size()) {
      rep(j, s2.size()) { dp[i][j] = 0; }
    }
    if (s1[0] == s2[0])
      dp[0][0] = 1;
    rep1(i, s1.size() - 1) {
      if (s1[i] == s2[0])
        dp[i][0] = 1;
      dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }
    rep1(i, s2.size() - 1) {
      if (s1[0] == s2[i])
        dp[0][i] = 1;
      dp[0][i] = max(dp[0][i], dp[0][i - 1]);
    }
    rep1(i, s1.size() - 1) {
      rep1(j, s2.size() - 1) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s1[i] == s2[j])
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
    cout << dp[s1.size() - 1][s2.size() - 1] << endl;
  }
}


#include <iostream>
#include <string>

int LCS(std::string str1, std::string str2, int i, int j) {
  if (str1[i] == str2[j]) {
    return 1;
  } else {
    return 0;
  }
}

int max(int a, int b, int c) {
  int m = a;
  if (b > m) {
    m = b;
  }
  if (c > m) {
    m = c;
  }
  return m;
}

int main() {
  int q;
  std::cin >> q;
  // int ans[q];
  std::string str1, str2;
  for (int z = 0; z < q; z++) {
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1 + 1][l2 + 1];

    for (int i = 0; i <= l1; i++) {
      DP[i][0] = 0;
    }
    for (int j = 0; j <= l2; j++) {
      DP[0][j] = 0;
    }

    for (int i = 1; i <= l1; i++) {
      for (int j = 1; j <= l2; j++) {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1],
                       DP[i - 1][j - 1] + LCS(str1, str2, i - 1, j - 1));
      }
    }
    // ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}

#include <iostream>
#include <string>

/*int LCS(std::string str1, std::string str2, int i, int j){
  if(str1[i] == str2[j]){
    return 1;
  }
  else{
    return 0;
  }
 }
*/

int max(int a, int b, int c) {
  int m = a;
  if (b > m) {
    m = b;
  }
  if (c > m) {
    m = c;
  }
  return m;
}

int main() {
  int q;
  std::cin >> q;
  // int ans[q];
  std::string str1, str2;
  for (int z = 0; z < q; z++) {
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1 + 1][l2 + 1];

    for (int i = 0; i <= l1; i++) {
      DP[i][0] = 0;
    }
    for (int j = 0; j <= l2; j++) {
      DP[0][j] = 0;
    }

    for (int i = 1; i <= l1; i++) {
      for (int j = 1; j <= l2; j++) {
        if (str1[i - 1] == str2[j - 1]) {
          DP[i][j] = max(DP[i - 1][j], DP[i][j - 1], DP[i - 1][j - 1] + 1);
        } else {
          DP[i][j] = max(DP[i - 1][j], DP[i][j - 1], DP[i - 1][j - 1]);
        }
      }
    }
    // ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}


#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<string> &rvsStrX, vector<string> &rvsStrY) {
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i) {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string> &cnrvsStrX,
                   const vector<string> &cnrvsStrY) {
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i) {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();

    vvnLcsDP.shrink_to_fit();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<string> &rvsStrX, vector<string> &rvsStrY) {
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i) {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string> &cnrvsStrX,
                   const vector<string> &cnrvsStrY) {
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i) {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();

    vvnLcsDP.clear();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}


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

int dp[1001][1001];
int main() {
  int b;
  scanf("%d", &b);
  char e[1001];
  char f[1001];
  for (int c = 0; c < b; c++) {
    scanf("%s%s", e, f);
    for (int g = 0; g <= strlen(e); g++) {
      for (int h = 0; h <= strlen(f); h++) {
        dp[g][h] = 0;
      }
    }
    for (int g = 0; g < strlen(e); g++) {
      for (int h = 0; h < strlen(f); h++) {
        if (e[g] == f[h]) {
          dp[g + 1][h + 1] = dp[g][h] + 1;
        } else {
          if (dp[g][h + 1] > dp[g + 1][h])
            dp[g + 1][h + 1] = dp[g][h + 1];
          else
            dp[g + 1][h + 1] = dp[g + 1][h];
        }
      }
    }
    printf("%d\\n", dp[strlen(e)][strlen(f)]);
  }
}


#include <iostream>
#include <string>

using namespace std;

int main() {

  string str1;
  string str2;
  int n, s1, s2;
  int c[1001][1001] = {{}};
  int max = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {

    cin >> str1 >> str2;

    str1 = ' ' + str1;
    str2 = ' ' + str2;

    s1 = str1.size();
    s2 = str2.size();

    for (int j = 1; j < s1; j++) {
      for (int k = 1; k < s2; k++) {
        if (str1[j] == str2[k]) {
          c[j][k] = c[j - 1][k - 1] + 1;
          if (c[j][k] > max)
            max = c[j][k];
        } else if (c[j - 1][k] > c[j][k - 1]) {
          c[j][k] = c[j - 1][k];
        } else {
          c[j][k] = c[j][k - 1];
        }
      }
    }

    cout << max << endl;

    max = 0;

    for (int j = 0; j <= s1; j++) {
      for (int k = 0; k <= s2; k++) {
        c[j][k] = 0;
      }
    }
  }
}

#include <algorithm>
#include <iostream>
#include <string>
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;
  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]);
      }
      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;
}


#include <algorithm>
#include <iostream>
#include <string>

int dp[1000 + 1][1000 + 1];

int lcsSub(std::string X, std::string Y, int i, int j) {
  if (i == -1 || j == -1) {
    return 0;
  } else {
    if (X[i] == Y[j]) {
      if (dp[i][j] < 0) {
        dp[i][j] = lcsSub(X, Y, i - 1, j - 1);
      }
      return dp[i][j] + 1;
    } else {
      int up, left;
      if (dp[i + 1][j] < 0) {
        dp[i + 1][j] = lcsSub(X, Y, i, j - 1);
      }
      left = dp[i + 1][j];
      if (dp[i][j + 1] < 0) {
        dp[i][j + 1] = lcsSub(X, Y, i - 1, j);
      }
      up = dp[i][j + 1];

      return std::max(up, left);
    }
  }
}

int lcs(std::string X, std::string Y) {
  int m, n, i, j;
  m = X.length();
  n = Y.length();
  for (i = 0; i < m + 1; i++) {
    for (j = 0; j < n + 1; j++) {
      dp[i][j] = -1;
    }
  }
  return lcsSub(X, Y, m - 1, n - 1);
}
int main(void) {
  int i, q, n, m;
  std::string str1, str2;
  std::cin >> q;
  for (i = 0; i < q; i++) {
    std::cin >> str1;
    std::cin >> str2;
    std::cout << lcs(str1, str2) << std::endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <string>

int dp[1001][1001];

int lcs(std::string X, std::string Y) {
  int m, n, i, j;
  m = X.length();
  n = Y.length();
  for (i = 0; i < std::max(m, n) + 1; i++) {
    dp[i][0] = 0;
    dp[0][j] = 0;
  }

  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      if (X[i] == Y[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = std::max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  return dp[m][n];
}
int main(void) {
  int i, q, n, m;
  std::string str1, str2;
  std::cin >> q;
  for (i = 0; i < q; i++) {
    std::cin >> str1;
    std::cin >> str2;
    std::cout << lcs(str1, str2) << std::endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s1, s2;
int memo[100][100];

int solve(int l1, int l2) {
  if (l1 < 0 or l2 < 0)
    return 0;
  if (memo[l1][l2] >= 0)
    return memo[l1][l2];
  // cout << s1[l1] << s2[l2] << endl;
  if (s1[l1] == s2[l2]) {
    return memo[l1][l2] = solve(l1 - 1, l2 - 1) + 1;
  }
  return memo[l1][l2] = max(solve(l1, l2 - 1), solve(l1 - 1, l2));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    fill(memo[0], memo[100], -1);
    cin >> s1;
    cin >> s2;
    cout << solve(s1.length(), s2.length()) - 1 << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s1, s2;
int memo[1001][1001];

int solve(int l1, int l2) {
  if (l1 < 0 or l2 < 0)
    return 0;
  if (memo[l1][l2] >= 0)
    return memo[l1][l2];
  // cout << s1[l1] << s2[l2] << endl;
  if (s1[l1] == s2[l2]) {
    return memo[l1][l2] = solve(l1 - 1, l2 - 1) + 1;
  }
  return memo[l1][l2] = max(solve(l1, l2 - 1), solve(l1 - 1, l2));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    fill(memo[0], memo[1001], -1);
    cin >> s1;
    cin >> s2;
    cout << solve(s1.length(), s2.length()) - 1 << endl;
  }
}


#include <cstring>
#include <iostream>
#include <string>
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];
// }

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int q;
int len[1005][1005];

int common(string a, string b, int m, int n) {
  int i, j;
  for (i = 0; i <= m; i++)
    len[i][0] = 0;
  for (i = 0; i <= n; i++)
    len[0][i] = 0;
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      if (a[i] == b[j])
        len[i + 1][j + 1] = len[i][j] + 1;
      else
        len[i + 1][j + 1] = max(len[i + 1][j], len[i][j + 1]);
    }
  }
  return len[m][n];
}

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();
    cout << common(a, b, m, n) << endl;
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

string s1, s2;

int L(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  else if (s1.at(i) == s2.at(j))
    return 1 + L(i - 1, j - 1);
  else {
    int a = L(i, j - 1);
    int b = L(i - 1, j);
    return a > b ? a : b;
  }
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s1 >> s2;
    cout << L(s1.size() - 1, s2.size() - 1) << endl;
  }
}

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

string s1, s2;
int l[1010][1010];
bool flag[1010][1010];

int L(int i, int j) {
  // cout<<i<<" "<<j<<endl;
  if (i < 0 || j < 0) {
    return 0;
  } else if (s1.at(i) == s2.at(j)) {
    if (!flag[i][j]) {
      l[i][j] = 1 + L(i - 1, j - 1);
      flag[i][j] = true;
    }
    return l[i][j];
  } else {
    if (!flag[i][j]) {
      int a = L(i, j - 1);
      int b = L(i - 1, j);
      l[i][j] = a > b ? a : b;
      flag[i][j] = true;
    }
    return l[i][j];
  }
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s1 >> s2;
    for (int j = 0; j < 1010; j++) {
      for (int k = 0; k < 1010; k++) {
        flag[j][k] = false;
      }
    }

    cout << L(s1.size() - 1, s2.size() - 1) << endl;
  }
}


#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int main() {
  int num;
  cin >> num;
  rep(k, 0, num) {
    string s1, s2;
    cin >> s1 >> s2;

    int dp
        [101]
        [101]; // s1???i??????????????§???s2???j??????????????§??§???LCS????±???????????????????
    int n = s1.length();
    int m = s2.length();
    rep(i, 0, m + 1) dp[0][i] = 0;

    rep(i, 0, n) {
      rep(j, 0, m) {
        if (s1[i] == s2[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]);
      }
    }
    cout << dp[n][m] << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int main() {
  int num;
  cin >> num;
  rep(k, 0, num) {
    string s1, s2;
    cin >> s1 >> s2;

    int dp
        [1001]
        [1001]; // s1???i??????????????§???s2???j??????????????§??§???LCS????±???????????????????
    int n = s1.length();
    int m = s2.length();
    rep(i, 0, m + 1) dp[0][i] = 0;

    rep(i, 0, n) {
      rep(j, 0, m) {
        if (s1[i] == s2[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]);
      }
    }
    cout << dp[n][m] << endl;
  }
  return 0;
}


#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main(void) {
  string st1, st2;
  int q, val[151][151];

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> st1;
    cin >> st2;

    for (int j = 0; j <= st1.length(); j++) {
      for (int k = 0; k <= st2.length(); k++) {

        if (j == 0 || k == 0)
          val[j][k] = 0;
        else {
          if (val[j - 1][k] == min(st1.length(), st2.length())) {
            val[j][k] = st1.length();
          } else {

            if (st1[j - 1] == st2[k - 1]) {
              val[j][k] = val[j - 1][k - 1] + 1;
            } else {
              val[j][k] = max(val[j - 1][k], val[j][k - 1]);
            }
          }
        }
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\\n";
  }

  return 0;
}

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main(void) {
  string st1, st2;
  int q, val[1001][1001];

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> st1;
    cin >> st2;

    for (int j = 0; j <= st1.length(); j++) {
      for (int k = 0; k <= st2.length(); k++) {

        if (j == 0 || k == 0)
          val[j][k] = 0;
        else {
          if (val[j - 1][k] == min(st1.length(), st2.length())) {
            val[j][k] = st1.length();
          } else {

            if (st1[j - 1] == st2[k - 1]) {
              val[j][k] = val[j - 1][k - 1] + 1;
            } else {
              val[j][k] = max(val[j - 1][k], val[j][k - 1]);
            }
          }
        }
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\\n";
  }

  return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main() {
  int setN;
  cin >> setN;
  string A, B;

  for (int i = 0; i < setN; i++) {
    cin >> A;
    cin >> B;
    int L[1000][1000] = {};

    for (int j = 1; j < A.length() + 1; j++) {
      for (int l = 1; l < B.length() + 1; l++) {
        if (A[j - 1] == B[l - 1]) {
          L[j][l] = 1 + L[j - 1][l - 1];

        } else {
          L[j][l] = max(L[j - 1][l], L[j][l - 1]);
        }
      }
    }
    /*
    for(int j=0;j<A.length()+1;j++) {
            cout << endl;
            for(int l=0;l<B.length()+1;l++) {
                    cout << L[j][l] << " ";

            }
    }
    */

    cout << L[A.length()][B.length()] << endl;
  }
}

#include <iostream>
#include <string>
using namespace std;

int main() {
  int setN;
  cin >> setN;
  string A, B;

  for (int i = 0; i < setN; i++) {
    cin >> A;
    cin >> B;
    int L[2000][2000] = {};

    for (int j = 1; j < A.length() + 1; j++) {
      for (int l = 1; l < B.length() + 1; l++) {
        if (A[j - 1] == B[l - 1]) {
          L[j][l] = 1 + L[j - 1][l - 1];

        } else {
          L[j][l] = max(L[j - 1][l], L[j][l - 1]);
        }
      }
    }
    /*
    for(int j=0;j<A.length()+1;j++) {
            cout << endl;
            for(int l=0;l<B.length()+1;l++) {
                    cout << L[j][l] << " ";

            }
    }
    */

    cout << L[A.length()][B.length()] << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <string>
#define MAX 10
using namespace std;

string x, y;

int lcs(int m, int n);

int main() {
  int i, q, n, m;
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> x >> y;
    cout << lcs(x.length(), y.length()) << endl;
  }
  return 0;
}

int lcs(int m, int n) {
  int i, j, maxl = 0, c[MAX][MAX];
  c[0][0] = 0;
  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 - 1] == y[j - 1])
        c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}

#include <algorithm>
#include <iostream>
#include <string>
#define MAX 1001
using namespace std;

string x, y;

int c[MAX][MAX];
int lcs(int m, int n);

int main() {
  int i, q, n, m;
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> x >> y;
    cout << lcs(x.length(), y.length()) << endl;
  }
  return 0;
}

int lcs(int m, int n) {
  int i, j, maxl = 0;
  c[0][0] = 0;
  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 - 1] == y[j - 1])
        c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1001][1001] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1010][1010] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}


#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[155][155];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}

#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[1010][1010];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}


#include <iostream>
using namespace std;
int lcs(string a, string b) {
  if (a.length() <= 0 || b.length() <= 0) {
    return 0;
  } else if (a[a.length() - 1] == b[b.length() - 1]) {
    return 1 + lcs(a.substr(0, a.length() - 1), b.substr(0, b.length() - 1));
  } else {
    return max(lcs(a.substr(0, a.length()), b.substr(0, b.length() - 1)),
               lcs(a.substr(0, a.length() - 1), b.substr(0, b.length())));
  }
};
int main() {
  int num;
  cin >> num;
  for (int i = 0; i < num; i++) {
    string a, b;
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
}

#include <iostream>
#include <string>
using namespace std;
int lcs[1001][1001];
/*int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return
max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};*/
int main() {
  int num;
  cin >> num;
  for (int l = 0; l < num; l++) {

    for (int i = 0; i < 1001; i++)
      for (int j = 0; j < 1001; j++)
        lcs[i][j] = 0;
    string a, b;
    cin >> a >> b;
    // cout<<lcs(a,b)<<endl;
    for (int i = 0; i < a.length() + 1; i++) {
      for (int j = 0; j < b.length() + 1; j++) {
        if (i == 0 || j == 0) {
          lcs[i][j] = 0;
        } else if (a[i - 1] == b[j - 1]) {
          lcs[i][j] = lcs[i - 1][j - 1] + 1;
        } else {
          if (lcs[i][j - 1] > lcs[i - 1][j])
            lcs[i][j] = lcs[i][j - 1];
          else
            lcs[i][j] = lcs[i - 1][j];
        }
      }
    }
    cout << lcs[a.length()][b.length()] << endl;
  }
}


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

#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)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (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;
}

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

#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)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

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

int main() {
  int n, m, a, dp[1002][1002] = {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;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int f(map<string, int> &dmap, const string &dstr1, string dstr2);

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string data[2];
    map<string, int> dmap;
    cin >> data[0] >> data[1];
    if (data[0].length() < data[1].length()) {
      cout << f(dmap, data[1], data[0]) << endl;
    } else {
      cout << f(dmap, data[0], data[1]) << endl;
    }
  }
}
int f(map<string, int> &dmap, const string &dstr1, string dstr2) {
  auto dit = dmap.find(dstr2);
  if (dit != dmap.end()) {
    return (*dit).second;
  }
  int j = 0;
  for (int i = 0; i < dstr1.length() && j < dstr2.length(); i++, j++) {
    while (i < dstr1.length() && dstr1[i] != dstr2[j]) {
      i++;
    }
  }
  if (j == dstr2.length()) {
    dmap.insert(pair<string, int>(dstr2, (int)dstr2.length()));
    return dstr2.length();
  }
  if (dstr2.length() == 1) {
    dmap.insert(pair<string, int>(dstr2, 0));
    return 0;
  }
  f(dmap, dstr1, dstr2.substr(1, dstr2.length() - 1));
  f(dmap, dstr1, dstr2.substr(0, dstr2.length() - 1));
  j = 0;
  for (int i = 2; i < dstr2.length(); i++) {
    int c = f(dmap, dstr1,
              dstr2.substr(0, i - 1) + dstr2.substr(i, dstr2.length() - i));
    if (j < c) {
      j = c;
    }
  }
  dmap.insert(pair<string, int>(dstr2, j));
  return j;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N;
  int data[1001][1001] = {};
  cin >> N;
  string str1, str2;
  for (int i = 0; i < N; i++) {
    cin >> str1 >> str2;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[j]) {
          data[i + 1][j + 1] = data[i][j] + 1;
        } else {
          data[i + 1][j + 1] =
              data[i + 1][j] > data[i][j + 1] ? data[i + 1][j] : data[i][j + 1];
        }
      }
    }
    cout << data[str1.length()][str2.length()] << endl;
  }
}