#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[101];
queue<pair<int, int> > qu;
map<pair<int, int>, int> ans;
set<pair<int, int> > chk[101];
set<int> stk;
int bfs(int st, int en) {
  int x, cnt, i, y, co, pr;
  cnt = 0;
  while (!qu.empty()) {
    x = qu.front().first;
    pr = qu.front().second;
    qu.pop();
    for (i = 0; i < adj[x].size(); i++) {
      y = adj[x][i].first;
      co = adj[x][i].second;
      if ((chk[co].find({x, y}) != chk[co].end()) || (co != pr && pr != -1))
        continue;
      chk[co].insert({x, y});
      chk[co].insert({y, x});
      if (y == en && stk.find(co) == stk.end()) {
        stk.insert(co);
        cnt++;
      } else
        qu.push({y, co});
    }
  }
  return cnt;
}
int main() {
  int n, m, i, q, a, b, c;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  cin >> q;
  while (q--) {
    scanf("%d %d", &a, &b);
    if (ans.find({a, b}) == ans.end()) {
      qu.push({a, -1});
      c = bfs(a, b);
      if (c == 0) c = -1;
      ans[{a, b}] = c;
      ans[{b, a}] = c;
      stk.clear();
      for (i = 0; i < 101; i++) chk[i].clear();
    } else {
      c = ans[{a, b}];
    }
    if (c == -1) c = 0;
    printf("%d\n", c);
  }
  return 0;
}
