#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, len = 0, par[200010], num[2000][2000], en[100010], bign[100010];
pair<pair<int, int>, int> e[100010];
vector<int> g[100010], v[100010], c[100010], passbig;
vector<pair<int, int> > ev[100010];
map<int, int> mp[100010];
map<pair<int, int>, int> res;
bool vis[100010];
int FIND(int pos) {
  if (par[pos] != pos) par[pos] = FIND(par[pos]);
  return par[pos];
}
void UNION(int pos, int pos2) { par[FIND(pos)] = FIND(pos2); }
void dfs(int pos) {
  if (bign[pos] > 0) passbig.push_back(pos);
  vis[pos] = true;
  for (int i = 0; i < g[pos].size(); i++) {
    if (vis[g[pos][i]]) continue;
    dfs(g[pos][i]);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &e[i].first.first, &e[i].first.second, &e[i].second);
    v[e[i].second].push_back(e[i].first.first);
    v[e[i].second].push_back(e[i].first.second);
    c[e[i].first.first].push_back(e[i].second);
    c[e[i].first.second].push_back(e[i].second);
    en[e[i].first.first]++;
    en[e[i].first.second]++;
    ev[e[i].second].push_back(e[i].first);
  }
  for (int i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
    v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
    for (int j = 0; j < v[i].size(); j++) mp[v[i][j]][i] = ++len;
  }
  for (int i = 0; i < len + 5; i++) par[i] = i;
  for (int i = 0; i < m; i++)
    UNION(mp[e[i].first.first][e[i].second],
          mp[e[i].first.second][e[i].second]);
  len = 0;
  for (int i = 1; i <= n; i++)
    if (en[i] > 150) bign[i] = ++len;
  vector<int> tmp;
  for (int i = 1; i <= m; i++) {
    tmp.clear();
    for (int j = 0; j < ev[i].size(); j++) {
      tmp.push_back(ev[i][j].first);
      tmp.push_back(ev[i][j].second);
      g[ev[i][j].first].clear();
      g[ev[i][j].second].clear();
      vis[ev[i][j].first] = vis[ev[i][j].second] = false;
    }
    for (int j = 0; j < ev[i].size(); j++) {
      g[ev[i][j].first].push_back(ev[i][j].second);
      g[ev[i][j].second].push_back(ev[i][j].first);
    }
    for (int j = 0; j < tmp.size(); j++) {
      if (vis[tmp[j]]) continue;
      passbig.clear();
      dfs(tmp[j]);
      for (int k = 0; k < passbig.size(); k++) {
        for (int p = k + 1; p < passbig.size(); p++) {
          num[bign[passbig[k]]][bign[passbig[p]]]++;
          num[bign[passbig[p]]][bign[passbig[k]]]++;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    sort(c[i].begin(), c[i].end());
    c[i].erase(unique(c[i].begin(), c[i].end()), c[i].end());
  }
  cin >> q;
  int x, y;
  for (int qn = 0; qn < q; qn++) {
    scanf("%d%d", &x, &y);
    if (en[x] > en[y]) swap(x, y);
    if (res.find(make_pair(x, y)) != res.end()) {
      printf("%d\n", res[make_pair(x, y)]);
      continue;
    }
    if (bign[x] == 0) {
      int ans = 0;
      for (int j = 0; j < c[x].size(); j++)
        if (FIND(mp[x][c[x][j]]) == FIND(mp[y][c[x][j]])) ans++;
      printf("%d\n", ans);
      res[make_pair(x, y)] = ans;
    } else {
      printf("%d\n", num[bign[x]][bign[y]]);
      res[make_pair(x, y)] = num[bign[x]][bign[y]];
    }
  }
  return 0;
}
