#include <bits/stdc++.h>
using namespace std;
int n, m, pre[100005];
vector<int> adjl[100005];
int p[2 * 100005], size[2 * 100005];
pair<pair<int, int>, int> edge[100005];
map<pair<int, int>, int> know;
int get(int c, int v) {
  vector<int>::iterator it = lower_bound((adjl[v]).begin(), (adjl[v]).end(), c);
  if (it == adjl[v].end() || (*it) != c) return -1;
  return pre[v] + it - adjl[v].begin();
}
int f(int i) { return p[i] == i ? i : p[i] = f(p[i]); }
void connect(int c, int a, int b) {
  a = get(c, a);
  if (a == -1) return;
  b = get(c, b);
  if (b == -1) return;
  a = f(a);
  b = f(b);
  if (a == b) return;
  if (size[a] < size[b])
    p[a] = b;
  else {
    p[b] = a;
    if (size[a] == size[b]) size[a]++;
  }
}
bool query(int c, int a, int b) {
  a = get(c, a);
  if (a == -1) return false;
  b = get(c, b);
  if (b == -1) return false;
  return f(a) == f(b);
}
int main() {
  scanf("%d %d", &n, &m);
  int a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    adjl[a].push_back(c);
    adjl[b].push_back(c);
    edge[i] = make_pair(make_pair(a, b), c);
  }
  for (int i = 1; i <= n; i++) {
    sort((adjl[i]).begin(), (adjl[i]).end());
    adjl[i].resize(unique((adjl[i]).begin(), (adjl[i]).end()) -
                   adjl[i].begin());
    pre[i] = pre[i - 1] + ((int)(adjl[i - 1]).size());
  }
  for (int i = 0; i <= 2 * m; i++) p[i] = i;
  for (int i = 0; i < m; i++)
    connect(edge[i].second, edge[i].first.first, edge[i].first.second);
  int q, u, v;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &u, &v);
    if (((int)(adjl[u]).size()) > ((int)(adjl[v]).size())) swap(u, v);
    int ans = 0;
    if (know.find(make_pair(u, v)) != know.end())
      ans = know[make_pair(u, v)];
    else {
      for (int i = 0; i < ((int)(adjl[u]).size()); i++)
        if (query(adjl[u][i], u, v)) ans++;
      know[make_pair(u, v)] = ans;
    }
    printf("%d\n", ans);
  }
  return 0;
}
