Mr. Kitayuta has just bought an undirected graph consisting of *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c*<sub class="lower-index">*i*</sub>, connecting vertex *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub>.

Mr. Kitayuta wants you to process the following *q* queries.

In the *i*-th query, he gives you two integers — *u*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i*</sub>.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u*<sub class="lower-index">*i*</sub> and vertex *v*<sub class="lower-index">*i*</sub> directly or indirectly.
The first line of the input contains space-separated two integers — *n* and *m* (2 ≤ *n* ≤ 100, 1 ≤ *m* ≤ 100), denoting the number of the vertices and the number of the edges, respectively.

The next *m* lines contain space-separated three integers — *a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub> ≤ *n*) and *c*<sub class="lower-index">*i*</sub> (1 ≤ *c*<sub class="lower-index">*i*</sub> ≤ *m*). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if *i* ≠ *j*, (*a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub>, *c*<sub class="lower-index">*i*</sub>) ≠ (*a*<sub class="lower-index">*j*</sub>, *b*<sub class="lower-index">*j*</sub>, *c*<sub class="lower-index">*j*</sub>).

The next line contains a integer — *q* (1 ≤ *q* ≤ 100), denoting the number of the queries.

Then follows *q* lines, containing space-separated two integers — *u*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i*</sub> (1 ≤ *u*<sub class="lower-index">*i*</sub>, *v*<sub class="lower-index">*i*</sub> ≤ *n*). It is guaranteed that *u*<sub class="lower-index">*i*</sub> ≠ *v*<sub class="lower-index">*i*</sub>.
///Bismillahir Rahmanir Rahim
//ShzFahad
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pll pair<ll,ll>
#define vp vector<pair<ll,ll>>
#define pb push_back
#define ff first
#define ss second
#define case_(tc) cout << "Case " << tc << ": ";
#define mem(a,b) memset(a,b,sizeof(a))
#define all(qz) qz.begin(),qz.end()
#define rall(qz) qz.rbegin(),qz.rend()

#define pi acos(-1.0) 
#define tani(a) atan(a)/(pi/180)
#define sini(a) asin(a)/(pi/180)
#define cosi(a) cos(a)/(pi/180)
#define cos(a)  cos(a*pi/180)
#define sin(a)  sin(a*pi/180)
#define tan(a)  tan(a*pi/180)
const ll mod=1e9+7;

#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define dbg cout << "EKHANE" << endl;

ll Pow(ll c,ll d){return d==0? 1:c*pow(c,d-1);}
ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);}
ll lcm(ll a,ll b) {return ((a*b)/gcd(a,b));}

ll egcd(ll a,ll b,ll &x,ll &y){if(b==0){x=1;y=0;return a;}ll x1,y1;ll gcd=egcd(b,a%b,x1,y1);x=y1;y=x1-y1*(a/b);return gcd;}
ll bigMod(ll a,ll b){a%=mod;ll res=1;while(b>0){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;}

inline ll MOD(ll a){ return (a%mod + mod) %mod ; }
inline ll modAdd(ll a,ll b){ return MOD( MOD(a) + MOD(b) ) ; }
inline ll modSub(ll a,ll b){ return MOD( MOD(a) - MOD(b) ) ; }
inline ll modMul(ll a,ll b){ return MOD( MOD(a) * MOD(b) ) ; }
inline ll modInv(ll a){ return bigMod(a,mod-2) ; }

//ll dx[] = {-1, 0, 1, 0};
//ll dy[] = {0, 1, 0, -1};,
//ll dx[]={1,1,1,0,0,-1,-1,-1};
//ll dy[]={1,0,-1,1,-1,1,0,-1};
ll vis[105];
vector<ll>v[105];
void dfs(ll n)
{
    vis[n]=1;
    for(ll i=0;i<v[n].size();i++)
    {
        ll child = v[n][i];
        if(vis[child]==0)
        {
            dfs(child);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll n,m;
        cin >> n >> m;
        set<ll>col[n+5];
        while(m--)
        {
            ll x,y,z;
            cin >>  x >> y >> z;
            v[x].pb(y);
            v[y].pb(x);
            col[x].insert(z);
            col[y].insert(z);
        }
        ll q;
        cin >> q;
        while(q--)
        {
            memset(vis,0,sizeof(vis));
            ll p,q;
            cin >> p >> q;
            dfs(p);
            if(vis[q]==0)
            {
                cout << 0 << endl;
            }
            else
            {
               ll c=0;
               for(auto it:col[p])
               {
                   if(col[q].find(it)!=col[q].end())
                   c++;
               }
               cout << c << endl;
            }
        }
    } 
}
