// Author : Chi
// created :
// Sawal : Practice

//header files
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//namespaces
using    namespace __gnu_pbds;
using    namespace std;
typedef tree <pair<int,int>, null_type, greater<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

//macros
#define ll 		    long long
#define pii         pair<ll,ll>
#define ss          second
#define ff          first
#define vi          vector<int>
#define vll         vector<long long>
#define pb(x)       push_back(x)
#define TEST  	    int TesT;cin>>TesT;while(TesT--)solve();
#define ai(a)       for(auto &i:a)cin>>i;
#define ao(a)       for(auto i:a)cout<<i<<' ';
#define TEST1       solve();
#define mod         998244353
#define aLL(x)      x.begin(),x.end()
#define r0n(i,n)    for(ll i = 0; i < n; ++i)
#define r1n(i,n)    for(ll i = 1; i <= n; ++i)
#define pdp(d,i)    fixed<<setprecision(i)<<d
#define CHI         ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define db(...)     __f(#__VA_ARGS__, __VA_ARGS__)                      //for debugging
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
//LOVE ALGO
//Make sure you have test cases (:->)
void solve(){
    int n,t;
    cin>>n>>t;
    pair<int,int>p[n];
    for(int i=0;i<t;i++){
        cin>>p[i].ff>>p[i].ss;
    }
    long long dp[n+1];
    memset(dp,0,sizeof dp);
    dp[0]=0;
    dp[1]=1;
    for(int i=2;i<=n;i++){
        dp[i]=dp[i-1];
        for(int j=0;j<t;j++){
            int r=max(0,i-p[j].ff);
            int l=max(0,i-p[j].ss-1);
            //db(i,l,r);
            dp[i]+=((dp[r]-dp[l]+mod)%mod);
            dp[i]%=mod;
        }
    }
    cout<<dp[n]-dp[n-1];
}
int main(){
    CHI
    TEST1
    return 0;
}
