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

#define ll long long int
#define pb push_back
#define CR7 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<ll, ll>
#define mod 1000000007
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
//RRMMMMMMMMMRRRRRRRRRR!!

int main()
{
    CR7;
    ll t;
    cin>>t;
    while (t--)
    {
    ll a,b;
    cin>>a>>b;
    ll d = 2*a;
    if(d>b)
       cout<<"0\n";
    else
    {
        if(d==b)
           cout<<"1\n";
        else
           {
               if(b%2)
               {
                  b=b-1;
                  ll p = d/2;
                  ll r = b/2;
                  ll dif = r - p + 1;
                  ll w = (2*dif) -1;
                  cout<<((w*(w+1))/2) + w+1<<endl;
               }
               else
               {
                  ll p = d/2;
                  ll r = b/2;
                  ll dif = r - p + 1;
                  ll w = (2*dif) -1;
                  cout<<((w*(w+1))/2)<<endl;
               }
           }   
    }   
    }
    return 0;
}
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 1001;

int A[N], B[N];
int dp[N][N];
int n,m;

int solve(int x, int y) {
  if(x == n) {
    return m - y;
  }

  if(y == m) {
    return n - x;
  }

  int &res = dp[x][y];
  if(res != -1) {
    return res;
  }

  res = n + m - x - y;
  res = min(res, 1 - (A[x] == B[y]) + solve(x + 1, y + 1));
  res = min(res, 1 + solve(x, y + 1));
  res = min(res, 1 + solve(x + 1, y));
  return res;
}

int main() {
  S2(n,m);
  rep(i,0,n) S(A[i]);
  rep(i,0,m) S(B[i]);
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, 0);
  P(ans);
}
