//Written by rraj001 :)
#include<bits/stdc++.h>
#define ll int
#define inf 1000000000
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
using namespace std;
vector<ll>adj[112];
vector<ll>d[112];
vector<ll>money[112];
ll budget,n,r,N;
ll dp[112][100011];
ll cal(ll city,ll budget)
{
if(city==N)return 0;
if(dp[city][budget]!=-1)return dp[city][budget];
ll ans=inf;
for(ll i=0;i<adj[city].size();i++)
{
if(money[city][i]<=budget)
{
ans=min(ans,cal(adj[city][i],budget-money[city][i])+d[city][i]);
}
}
return dp[city][budget]=ans;
}
int main()
{
//freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);
ll tc;
cin>>tc;
while(tc--)
{
cin>>budget>>n>>r;
for(ll i=0;i<=n;i++){adj[i].clear();d[i].clear();money[i].clear();}
for(ll i=0;i<r;i++)
{
ll src,dest,len,t;
cin>>src>>dest>>len>>t;
src--;
dest--;
adj[src].pb(dest);
d[src].pb(len);
money[src].pb(t);
}
N=n-1;
memset(dp,-1,sizeof(dp));
ll ans=cal(0,budget);
cout << (ans == inf? -1 : ans) << endl;
}
}
Ly9Xcml0dGVuIGJ5IHJyYWowMDEgOikKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBpbnQKI2RlZmluZSBpbmYgMTAwMDAwMDAwMAojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3RvcjxsbD5hZGpbMTEyXTsKdmVjdG9yPGxsPmRbMTEyXTsKdmVjdG9yPGxsPm1vbmV5WzExMl07CmxsIGJ1ZGdldCxuLHIsTjsKbGwgZHBbMTEyXVsxMDAwMTFdOwpsbCBjYWwobGwgY2l0eSxsbCBidWRnZXQpCnsKICAgIGlmKGNpdHk9PU4pcmV0dXJuIDA7CiAgICBpZihkcFtjaXR5XVtidWRn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