#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+5;
const int B = 320;
int n,m,tot,u[N],v[N];
vector<pair<int,int> > g[N];
vector<int> e[N];
int fa[B][N];
map<pair<int,int>,int> mp,has;
int ff(int id,int x){
return x==fa[id][x]?x:fa[id][x]=ff(id,fa[id][x]);
}
void merge(int x,int y){
x=ff(tot,x);
y=ff(tot,y);
if (x!=y){
fa[tot][x]=y;
}
}
void gtfa(int id){
for (int i=1; i<=n; i++){
fa[tot][i]=i;
}
for (auto p : g[id]){
merge(p.first,p.second);
}
}
void gtt(int id){
set<int> st;
for (auto p : g[id]){
e[p.first].push_back(p.second);
e[p.second].push_back(p.first);
st.insert(p.first);
st.insert(p.second);
}
vector<int> vc(n+1,-1);
int cur=0;
for (auto u : st){
vc[u]=++cur;
}
int sz=st.size();
for (auto x : st){
queue<int> q;
vector<int> vis(sz+1,0);
q.push(x);
vis[vc[x]]=1;
while (!q.empty()){
int cn=q.front();
q.pop();
for (auto ny : e[cn]){
if (!vis[vc[ny]]){
vis[vc[ny]]=1;
if (has[{x,ny}]){
mp[{x,ny}]++;
}
q.push(ny);
}
}
}
}
for (auto p : g[id]){
e[p.first].clear();
e[p.second].clear();
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for (int i=1; i<=m; i++){
int a,b,c;
cin>>a>>b>>c;
g[c].push_back({a,b});
}
int q;
cin>>q;
for (int i=1; i<=q; i++){
cin>>u[i]>>v[i];
has[{u[i],v[i]}]=1;
}
for (int i=1; i<=m; i++){
if (g[i].size()>=B){
tot++;
gtfa(i);
}
else{
gtt(i);
}
}
for (int t=1; t<=q; t++){
int ans=mp[{u[t],v[t]}];
for (int i=1; i<=tot; i++){
if (ff(i,u[t])==ff(i,v[t])){
ans++;
}
}
cout<<ans<<"\n";
}
return 0;
}